Common metrics with the GA API
Posted on 2015/01/01
While creating a client for Google Analytics recently I have been creating quite a collection of functions that pull metrics. So I wanted to spread the joy and add a few examples of how to pull these common metrics from the API.
The Google Analytics API allows you to pull all the information and use it in some way for your app. Including sorting, selecting fields and filtering according to certain criteria.
What follows are a few samples of combinations to use to achieve common metrics.
Bounce rate
To return the bounce rate for the first week of 2015
metrics | ga:bounceRate |
start_date | 2015-01-01 |
start_date | 2015-01-07 |
Average time on site
To return the time that users spent on the site for the first week of 2015
metrics | ga:avgTimeOnPage |
start_date | 2015-01-01 |
start_date | 2015-01-07 |
Page views
This will the total pages view for the first week of 2015
metrics | ga:pageviews |
start_date | 2015-01-01 |
start_date | 2015-01-07 |
But we can go a bit further. Let's say you need the total page views for a certain device category. Device categories represent which type of device the session was on. Namely desktop, tablet and mobile
For a quick example. Let's say we want the total pageviews from tablets in the first week of 2015
metrics | ga:pageviews |
filters | ga:deviceCategorytablet |
start_date | 2015-01-01 |
start_date | 2015-01-07 |
ga:deviceCategory can be replaced by any of those categories and the results will be filtered for only those devices. Quite cool !
Total unique sessions
This will the total unique sessions for the first week of 2015
metrics | ga:users |
start_date | 2015-01-01 |
start_date | 2015-01-07 |
Which is awesome, we now have that number. ga:users is a metric from Google after they have identified a unique session. To see the unique session per device simply add the ga:deviceCategory filter again.
Top Referrals
Let's say we want to have the top sites that sent traffic our way for the first week of 2015, and we want them sorted !
metrics | ga:pageviews |
dimensions | ga:source |
sort | -ga:pageviews |
filters | ga:mediumreferral |
start_date | 2015-01-01 |
start_date | 2015-01-07 |
Remember we can also limit this to say return only the top 15 which will require that we add max_results=15
Top search keywords
Now let's return the top search keywords that brought traffic to our site 2015, and we want them sorted !
metrics | ga:sessions |
dimensions | ga:keyword |
sort | -ga:sessions |
filters | ga:sourceMedium==google / organic |
start_date | 2015-01-01 |
start_date | 2015-01-07 |
And wala. A list of top keywords for that time period. We are only interested in organic / google as our filter shows.
Remember we can also limit this to say return only the top 15 which will require that we add max_results=15
Most popular pages
We're looking at our traffic and let's say we want to see what the most popular pages were the first week of 2015, and we want them sorted !
metrics | ga:pageviews |
dimensions | ga:pageTitle ga:pagePath |
sort | -ga:pageviews |
start_date | 2015-01-01 |
start_date | 2015-01-07 |
And that's it. This query will return the most popular pages for the time range.
That's all folks
Those are all the metrics I needed. Feel free to share in the comments so we can add more if you have some specific use cases.
Hope this helps to accelerate your Google Analytics API calls !
What's currently keeping me busy

Testing and keeping websites safe

Tech/product of new incubating startups

Advocate and educate on the Google Cloud

Easy prescribed book management

Loadshedding being constantly updated and watched

Secret management for PAAS

National microchip database

Youtube channel of edited meetup talks

Gaming Youtube Channel