大約一週後 - 當然,在同一天我發佈這個問題,我終於設法自己解決這個問題。
這是我如何去:
我從their github下載了最新的谷歌客戶端API(對於PHP)。
我在我的應用程序/ third_party文件夾中添加了Google文件夾(src)。
在我的控制我包括所需的文件做:
require_once(BASEPATH . '../application/third_party/Google/Client.php');
require_once(BASEPATH . '../application/third_party/Google/Service/Analytics.php');
然後我在下面,下面的代碼進行授權與服務帳戶(你從Google Console獲取和選擇項目>的API & AUTH >憑據>然後創建一個新的客戶端ID,選擇服務帳戶,完成後,點擊「生成新的P12鍵」和鍵添加到您的THIRD_PARTY /谷歌文件夾:
session_start();
$client_id = '<YOUR_CLIENT_ID>'; //Client ID
$service_account_name = '<YOUR_CLIENT_EMAIL>'; //Email Address
$key_file_location = BASEPATH . '../application/third_party/Google/<YOUR KEY.p12>'; //key.p12
$client = new Google_Client();
$client->setApplicationName("ApplicationName");
$service = new Google_Service_Analytics($client);
if (isset($_SESSION['service_token'])) {
$client->setAccessToken($_SESSION['service_token']);
}
$key = file_get_contents($key_file_location);
$cred = new Google_Auth_AssertionCredentials(
$service_account_name,
array(
'https://www.googleapis.com/auth/analytics',
),
$key,
'notasecret'
);
$client->setAssertionCredentials($cred);
if($client->getAuth()->isAccessTokenExpired()) {
$client->getAuth()->refreshTokenWithAssertion($cred);
}
$_SESSION['service_token'] = $client->getAccessToken();
下面的代碼是FO從上31天
$analytics = new Google_Service_Analytics($client);
$profileId = "ga:<YOUR_PROFILE_ID>";
$startDate = date('Y-m-d', strtotime('-31 days')); // 31 days from now
$endDate = date('Y-m-d'); // todays date
$metrics = "ga:sessions";
$optParams = array("dimensions" => "ga:date");
$results = $analytics->data_ga->get($profileId, $startDate, $endDate, $metrics, $optParams);
$data['report'] = $results->rows; //To send it to the view later
[R越來越會話(頁面訪問量)要獲得所有可以使用的維度和指標,use this link。關於如何將其發送到視圖 實施例:
$this->view->load('your_view', $data);
要寫入它作爲我只是用Google charts一個圖表,在JS(在視圖中)我剛剛從$循環數據的數據[「報告」 ]繪製圖表。
希望這將有助於未來這個問題的人。
我不明白爲什麼有人會低估這一點,因爲更多的人可能有這個問題!我已經嘗試過Sparks for Codeigniter的插件,但那個是兩年前的,這意味着api已經過時了,並且所有在Google網站上的示例中的命名約定都是錯誤的... – Michael 2014-09-04 14:35:56