Many websites use Google Analytics to gather data on site traffic and user activity. In addition to providing crucial business insights to users through the user console, Google Analytics also has many potential applications in code. For example, a client recently requested a new component for their website that would display the most viewed pages on the site, excluding internal traffic. This client was using Google Analytics on their website, so all the data they were requesting was already available, and just needed to be accessed.
There are several steps that must be taken to set up the Google Analytics Reporting API so that it can be accessed in code:
Enable the Analytics API
- Go to http://code.google.com/apis/console
- Select the drop down and create a project if you do not already have one
- Once the project is created, go to the APIs and Services section from the main menu
- Click “Enable APIs and Services;” search for Google Analytics Reporting API and click Enable.
Create a Service Account to access the API
Full documentation can be found on developers.google.com, but here is a summary of the setup steps:
- Go to IAM & Admin from the main menu
- Click on Service Accounts and then click Create Service Account
- Choose a name for your account and for Role select Project > Owner. Check off “Furnish a new private key” and select Key type: JSON
- Click Create. A json file with your public/private key pair will be generated and downloaded to your machine; you will need this later in your code
Google documentation: https://developers.google.com/identity/protocols/OAuth2ServiceAccount
Give the Service Account access to your Google Analytics account
- Log into Google Analytics and go to Admin
- Click the account dropdown and select the account/site you would like to access
- Under the Account column, click on “User Management”
- Click “+” to add a new user and enter the email address that was generated for the service account you created (e.g. email@example.com)
- Your service account email address can be found at https://console.developers.google.com/iam-admin/serviceaccounts/ under Service account ID. It is also in the public/private key pair file that was downloaded previously, under “client_id”
Google documentation: https://support.google.com/analytics/answer/1009702?hl=en
Now the fun part
Now that the setup is done, it’s time to start coding. I will provide and explain a code example, with two preliminary steps that must be done in your project.
1. Add the key file that was downloaded previously to the root level of your project. Rename it something easier to read, such as “service_account_key.json”
2. Use NuGet to install Google.Apis.AnalyticsReporting.v4
The following example queries Google Analytics to get the most viewed pages on the site; it retrieves the top 1000 pages ordered by page views, descending.
The first thing being set is the View Id. This can be found in the Google Analytics console. Your account may have multiple Properties and Views; each one has a different ID, and any of these can be used to pull specific data. For example, if your site includes subdomains and you have Google Analytics set up to track traffic on the subdomains separately from the main site, you can use the Id of the subdomain view to get page views for just the subdomain.
Within the GetReport() method, the key file that was placed in the project root is accessed in order to create the credentials for the API, and a new AnalyticsReportingServiceis initialized using those credentials.
In the example above, a start date is provided, which is used to set the date range for the query. There are three other properties added to the request: Metrics, Dimensions, and OrderBys. Metrics and Dimensions specify which fields are returned by the API; Metrics are quantitative, Dimensions are qualitative. For example, page views is a metric; page path is a dimension. A full list of the available Metrics and Dimensions can be found here: https://developers.google.com/analytics/devguides/reporting/core/dimsmets
OrderBy tells the query which field to order on, and in which order. Further documentation on the OrderBy property can be found here: https://www.any-api.com/googleapis_com/analyticsreporting/docs/Definitions/OrderBy
The report request is sent to the API, and the results are transformed into a list of custom Analytics Page objects using Linq.
While this example queries for top pages, there are many other things that the Google Analytics API can be used for. In addition to querying for top pages sitewide, you can query for top landing pages or top exist pages, or you can query the site searches to recommend search keywords or suggest pages that are most commonly searched for. For more query examples, check out the documentation here:
The Query Explorer is also an excellent tool to build and test queries: