Skip to content

Instantly share code, notes, and snippets.

@mistymagich
Created May 27, 2015 08:45
Show Gist options
  • Save mistymagich/89342f84796cc09e2d8d to your computer and use it in GitHub Desktop.
Save mistymagich/89342f84796cc09e2d8d to your computer and use it in GitHub Desktop.
SERVICE_ACCOUNT_EMAIL = '<service_account_email>'
SERVICE_ACCOUNT_KEY_FILE = __dirname + '/googleapi-privatekey.pem'
google = require 'googleapis'
moment = require 'moment'
year=2015
month=3 # 月は0始まり (3=4月)
m = moment {'year': year, 'month': month}
jwt = new google.auth.JWT SERVICE_ACCOUNT_EMAIL,
SERVICE_ACCOUNT_KEY_FILE,
null,
['https://www.googleapis.com/auth/analytics.readonly']
jwt.authorize (err, result) ->
if err
console.error err
process.exit()
analytics = google.analytics 'v3'
analytics.data.ga.get {
'ids': 'ga:<ViewID>',
'start-date': m.format('YYYY-MM-DD'),
'end-date': m.endOf('month').format('YYYY-MM-DD'),
'metrics': 'ga:visitors',
'auth': jwt
}, (err, resp) ->
if err
console.error err
process.exit()
console.log "Visitors: " + resp.totalsForAllResults['ga:visitors']
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment