-
-
Save NoobsArePeople2/e2d819ca439ebc0b4ef1 to your computer and use it in GitHub Desktop.
Node.js + googleapis to query google analytics
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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