在非对象

时间:2016-05-27 18:00:03

标签: google-analytics google-analytics-api

我正在努力获取一些谷歌分析api统计数据,并且能够使用此功能获得指标......

$results = getResults($analytics, $profile->getId(), $value);
$rows = $results->getRows();
$myvalue = $rows[0][0];
echo "<b>$value:</b> ". round($myvalue, 0) ."</br>";  

但是当我使用在上面的代码中工作的相同分析对象调用batchGet时,下面的代码会引发错误(请参阅帖子标题)。不确定为什么或者是否有另一种方法来获取我追求的维度数据。

$device = new Google_Service_AnalyticsReporting_Dimension();
$device->setName("ga:deviceCategory");
// Create the ReportRequest object.
$request = new Google_Service_AnalyticsReporting_ReportRequest();
$request->setDimensions(array($device));
$body = new Google_Service_AnalyticsReporting_GetReportsRequest();
$body->setReportRequests( array( $request) );
return $analytics->reports->batchGet( $body );

以下是我实例化$ analytics对象的方法

function getService()
{
// service account email, and relative location of your key file.
$service_account_email = 'email@gserviceaccount.com';
$key_file_location = 'pathto/file.p12';

// Create and configure a new client object.
$client = new Google_Client();
$client->setApplicationName("Analytics");
$analytics = new Google_Service_Analytics($client);

$key = file_get_contents($key_file_location);
$cred = new Google_Auth_AssertionCredentials(
    $service_account_email,
    array(Google_Service_Analytics::ANALYTICS_READONLY),
    $key
);
$client->setAssertionCredentials($cred);
if($client->getAuth()->isAccessTokenExpired()) {
  $client->getAuth()->refreshTokenWithAssertion($cred);
}

return $analytics;
}

1 个答案:

答案 0 :(得分:0)

仍然不知道为什么batchGet会抛出错误,但下面的代码可以运行并返回deviceCategories。

  $optParams = array(
  'dimensions' => 'ga:deviceCategory',
  'filters' => 'ga:medium==organic');

$devices = $analytics->data_ga->get(
  'ga:'.$profile->getId(),
  '2015-05-01',
  '2015-05-15',
  'ga:sessions',
  $optParams);

print_r($devices);