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:
A full example request might look like this, where I've added linebreaks to make it more legible:
Using my GA API PHP Class
If you are using my Google Analytics API PHP Class you can do the above like so:
Doing print_r($data) on the above will show something along these lines:
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).
0 comments:
Post a Comment