Skip to content

Instantly share code, notes, and snippets.

@mikeyhu
Created November 13, 2013 13:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mikeyhu/7449512 to your computer and use it in GitHub Desktop.
Save mikeyhu/7449512 to your computer and use it in GitHub Desktop.
Dashing-js Job to receive Google Analytics API data using CoffeeScript
googleapis = require 'googleapis'
moment = require 'moment'
email = 'MYACCOUNT@developer.gserviceaccount.com'
scope = ['https://www.google.com/analytics/feeds/']
keyFile = 'MYKEY.pem'
formatRowsByDay = (rows)->
count = -16
({x:count++,y:parseInt(row[1])} for row in rows)
getMetrics = ()->
googleapis.discover('analytics','v3').execute (err,client)->
jwt = new googleapis.auth.JWT(email,keyFile,scope)
jwt.authorize (err,result)->
console.log "#{moment().format()} : getMetrics-authorise : #{err}" if err
client.analytics.data.ga.get({
'ids': 'ga:MY_GA_ID'
'start-date': moment().subtract('days',16).format("YYYY-MM-DD")
'end-date': moment().subtract('days',2).format("YYYY-MM-DD")
'metrics': 'ga:visits'
'dimensions' : 'ga:date'
}).withAuthClient(jwt).execute (err,results)->
console.log "#{moment().format()} : getMetrics-execute : #{err}" if err
points = formatRowsByDay(results.rows)
send_event('visitsByDay',{points:points})
setInterval(getMetrics, 60 * 1000)
getMetrics()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment