Fetching Google Analytics data using Node.js (without OAuth authentication)

Hello,
I'm Mandai, the Wild Team member of the development team.

In addition to my regular work, I also (occasionally) work for the web marketing team, which is made up of people from across the company, and my main job is to look at Google Analytics data and say, "Is it going up? Is it going down?"

I'm using Node.js to retrieve data from Google Analytics via the API and analyze it in a similar way, but even creating a basic program is a lot of work, so I'd like to share a sample program with you

I haven't written the main text yet, but I feel like I'll be devoting more space to settings related to Google accounts than to the program


 

Google account settings

 

Create a project

Create a project at
https://console.developers.google.com , or if you have an existing project and are happy with that, you can use that.

 

Enable Google APIs

Enable the Google Analytics Reporting API from the "Library" in the "API Manager."
However, there is no link on this page, so you will need to search for it by entering "analytics reporting" or similar in the search box.

 

Create a service account key from your credentials

Once you have enabled the API, create credentials by clicking the "Credentials" link on the left

The type of credential you will create is a "service account key."

Give the service account a suitable name and set the role to Project Viewer.
(Actually, I'm not confident about these settings, so if you have any better role settings, please let me know!)

The "Key Type" will be created in JSON.
You will download a JSON file filled with authentication information, so keep it safe.
You will use this later.

 

Register the service account key you created on the Google Analytics side

I thought I was all set after creating a service account with access to a project with the Google Analytics Reporting API enabled, but there was one more step I needed to configure

https://analytics.google.com page, you need to go to the admin screen and register the "service account key" in the user management for the view you want to retrieve data from.

Here's a question

Is it necessary to register a service account key even if the user who created it has access to the view?

The answer is of course yes

I think the Google account permission structure is broken, but at least from Google Analytics, it seems that the service account key is recognized as belonging to a different user than the user who created it

Therefore, we will register the service account key again with the permission "View and Analyze"

You will need an email address to register, but the JSON file for the service account key you downloaded earlier contains a key called "client_email", so that value will be your email address

Since you've already opened the Google Analytics screen, take a moment to note down the ID of the view you want to retrieve data from

This completes the Google Analytics setup

 

Node.js configuration

Install the package called googleapis from npm.
As the name suggests, this is a package for accessing Google APIs made by Google.

npm install googleapis

 

If you just want to communicate with the Google Analytics Reporting API, no other packages are required

 

Get the data

It's not a sauce that's worth exaggerating, so please take a look at it first

var google = require('googleapis'); var analytics = google.analyticsreporting('v4'); // Specify the JSON file of the service account key var credential = require('./xxxxxxxx.json'); // Specify the Google Analytics view ID you want to retrieve data from var viewId = 'XXXXXXXXX'; // Target data period var startDate = "xxxx-xx-xx"; var endDate = "xxxx-xx-xx"; var jwtClient = new google.auth.JWT(credential.client_email, null, credential.private_key, ["https://www.googleapis.com/auth/analytics.readonly"], null); jwtClient.authorize((error, tokens) => { if (error){ console.log(error); return; } analytics.reports.batchGet({ resource: { "reportRequests": [ { "dateRanges": [ { "startDate": startDate, "endDate": endDate } ], "viewId": viewId, "dimensions": [ { "name": "ga:pagePath" } ] } ] }, auth: jwtClient }, (error, response) => { if (error){ console.log(error); } console.log(response); }) });

 

There are four changes required for testing:

  • Path to the service account key JSON file
  • View ID
  • Start date of the target data period
  • End date of the target data period

By simply changing the above, the number of accesses for the target period will be aggregated for each URL and data will be obtained

As I expected, the source isn't that great, but they did spend a lot of space on the settings related to Google accounts

 
That's all

If you found this article useful, please click [Like]!
1
Loading...
1 vote, average: 1.00 / 11
1,667
X Facebook Hatena Bookmark pocket

The person who wrote this article

About the author

Yoichi Bandai

My main job is developing web APIs for social games, but I'm also grateful to be able to do a variety of other work, including marketing.
My portrait rights within Beyond are CC0.