Skip to content

Instantly share code, notes, and snippets.

@xseignard
Created January 19, 2015 18:59
Show Gist options
  • Save xseignard/e30630d652d33b9f6c85 to your computer and use it in GitHub Desktop.
Save xseignard/e30630d652d33b9f6c85 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 = 'XXXXXXXXXX@developer.gserviceaccount.com';
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