Created
July 23, 2013 13:58
-
-
Save apiarian/6062547 to your computer and use it in GitHub Desktop.
A Django based Piwik -> StatusBoard bridge
This file contains hidden or 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
# views.py | |
# Aleksandr Pasechnik | |
# | |
# A Django views.py function which bridges Piwik and StatusBoard | |
# | |
# Django: https://www.djangoproject.com | |
# Piwik: http://piwik.org | |
# StatusBoard: http://panic.com/statusboard/ | |
# PiwikAPI: https://github.com/piwik/piwik-python-api | |
# | |
# This function creates a line graph with the last `days` days of visit counts | |
# The function plots both of the sites in my Piwik system | |
from django.http import HttpResponse | |
import json | |
from piwikapi.analytics import PiwikAnalytics | |
def recent_visits_all(request,days='10'): | |
# Note that the datasequences are plotted in array order | |
# Put the sequences you want on top last | |
ret = {'graph' : { | |
'title' : 'recent visits', | |
'type' : 'line', | |
'datasequences': [ | |
{ 'title' : 'test' , | |
'color' : 'lightGray', | |
'datapoints': [], | |
}, | |
{ 'title' : 'live', | |
'color' : 'red', | |
'datapoints': [], | |
}, | |
] | |
} | |
} | |
pa = PiwikAnalytics() | |
pa.set_api_url('YOUR_PIWIK_URL') | |
pa.set_id_site('2') | |
pa.set_format('json') | |
pa.set_period('day') | |
pa.set_date('last'+days) | |
pa.set_method('VisitsSummary.getVisits') | |
pa.set_parameter('token_auth','YOUR_TOKEN') | |
# get the visits summary for site 2 | |
visits = json.loads(pa.send_request()) | |
for (i,k) in enumerate(sorted(visits.keys())): | |
ret['graph']['datasequences'][1]['datapoints'].append( | |
{'title':'%d'%(i-len(visits)+1),'value':visits[k]} | |
) | |
# switch to site 1 | |
pa.set_id_site('1') | |
# get the visits summary for site 1 | |
visits = json.loads(pa.send_request()) | |
for (i,k) in enumerate(sorted(visits.keys())): | |
ret['graph']['datasequences'][0]['datapoints'].append( | |
{'title':'%d'%(i-len(visits)+1),'value':visits[k]} | |
) | |
return HttpResponse(json.dumps(ret)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment