Google Analytic V4 API with Rails 5

Yazed Jamal
Isian
Published in
4 min readFeb 27, 2018

--

Pulling data from an API is something that I have not done in a while. In a recent app I was making for a client requires the app to pull data from Google Analytic, populate it and finally display it on a leaderboard. Google have not provide a very detail guide yet, but they have some infos for anyone to get started.

Base on their documentations, I need to use Oauth 2 for my authentication to get an access token in order to fetch any data from Google Analytic API. Because I am using Rails I will not create the flow to request the token manualy, instead I will use devise for this purpose.

Next I will require a gem which is maintain by google specifically to handle APIs request, google-api-client gem. Basically, what I will do is.

  1. Request token and refresh token from google
  2. Use these tokens to get new token later
  3. Make a request to google analytic

So today I will walkthrough the process to setup these items only. I will not talk about how I populate the data for the leaderboard and I also I will not show how to setup devise because I believed there are enough example and tutorial how to do that out there.

According to google, I need to make a request to the OAuth server to get an authorization code before exchanging it with an access token. Thanks to devise I do not need to worry for any of this. I just need to configure devise to utilize Omniauth Google for user login and make sure to save the refresh and access token in the database. Before writing any code, I need to do these 3 important steps beforehand.

  1. Enable Google Analytic V4 API
  2. Obtain google credentials, Client id and Client secret
  3. Identify the scope I would like to use when requesting the API.

Request token and refresh token from google

Since, I am going to use devise to obtain the tokens, I also need to configure devise to include scope for Google Analytic, so whenever a user proceed with an authentication, google will ask permission to access their google analytic data base on the scope provided. I also believe that step 2 above about geeting credential is covered since the same credential needed when I setup devise with Omniauth for the first time.

On line 3, I would want google to ask for consent each time a user perform an authentication. I would like to do just to make sure the refresh token is provided each time a user authenticate.

On line 4, google recommend to include this in order for me to have incremental authorisation. Head to this link in order to learn more about it

On line 6, this is where I define the scope for accessing Google Analytic. For this case, I will set it on readonly access.

And on the final line is just to make sure rails could use a local certificate for SSL if it can’t find one on the system.

Finally don’t forget to save the tokens for use later

Use these tokens to get new token later

The tokens’ validity that google provide after the authentication will not last long. After it is expired, the token need to be replace by a new one using the refresh token. So imagine if the application need to call the api long after the user make the authentication, I need to create a method which will refresh the token each time I need to call the API.

From line 3 to 14, I prepared the body and headers to make a request for a token replacement. On line 16, I amusing HTTParty gem to make a request and finally from line 18 to 21 I will update the token if the request is a success.(The code should also handle other response for best practice).

Make a request to google analytic

Now , I am ready to make an API call

On line 3, I create an instance of the Google Analytic request service from the google-api-client gem.

From line 5, I refresh the token to make sure the token is still valid.

From line 7 to 11, I make an authorisation for the api connection by creating a client object using the updated token.

From line 13 to 22, I prepare all the details required for the API call. This items may vary depends on what information I am trying to get from the API call.

Lastly on line 24, I make the API call.

If the request is 200, I can now use the information from the response for whatever I want, in my case, I can save it to the database and update my leaderboard with the latest information.

That’s all Folk. Cheers

Notes: There are probably many ways to implement this feature, but this is what I have come out with. Anybody is free to comment on any mistakes or improvements that I can apply. This guide is initially for me to learn and remember what I have done. Nonetheless anybody is welcome to follow this guide if you find it is helpful.

--

--

Yazed Jamal
Isian
Editor for

A CODER on the outside and a Designer on the inside. An Indie game developer at free time. Also like cooking and gadgets.