使用PHP从Google Analytics API获取事件数据

使用PHP从Google Analytics API获取事件数据,php,google-analytics-api,google-api-php-client,Php,Google Analytics Api,Google Api Php Client,我想获取我的一个使用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 t

我想获取我的一个使用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();

我终于得到了我想要的东西