google-analyticsgoogle-analytics-apiuniversal-analytics

Analytics php api get json response (UA)


I run this query via the analytics api, to get my results:

$ids = "ga:xxxxxx";
$start_date = "2015-01-01";
$end_date = "2015-10-08";
$metrics = "ga:pageviews";
$dimensions = "ga:pagePath";
$optParams = array('dimensions' => $dimensions, 'output' => 'json');
$data = $analytics->data_ga->get($ids,$start_date,$end_date,$metrics,$optParams); 
$items = $data->getRows();

Currently I get an array as response, but I want a json string. What is wrong in my code?


Solution

  • There is nothing wrong with your code. The PHP client library returns an object Google_Service_Analytics_GaData, You can either convert that object into a JSON object or you could construct your API request with out the client library to get at the direct API response.