简体   繁体   中英

Get google analytics real time data using php

I Want to get google analytics real time data for active users. I have used google api client php from 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);
}

I am getting proper result. I want to get realtime data but unable to get it. I have tried as below.

$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

Please give me solution to get real time data.

The problem is that you checked out a very old version of the client library. Go ahead and checkout the v1-master branch from github:

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

I would also suggest following the instructions in the Hello Analytics API php to help get you started.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM