Analytics API和PHP - 以不同格式获取报告

Nic*_*ick 15 php google-analytics google-analytics-api

以下代码返回一个对象 Google_Service_AnalyticsReporting_GetReportsResponse

$body = new Google_Service_AnalyticsReporting_GetReportsRequest();
$body->setReportRequests($aRequests);
return $this->oAnalytics->reports->batchGet($body);
Run Code Online (Sandbox Code Playgroud)

我想知道我是否可以以不同的格式获取报告,例如: Array(Dimension,value)

cmc*_*cmc 6

是.

适用于PHP的Google Reporting v4 SDK看起来已经从java代码自动转换,以及所有常见的对象矫枉过正.

要转换为数组,假设您要求一个维度和一些指标:

$data = []
foreach ($this->oAnalytics->reports->batchGet($body)->getReports()[0]->getData()->getRows() as $row) {
  $key = $row->dimensions[0]; // chose a unique value or dimension that works for your app, or remove and use $data[] = $value; below
  $values = array_merge($row->metrics, $row->dimensions);    
  $data[$key] = $value;
}
Run Code Online (Sandbox Code Playgroud)