-
-
Save buley/5957023 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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." | |
) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment