Skip to content

Instantly share code, notes, and snippets.

@orta
Last active April 13, 2016 00:35
Show Gist options
  • Save orta/4945269 to your computer and use it in GitHub Desktop.
Save orta/4945269 to your computer and use it in GitHub Desktop.
Get Google Analytics Stats from Hubot
# Artsy Editorial
#
# Get Page stats from google analytics
GA = require('googleanalytics')
util = require('util')
require('date-utils');
module.exports = (robot) ->
robot.hear /stats (.*)http:\/\/artsy.net\/(.*)/i, (msg) ->
url = msg.match[0]
config = { "user": "[email]", "password": "[password]" }
ga = new GA.GA(config);
ga.login (err, token) ->
start_date = Date.yesterday().toYMD("-")
end_date = Date.today().toYMD("-")
# support custom ranges
if url.indexOf("stats http") == -1
range = url.split(" http://artsy.net/")[0].split("stats ")[-1..][0].trim()
if range is "week"
start_date = Date.today().add({ weeks: -1 }).toYMD("-")
if range is "month"
start_date = Date.today().add({ months: -1 }).toYMD("-")
if range is "year"
start_date = Date.today().add({ years: -1 }).toYMD("-")
path = url.split("http://artsy.net")[-1..]
options = {
'ids': 'ga:[analytics profile]',
'start-date': start_date,
'end-date': end_date,
'filters': "ga:pagePath==#{path}",
'metrics': 'ga:visits, ga:pageviews',
}
# [{"metrics":[{"ga:visits":478,"ga:pageviews":688}],"dimensions":[{}]}]
ga.get(options, (err, entries) ->
visits = entries[0]["metrics"][0]["ga:visits"]
pageviews = entries[0]["metrics"][0]["ga:pageviews"]
msg.send "/#{path} recieved #{visits} visits and #{pageviews} pageviews."
)
@Godoy
Copy link

Godoy commented Apr 13, 2016

Good! I'm writing a hubot script plugin for analytics: https://github.com/PlanBCom/hubot-analytics

Any help will be appreciated!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment