Modules you will need:
- Go to the "Google API console", and under "APIs & auth" go to credentials.
- Create a service account for your project.
- Go to the settings for that account, and click "Generate new P12 Key" to download a new key.
- Convert the .p12 file to .pem:
openssl pkcs12 -in key.p12 -out key.pem -nocerts
- Remove the passphrase from the .pem file:
openssl rsa -in key.pem -out key.pem
You can now make a request to the analytics api like so:
var ga = require('googleanalytics')
, GAPI = require('gapitoken')
var gapi = new GAPI(
{ iss: 'xxxxxxxxx@developer.gserviceaccount.com'
, scope: 'https://www.googleapis.com/auth/analytics'
, keyFile: __dirname + '/ga-cert.pem'
}
, function (error) {
if (error) return callback(error)
gapi.getToken(function (error, token) {
var GA = new ga.GA({ token: token })
, options = {}
GA.get(options, callback)
})
}
)