Skip to content

Instantly share code, notes, and snippets.

@NoobsArePeople2
Forked from xseignard/googleAnalytics.js
Created November 17, 2015 20:28
Show Gist options
  • Save NoobsArePeople2/e2d819ca439ebc0b4ef1 to your computer and use it in GitHub Desktop.
Save NoobsArePeople2/e2d819ca439ebc0b4ef1 to your computer and use it in GitHub Desktop.
Node.js + googleapis to query google analytics
var googleapis = require('googleapis'),
JWT = googleapis.auth.JWT,
analytics = googleapis.analytics('v3');
var SERVICE_ACCOUNT_EMAIL = '[email protected]';
var SERVICE_ACCOUNT_KEY_FILE = __dirname + '/key.pem';
var authClient = new JWT(
SERVICE_ACCOUNT_EMAIL,
SERVICE_ACCOUNT_KEY_FILE,
null,
['https://www.googleapis.com/auth/analytics.readonly']
);
authClient.authorize(function(err, tokens) {
if (err) {
console.log(err);
return;
}
analytics.data.ga.get({
auth: authClient,
'ids': 'ga:XXXXXXXX',
'start-date': '2015-01-19',
'end-date': '2015-01-19',
'metrics': 'ga:visits'
}, function(err, result) {
console.log(err);
console.log(result);
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment