使用php获取谷歌分析实时数据

时间:2015-11-09 14:08:22

标签: php google-api google-analytics-api google-api-php-client

我想为活跃用户获取谷歌分析实时数据。我用过google api client php     https://code.google.com/p/google-api-php-client/source/browse/trunk/src/?r=474#

For screenviews after setting metrics and dimensions

$metrics    =   'ga:screenviews';
$optParams  =   array('dimensions' => 'ga:screenName');

$service = new Google_AnalyticsService($client);
try {
    $result = $service->data_ga->get(
        $GA_VIEW_ID,
        $start_date,
        $end_date,
        $metrics,
        $optParams
    );
} catch(Exception $e) {
    var_dump($e);
}

我得到了正确的结果。 我想获得实时数据但无法获得它。我试过如下。

$ metrics ='rt:medium'; $ optParams = array('dimensions'=>'rt:activeUsers');

try {
  $results = $service->data_realtime->get(
      $GA_VIEW_ID,
      'rt:activeUsers',
      $optParams);
} catch (apiServiceException $e) {
  // Handle API service exceptions.
  $error = $e->getMessage();
}

// Error  : Undefined property: Google_AnalyticsService::$data_realtime

请给我解决方案以获取实时数据。

1 个答案:

答案 0 :(得分:0)

问题是您签出了一个非常旧版本的客户端库。继续从github检查v1-master分支:

git clone -b v1-master https://github.com/google/google-api-php-client.git

我还建议您按照Hello Analytics API php中的说明操作,以帮助您入门。