Skip to content

Instantly share code, notes, and snippets.

@stefangrund
Last active August 29, 2015 14:07
Show Gist options
  • Save stefangrund/32278ad2325c90a22a99 to your computer and use it in GitHub Desktop.
Save stefangrund/32278ad2325c90a22a99 to your computer and use it in GitHub Desktop.
Gaug.es CSV Report: Create an ongoing CSV file with people/views from your Gaug.es Analytics. Works perfectly with Panic's Status Board

Gauges CSV Report

Create an ongoing CSV file with people/views from your Gaug.es Analytics. Works perfectly with Panic's Status Board.

Installation

  1. Copy gauges-csv.py to your web server or Dropbox.
  2. Fill out the script's settings with your website's name, your Gaug.es API key, etc.
  3. Run the script daily (with a cron job, Lingon or whatever) - and you're done.

Use with Status Board

If you want to use the generated CSV file in your Status Board, just add a new graph panel and enter the file's URL. If you placed it in your Dropbox you'll have to generate a link for it (right-click on the file and choose Dropbox ➜ Share Link).

#!/usr/bin/python
from os import path, access, R_OK
import json
import urllib2
import datetime
gauge = {
'id' : 'YOURID', # The ID can be found in your Gauge's Dashboard URL, e.g. https://secure.gaug.es/dashboard#/gauges/685d4566673fefh4ab7ff76d/overview -> ID = 685d4566673fefh4ab7ff76d
'title' : 'My Website', # Title of your website, don't use any commas!
'site' : 'my-website' # Abbreviation for the file name
}
api = {
'url' : 'https://secure.gaug.es/gauges/',
'header' : 'X-Gauges-Token',
'token' : 'YOURTOKEN' # Get your API key here: https://secure.gaug.es/dashboard#/account/clients
}
report = {
'path' : '/path/to/your/directory/', # Directory in which the CSV file will be created
'file' : 'report-' + gauge['site'] + '.csv',
}
FILE = report['path'] + report['file']
HEADER = gauge['title'] + ",Views,People\n"
def getJSON(url, header, token):
req = urllib2.Request(url)
req.add_header(header, token)
opener = urllib2.build_opener()
f = opener.open(req)
return json.load(f)
def appendViews():
# Get date
today = datetime.date.today()
yesterday = today - datetime.timedelta(1)
checkDate = yesterday.strftime("%Y-%m-%d")
# Get traffic
trafficURL = api['url'] + gauge['id'] + '/traffic?date=' + checkDate
j = getJSON(trafficURL, api['header'], api['token'])
date = j['date']
latest = len(j['traffic'])-2
today = j['traffic'][latest]
views = today['views']
people = today['people']
# Write to file
f.write(date + ',' + str(views) + ',' + str(people) + '\n')
# File exists and is readable
if path.isfile(FILE) and access(FILE, R_OK):
# Read first line
f = open(FILE, 'r')
firstLine = f.readline()
f.closed
# HEADER matches -> append views
if firstLine == HEADER:
f = open(FILE, 'a')
appendViews()
f.closed
# HEADER doesn't match -> overwrite file
else:
f = open(FILE, 'w') # change 'w' to 'a' if you don't want to override the file, but to append the correct HEADER and views
f.write(HEADER)
appendViews()
f.closed
# File doesn't exist
else:
f = open(FILE, 'w')
f.write(HEADER)
appendViews()
f.closed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment