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.