[英]Get events data from Google Analytics API with PHP
我想獲取其中一個使用Google Analytics(分析)的應用程序的事件。 我對官方文檔中的示例不感到困惑。 我可以使用它,但我不知道如何在案件中使用它。
require_once 'GA/vendor/autoload.php';
// Start a session to persist credentials.
session_start();
// Create the client object and set the authorization configuration
// from the client_secretes.json you downloaded from the developer console.
$client = new Google_Client();
$client->setAuthConfigFile('client_secrets.json');
$client->addScope(Google_Service_Analytics::ANALYTICS_READONLY);
// If the user has already authorized this app then get an access token
// else redirect to ask the user to authorize access to Google Analytics.
if (isset($_SESSION['access_token']) && $_SESSION['access_token']) {
// Set the access token on the client.
$client->setAccessToken($_SESSION['access_token']);
// Create an authorized analytics service object.
$analytics = new Google_Service_Analytics($client);
在這段代碼之后,我想獲取事件數據-我應該如何指定計數器ID和事件信息?
使用查詢瀏覽器,我設法創建了我需要的查詢:
googleapis.com/analytics/v3/data/ga?ids=ga%3A111111&start-date=30daysAgo&end-date=2016-05-11&metrics=ga%3AtotalEvents&dimensions=ga%3AeventLabel
我不確定計數器ID是什么意思。 您有了分析服務,現在只需要發出請求即可。
$params = array('dimensions' => 'ga:AeventLabel');
// requesting the data
$data = $analytics->data_ga->get("ga:89798036", "30daysAgo", "2016-05-11", "ga:totalEvents", $params );
// displaying the data
foreach ($data->getRows() as $row) {
print "ga:AeventLabel ".$row[0]." ga:totalEvents ".$row[1]";
}
我將此類與以下代碼一起使用,並且效果很好:
require 'GoogleAPI/gapi.class.php';
define('ga_profile_id','YOUR-DEV-ID');
$ga = new gapi('YOUR-DEV-ID@developer.gserviceaccount.com','GoogleAPI/cert.p12');
$ga->requestReportData('YOUR-APP-ID',array('eventAction'),array('visitors'),null,"eventAction==loginViaWebsite",$"2016-05-11","2016-05-11");
$logins = $ga->getResults();
if($logins) $logins = $logins[0]->getMetrics();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.