Get data for a single page from the Google Analytics APIGet data for a single page from the Google Analytics API

Posted July 14th, 2009 in PHP

The Google Analytics API has a filters feature which allows you to filter the data in a variety of ways, including filtering by URL. This means that you can get specific metrics (e.g. pageviews etc) for a single page from the GA API.

Dimensions and Metrics Available

Refer to the Dimensions & Metrics Reference in the Google Analytics API documentation for a complete list and full details about the dimensions and metrics available.

Filters

Refer to the Filters section of the "Data API - Data Feed" section of the Google Analytics API documentation for complete details about filters.

The GA API request URL

When querying the Google Analytics API for just a specific URL, add a filter for ga:pagePath set to an exact match for the URL you wish to query.

At the time of writing this post the most popular page on this blog is "How to get and set form element values with jQuery" which has the page /jquery-get-set-form-values/ so I will use that as the example for the filter.

With proper escaping, an exact match == looks like %3D%3D and /jquery-get-set-form-values/ looks like %2Fjquery-get-set-form-values%2F Therefore the following would be appended to your request URL:

&filters=ga:pagePath%3D%3D%2Fjquery-get-set-form-values%2F

A full example request might look like this, where I've added linebreaks to make it more legible:

https://www.google.com/analytics/feeds/data?ids=ga:7426158
  &dimensions=ga:pagePath&metrics=ga:pageviews,ga:uniquePageviews,
  ga:bounces,ga:entrances,ga:exits,ga:newVisits,ga:timeOnPage
  &start-date=2009-06-14&end-date=2009-07-13&max-results=10
  &start-index=1&filters=ga:pagePath%3D%3D%2Fjquery-get-set-form-values%2F

Using my GA API PHP Class

If you are using my Google Analytics API PHP Class you can do the above like so:

$filters = new analytics_filters("ga:pagePath", "==", "/jquery-get-set-form-values/");
$data = $api->data($id, 'ga:pagePath', 'ga:pageviews,ga:uniquePageviews,ga:bounces,ga:entrances,ga:exits,ga:newVisits,ga:timeOnPage', '', '', '', 10, 1, $filters, true);

Doing print_r($data) on the above will show something along these lines:

Array
(
    [/jquery-get-set-form-values/] => Array
        (
            [ga:pageviews] => 5206
            [ga:uniquePageviews] => 4816
            [ga:bounces] => 4116
            [ga:entrances] => 4592
            [ga:exits] => 4572
            [ga:newVisits] => 3525
            [ga:timeOnPage] => 220224.0
        )

)

The timeOnPage metric is aggregated for all pageviews in seconds. To convert it to per pageview you'd need to do a caclulation like $data['ga:timeOnSite'] / $data['ga:pageviews'];

Other Dimensions

The dimension doesn't have to be ga:pagePath; it could be any valid dimension such as ga:keyword (to get the keywords used by visitors to reach your site, via both paid ads and through search engine results) or ga:source,ga:referralPath (the domain and path of a referring url to the page).

Related posts:

Share or Bookmark

Share or Bookmark this page using the following services. You will need to have an account with the selected service in order to post links or bookmark this page.

Subscribe or Follow

Subscribe via RSS or email, or follow me on Facebook or Twitter below. The RSS icon takes you through to Feedburner where you can select the service or application to use.

Comments

blog comments powered by Disqus