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.

Categories PHP

Google Analytics API dimensions vs metrics

The Google Analytics API has dimension and metric fields for getting data. If just a metric is specified then the data returned is for the time period as a whole. If dimensions are also supplied then the data is segmented by the metric. This post looks at the XML returned when a dimension is supplied and when it is not.

What is a dimension?

Dimensions are segments like browser and browser version, country, landing and exit pages, the URL or title of a page, and the source website a visitor has come from.

What is a metric?

Metrics are the counts of a dimension for specific data types, such as counts of new visitors, page views, unique pageviews and so on.

Retrieving data for a metric

If you were going to get the count of pageviews and unique pageviews for an account profile as a whole without specifying a dimension, you would specify “ga:pageviews,ga:uniquePageviews” as the metric when making the API call. Example XML output for this for my blog would be something like this:

<entry>
 <id>http://www.google.com/analytics/feeds/data?ids=ga:7426158&&start-date=2009-04-04&end-date=2009-05-03</id>
 <updated>2009-05-02T17:00:00.001-07:00</updated>
 <title type="text"/>
 <link rel="alternate" type="text/html" href="http://www.google.com/analytics"/>
 <dxp:metric confidenceInterval="0.0" name="ga:pageviews" type="integer" value="111634"/>
 <dxp:metric confidenceInterval="0.0" name="ga:uniquePageviews" type="integer" value="101403"/>
 </entry>
 

Note that there are two separate dxp:metric nodes which store the metric name in a name element and the value element of the node is the count for that metric. Because no dimension was specified there will only be one <entery> node.

Retrieving data for the dimension vs metric

If the “ga:pagePath” was used the the dimension value then there will be a number of <entry> nodes equal to or less than the max-results value of the API call. Some example output for one of the <entry> nodes from my blog is as follows:

<entry>
 <id>http://www.google.com/analytics/feeds/data?ids=ga:7426158&ga:pagePath=/jquery-get-set-form-values/&start-date=2009-04-04&end-date=2009-05-03</id>
 <updated>2009-05-02T17:00:00.001-07:00</updated>
 <title type="text">ga:pagePath=/jquery-get-set-form-values/</title>
 <link rel="alternate" type="text/html" href="http://www.google.com/analytics"/>
 <dxp:dimension name="ga:pagePath" value="/jquery-get-set-form-values/"/>
 <dxp:metric confidenceInterval="0.0" name="ga:pageviews" type="integer" value="2903"/>
 <dxp:metric confidenceInterval="0.0" name="ga:uniquePageviews" type="integer" value="2684"/>
 </entry>
 

Note that there is now an additional <dxp:dimension> node which specifies the dimension name and value as elements of the node. A script parsing data from this XML would need to loop through the <entry> nodes and extract the dimension names and values, and metric names and values into some sort of array or other data format to be able to use it.

PHP Analytics API Class

I have written and am continuing to develop and PHP Class for accessing the Google Analytics API which converts the above XML into easy to use arrays. Please refer to my Google Analytics API and PHP Series for more details and to download the class.

Password protect a directory with Apache

This post is more of a self-reference than anything because there are probably fifty million other posts on the Internet showing how to password protect a directory with an Apache .htaccess file…

Fix “the operation can’t be completed because you don’t have permission to access some of the items” error on OSX

I run my webserver and file server from a virtual machine with the files shared using Samba (yes, now that I’m running on OSX I should really share the files with NFS but that’s for another day). When trying to copy files from the Mac to the Samba share I was getting the error message "The operation can’t be completed because you don’t have permission to access some of the items".