Home / PHP / Google Analytics PHP API: Getting data without a dimension

Google Analytics PHP API: Getting data without a dimension

My Google Analytics API PHP class was initially developed assuming that a dimension would always be supplied but it doesn’t actually need to be, so I modified it to deal with no dimension and present an example here.

Thanks to Turner Walters from www.visionpointmarketing.com for pointing out this issue and helping with the solution.

If both a dimension (more more than one dimension) and a metric is supplied to the data() function then a multi-dimensional array is created storing the dimension(s) and metrics. I have now modified the function so that if there is no dimension then is a single dimensional array containing just the metric data instead.

This is useful if you want to get a summary for the account profile as a whole, instead of looking at data for specific dimensions.

For example, if you wanted to get an account overview for the last month showing counts for the number of bounces (i.e. one page only visits), new visitors, pageviews and unique pagesviews, you would do this:

$data = $api->data($id, '', 'ga:bounces,ga:newVisits,ga:visits,ga:pageviews,ga:uniquePageviews');

Doing print_r($data) would result in something along these lines:

Array
(
    [ga:bounces] => 80358
    [ga:newVisits] => 77986
    [ga:visits] => 91141
    [ga:pageviews] => 111634
    [ga:uniquePageviews] => 101403
)

You could then potentially loop through all the accounts in the class’s accounts array and get a summary for each account.