This blog post explains how to use the Analytics Core Reporting API by Google from Node.js.
Let’s use that API to create a Node.js script analytics.js
that downloads the top 10 most visited pages of your website.
You must first make sure that you can access the Google Analytics API from the script and that the appropriate library for doing so is installed.
Go to the Google Developers Console:
myproject
).myproject-3126e4caac6a.json
”).node_modules
that is inside one of the parent directories of the script that we’ll create later. That means that you can keep it out of the repository with analytics.js
. For example, the following path is perfectly fine:$HOME/node_modules/myproject-3126e4caac6a.json
Go to the Admin panel in Google Analytics:
npm install --save googleapis
The first line of the script enables us to execute the script directly on Unix systems (if it is executable):
#!/usr/bin/env babel-node
The code shown in this section is ECMAScript 6, which is why we run the script via babel-node. You can also omit this first line and run the script like this:
babel-node analytics.js
Next, we import the Google API client library.
import google from 'googleapis';
Next, we import the key and define the view ID (prefixed with 'ga:'
):
import key from 'myproject-3126e4caac6a.json';
const VIEW_ID = 'ga:97675673';
Next, we authenticate (consult “Authorizing and Authenticating” for more information):
let jwtClient = new google.auth.JWT(
key.client_email, null, key.private_key,
['https://www.googleapis.com/auth/analytics.readonly'], null);
jwtClient.authorize(function (err, tokens) {
if (err) {
console.log(err);
return;
}
let analytics = google.analytics('v3');
queryData(analytics);
});
Finally, we make the query (for consistency’s sake, all keys are quoted):
function queryData(analytics) {
analytics.data.ga.get({
'auth': jwtClient,
'ids': VIEW_ID,
'metrics': 'ga:uniquePageviews',
'dimensions': 'ga:pagePath',
'start-date': '30daysAgo',
'end-date': 'yesterday',
'sort': '-ga:uniquePageviews',
'max-results': 10,
'filters': 'ga:pagePath=~/ch_[-a-z0-9]+[.]html$',
}, function (err, response) {
if (err) {
console.log(err);
return;
}
console.log(JSON.stringify(response, null, 4));
});
}
We retrieve the top 10 most visited pages within the last 30 days:
metrics
: what was measured?dimensions
: what meta-data are we interested in?'2005-01-01'
.sort
: comma-separated keys of either metrics or dimensions.filter
: we filter the ga:pagePath
via a regular expression, so that only files with specific names are shown in the results. The property value is an ES6 tagged template, which is why we can use the backslash directly, without escaping it.More information on the Analytics Core Reporting API:
analytics.data.ga.get()
)