Google Analytics Reporting API in Ruby on Rails

How to integrate the Google Analytics Reporting API into a Ruby on Rails application.

Abigail McPhillips
Pixie Labs

--

The Google Analytics platform is one of the most popular web analytics platforms, allowing automatic collection of data about users accessing your site.

They offer an impressive number of metrics (page views, country of origin, average time on page etc.), and there are myriad of different tools and graphing options you can use to make sense of this data through their own dashboard. It’s something we often use at Pixie Labs together with our clients to give us insight into different elements of user behaviour.

But what about if you want to use the same data in your own application? Take, for example, a dashboard that allows bloggers to view stats about their own stories on some popular online publishing platform…

This blog post will run through how you might achieve that — using data pulled from Google Analytics via the Google Analytics Reporting API V4 and integrated into a Ruby on Rails application.

Note: There are no official guides for Ruby in Google’s own developer documentation, so we’re going to roughly follow the same steps as outlined in the Python setup.

Prerequisites

Set up a service account.

Follow the instructions to set up a service account for your project. Once set up, you’ll be able to download a private key. Keep this safe!

Add the service account to your Google Analytics account

Your service account will have its own email address — you can find this in the main dashboard. Log in to your Google Analytics account and follow these instructions to add permissions for that service account to access the Google Analytics property you want to access from your application.

Install the client library.

Install the Google API Ruby client by adding the following to your application’s Gemfile:

gem 'google-api-client', '~> 0.11', require: ‘google/apis/analyticsreporting_v4’

Note that I’m requiring the specific `analyticsreporting_v4` module.

Install Google Auth

Install the Google Auth Library for Ruby. This gives us a simple way to get authorization credentials to use when calling the Google API.

gem install googleauth

Steps

1. Craft your query

To give yourself an idea of how you should structure your query, have a play around with Google’s online Request Composer. Here you can run queries against your data by providing your viewId (log in to Google Analytics and go to Admin > View Settings > Basic Settings to find this) and selecting different metrics and dimensions.

You can even pass in specific filter expressions to target specific fields in your dataset. For example, passing in ga:country==United Kingdom will restrict results to just UK page views.

Check out this useful filter reference for a full list of expressions available.

2. Initialize and authorize a Reporting Service

To make calls to the Reporting API, we need to authorize a new service with the credentials we were provided with when we set up our service account.

Drop your credentials JSON into your project (service_account_cred.json) and add the following in an initializer:

3. Build and make the API request

There isn’t really any documentation about using the Reporting API other than the code itself, so creating the request and contents involves some digging around.

As an example, let’s create a report request that returns the number of UK users accessing the site and from what browser.

Here we build up the separate parts of our request and create it using the ReportRequest class. Take a look at the code on GitHub to see the full list of attributes aReportRequest instance can be created with — there are quite a few!

You can then pass as many ReportRequest objects in to initialize an instance of GetReportsRequest, which then, in turn, passed into get_batch_reports, making the call to the Reporting API client.

And that’s it!

You can now iterate over response.data.rows to access and format the data however you need.

.

.

.

Pixie Labs is a leading London-based digital product studio with deep software development expertise in Ruby on Rails, React and React Native. We design and build high-impact apps, platforms and digital tools. Let us help you make your digital product a success.

--

--