In the Google API console, you need to activate the Google Analytics API and finally configure the service account, then you upload the *.p12 file.
From this *.pem file, you need to convert it to a *.pem file, to do this, do the following:
openssl pkcs12 -in XXXXX.p12 -nocerts -nodes -out XXXXX.pem
You will be given a password, it must be notasecret
Now you have the *.pem file that you need, and the email account is the one that appears in the google api console as EMAIL ADDRESS .
Remember to add this address to your analytics account (see: Google Analytics Google Analytics 403: “User does not have a Google Analytics account” )
You should be ready to go with the following snippet:
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); }); });
source share