[英]Get events data from Google Analytics API with PHP
I want to get events of one of my apps that uses Google Analytics. 我想获取其中一个使用Google Analytics(分析)的应用程序的事件。 I am little confused with example given in the official documentation .
我对官方文档中的示例不感到困惑。 I make it work but i don't understand how to use it for my case.
我可以使用它,但我不知道如何在案件中使用它。
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);
After this code I want to get events data - how should I specify counter id, and event info? 在这段代码之后,我想获取事件数据-我应该如何指定计数器ID和事件信息?
Using Query Explorer I managed to created query that I need: 使用查询浏览器,我设法创建了我需要的查询:
googleapis.com/analytics/v3/data/ga?ids=ga%3A111111&start-date=30daysAgo&end-date=2016-05-11&metrics=ga%3AtotalEvents&dimensions=ga%3AeventLabel
I am not sure what you mean by counter id. 我不确定计数器ID是什么意思。 You have your analytics service you just need to make the request now.
您有了分析服务,现在只需要发出请求即可。
$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]";
}
I use this class with the following code and it works well: 我将此类与以下代码一起使用,并且效果很好:
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.