Introducing Google Analytics
Although there are alternatives out there, Google Analytics is certainly one of, if not the most popular web and mobile analytics platforms in existence. It’s been around for just over 10 years, is incredibly reliable, and has a comprehensive feature set. And did I mention it’s free? It’s so popular, developers have even used the platform to monitor things it isn’t designed for, like data from your company’s Slack team.
Having said that, maybe Google’s reporting platform is fine, but its own web and mobile applications for analysing reporting data don’t live up to your business needs or expectations. Fret not! Google Analytics comes with a pretty decent API you can integrate to get the data out and do with it what you will, which is the point of this post. So let’s get started!
1. Getting Analytics API access
First things first, you need access to Google’s API. Because the Analytics API returns user-specific data, you need to use OAuth to get user-specific access tokens to query against the API.
Because every application will be different, I’m going to skip the full OAuth setup portion and suggest you use Google’s OAuth playground to get an access token to use for testing:
- Open the OAuth playground (https://developers.google.com/oauthplayground)
- Select all options under “Google Analytics API v3”:
- Click “Authorize APIs.”
At this point, Google will direct you to the Google authentication page and request access to your analytics data. Log in with the account you’d like to test, and allow the access you’ve requested. Google will give you an authorization code, which you can use to get the access and refresh tokens.
Click “Exchange authorization code for tokens” and you’ll have an access token you can use to access the analytics data for the user account you authenticated with.
Okay, now we can get started with our first API request!
2. Making your first API requests
Let’s start by simply querying for the analytics “profiles” your account has access to. In Google Analytics, your data for each web site or application is typically reported to a unique profile, which can have multiple “views” associated with it that filter or segment the data in different ways.
If you’re still using Google’s OAuth playground, you can begin to make requests by simply pasting in the “Request URI” field. If you’re using something else to perform your HTTP requests (like Postman), you’ll have to append “?access_token=(token from part 1)” to authorize your requests.
Start by querying “https://www.googleapis.com/analytics/v3/management/accounts/.” By default, you’ll get a JSON response payload that includes an “items” array for every profile you have access to. Record the ID for the profile you’re interested in, and you’ll use that to query for the views on that profile that you have access to.
This second query URI will be:
click here where “profileID” is the ID from one of the profiles you found in the first query. This query returns another array of items, this time representing the views on the profile you’re able to view. The ID you’re looking for here is called the defaultProfileId. We finally have the piece of information we need to begin querying for interesting data.
3. Basic metric queries
Now that we have a particular view’s ID and your access token, we can begin to query for things like sessions and page views. Data points like these are called metrics, and can be queried by asking for metrics across various dimensions like time, referral source, and geographic location. Certain metrics and dimensions can’t be queried together, so you’ll likely want to check out Google’s .
Queries for data will be the URI “https://www.googleapis.com/analytics/v3/data/ga” with the following required GET URL parameters:
- “ids”: the string “ga:profileID”, where “profileID” is the defaultProfileId we found in part two.
- “start-date”: a string representing the time you’d like to begin querying for data. This can be absolute, like “2017-01-01” or relative, like “7daysAgo.”
- “end-date”: a string representing the time you’d like to query until. This again can be absolute, or relative (like “yesterday”).
- “metrics”: The final required parameter is the list of data types you’d like to query for. These are in the format “ga:metric,” where metric is something like “sessions” or “pageViews.” You can query for multiple metrics by separating them by commas (for example “metrics=ga:sessions,ga:pageViews”).
Additionally, you can specify a list of dimensions to view your metrics across. If you’d don’t specify any dimensions, the API will simply give you back totals for the time period you specified (again, dimensions are in the format “ga:dimension”).
An example query could be something like the following, which returns the sessions and pageviews from the last seven days split across the week of the year:
This will return lots of data, but the data itself will be in the “rows” section:
"02", // Dimension value (second week of the year)
"791", // Metric 1 value (sessions: 791 for week 02)
"0" // Metric 2 value (pageViews: 0 for week 02)
At this point, it’s simply a matter of tweaking your combination of metrics and dimensions to get the data you’re looking for. You can also add additional parameters to your query like segments, filters, and sort options. Check out the API referencefor more details.