Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Analytics API & PHP - Get reports in different format

The following code returns an object of Google_Service_AnalyticsReporting_GetReportsResponse

$body = new Google_Service_AnalyticsReporting_GetReportsRequest();
$body->setReportRequests($aRequests);
return $this->oAnalytics->reports->batchGet($body);

I'm wondering if I can get Reports in a different format, example: Array(Dimension,value)

like image 527
Nick Avatar asked Jun 13 '16 12:06

Nick


People also ask

What is analytic API?

API Analytics are designed to capture a wide range of analytics data for use by several different teams but the data is typically used for analysis in non-realtime scenarios. One of the primary differences between API Monitoring and API Analytics is the alert mechanism built into API Monitoring.

Is Google Analytics an API?

The Google Analytics Reporting API v4 provides programmatic methods to access report data in Google Analytics (Universal Analytics properties only). With the Google Analytics Reporting API, you can: Build custom dashboards to display Google Analytics data.

Is Google Analytics API free?

Basic Google Analytics is free and Google is trying to keep people from abusing it and steer heavy users to its premium service.

Where is my Google Analytics API?

For information on the Analytics APIs, refer to https://developers.google.com/analytics/.


1 Answers

Yes.

The Google Reporting v4 SDK for PHP looks like it has been automatically converted from java code, right along with all the usual object overkill.

To convert to an array, assuming you asked for one dimension and some metrics:

$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;
}
like image 185
cmc Avatar answered Oct 11 '22 05:10

cmc