Search code examples
phpgoogle-analytics-api

Google Analitycs API get data with dimensions


I work with Google Analitycs API. I want to get all sessions for last 7 days per day like this in only one API call:

[day 1] -> 10

[day 2] -> 100 ...

I use this:

$service->data_ga->get('ga:'.$profile['id'],'7daysAgo','today','ga:sessions');

It works fine but return the sum. I've checked with https://ga-dev-tools.appspot.com/query-explorer/ and for me needed i should add metrics ga:date so the date to be individualy per day not all.

I've tried to add metrics at the end of line:

$service->data_ga->get('ga:'.$profile['id'],'7daysAgo','today','ga:sessions','ga:date');

I got this error:

Uncaught exception 'Google_Exception' with message '(get) missing required param: 'start-date'' in


Solution

  • The answer is this:

    $SecondaryParams = array('dimensions' => 'ga:date');
    
    $results = $service->data_ga->get('ga:'.$profile['id'],
                                      '7daysAgo',
                                      'today',
                                      'ga:sessions',   
                                       $SecondaryParams);
    

    Send dimensions as array.