2

所以我想在自定義PHP後端顯示GA圖表。所以我創建了APP在https://console.developers.google.com/,但我無法弄清楚如何訪問我自己的谷歌分析數據,而不要求用戶登錄。現在我有簡單的代碼:PHP後端Google Analytics API

$client = new Google_Client(); 
    $client->setApplicationName("App Name"); 
    $client->setClientId(CLIENT_ID); 
    $client->setClientSecret(CLIENT_SECRET); 
    $client->setDeveloperKey(APP_ID); 
    $client->setScopes(array('https://www.googleapis.com/auth/analytics.readonly')); 
    $client->authenticate(); 
    $token = $client->getAccessToken(); 
    $service = new Google_Service_Analytics($client); 
$optParams = array(
     'sort' => 'ga:pageviews', 
     'max-results' => '5'); 
$results = $service->data_ga->get(
     'ga:123456', 
     '2015-01-01', 
     '2015-01-30', 
     'ga:pagePath', 
     $optParams); 

print_r($results); 

我想我所做的一切錯誤:),所以我的問題是,如何讓任何人都可以通過PHP腳本訪問我自己的谷歌分析數據?

回答

1

如果你想訪問自己的帳戶,那麼我建議你看看使用service account

一旦您在Google Developer console上創建了service account,請記得轉到Google Analytics帳戶,並在服務帳戶級別爲服務帳戶的電子郵件地址提供對Google Analytics數據的訪問權限。你可以在這裏閱讀關於如何做到這一點Google Service Account PHP從該教程撕下的代碼。

session_start();   
require_once 'Google/Client.php'; 
require_once 'Google/Service/Analytics.php';   
/************************************************ 
The following 3 values an befound in the setting 
for the application you created on Google  
Developers console.   Developers console. 
The Key file should be placed in a location  
that is not accessable from the web. outside of 
web root.  web root. 

In order to access your GA account you must  
Add the Email address as a user at the  
ACCOUNT Level in the GA admin.   
************************************************/ 
$client_id = 'xxx-nk421gjc2v8mlr2qnmmqaak04ntb1dbp.apps.googleusercontent.com'; 
$Email_address = '[email protected]om'; 
$key_file_location = '629751513db09cd21a941399389f33e5abd633c9-privatekey.p12';  
$client = new Google_Client();  
$client->setApplicationName("Client_Library_Examples"); 
$key = file_get_contents($key_file_location);  
// seproate additional scopes with a comma 
$scopes ="https://www.googleapis.com/auth/analytics.readonly"; 
$cred = new Google_Auth_AssertionCredentials( 
$Email_address,   
array($scopes),   
$key   
);  
$client->setAssertionCredentials($cred); 
if($client->getAuth()->isAccessTokenExpired()) {   
$client->getAuth()->refreshTokenWithAssertion($cred);  
}  
$service = new Google_Service_Analytics($client); 
$accounts = $service->management_accountSummaries->listManagementAccountSummaries(); 
//calulating start date 
$date = new DateTime(date("Y-m-d"));  
$date->sub(new DateInterval('P10D'));  
//Adding Dimensions 
$params = array('dimensions' => 'ga:userType'); 
// requesting the data 
$data = $service->data_ga->get("ga:78110423", $date->format('Y-m-d'), date("Y-m-d"), "ga:users,ga:sessions", $params); 
?><html>  
<?php echo $date->format('Y-m-d') . " - ".date("Y-m-d"). "\n";?>  
<table> 
<tr>  
<?php  
//Printing column headers 
foreach($data->getColumnHeaders() as $header){ 
print "<td>".$header['name']."</td>";  
}  
?>  
</tr>  
<?php  
//printing each row. 
foreach ($data->getRows() as $row) {   
print "<tr><td>".$row[0]."</td><td>".$row[1]."</td><td>".$row[2]."</td></tr>";  
}  
//printing the total number of rows 
?>  
<tr><td colspan="2">Rows Returned <?php print $data->getTotalResults();?> </td></tr>  
</table>  
</html> 
+0

太棒了,正是我所需要的。但是,該教程有點老,所以我們需要將'require_once'Google/Client.php';'和'require_once'Google/Service/Analytics.php';'改爲'require_once'Google/autoload.php' ; '並進一步注意,當你添加服務的電子郵件到GA帳戶你需要等待(不知道多久)它在谷歌服務器之間同步(我猜)或者你會得到錯誤:'(403)用戶不擁有任何Google Analytics(分析)帳戶.' –

+0

我將盡快更新教程,謝謝你的支持。 – DaImTo