Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Piwik cvs exporter
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import sys
import csv
from optparse import OptionParser
import urllib
import httplib
import urlparse
import simplejson
except ImportError:
print "You must install the python-simplejson to use this script."
print "On Debian/Ubuntu, you can install it with this command:"
print "sudo apt-get install python-simplejson"
HEADERS = ['entry_sum_visit_length',
class PiwikAPI:
def __init__(self, url, token_auth):
self.url = url
self.token_auth = token_auth
(scheme, netloc, path, query, fragment) = urlparse.urlsplit(self.url) = netloc
def call(self, method, params = {}, format = 'json'):
args = {'module' : 'API',
'method' : method,
'format' : format,
'token_auth' : self.token_auth}
conn = httplib.HTTPConnection(
conn.request('GET', u"%s?%s" % (self.url, urllib.urlencode(args)), headers={'User-Agent' : 'Django Piwik'})
result = conn.getresponse()
data = None
if result.status == 200:
data =
if data is not None and format == 'json':
return simplejson.loads(data)
return data
def getAllSites(self):
def getSiteFromId(self, id):
return'SitesManager.getSiteFromId', params = {'idSite' : id})
def getJavascriptTag(self, id, piwikUrl = '', actionName = ''):
result ='SitesManager.getJavascriptTag', params = {'idSite' : id})
if result:
return result['value']
return None
def main():
parser = OptionParser()
parser.add_option("-u", "--url", dest="url",
help="Piwik url")
parser.add_option("-k", "--key", dest="key",
help="Piwik key")
parser.add_option("-p", "--period", dest="period",
help="Period of the export (year, month, day)")
parser.add_option("-d", "--date", dest="date",
help="The date to export in the format YYYY-MM-DD")
parser.add_option("-o", "--output", dest="filename",
help="write report to FILE", metavar="FILE")
(opt, args) = parser.parse_args()
if not opt.url or not opt.key or not opt.filename:
print "incorrect number of arguments"
return sys.exit(1)
if opt.filename == '-':
csv_file = sys.stdout
csv_file = open(opt.filename, 'w')
piwik = PiwikAPI(opt.url, opt.key)
result ='Actions.getPageUrls',
params = {'idSite': 1,
'period' : opt.period,
dw = csv.DictWriter(csv_file,
# Stay compatbile with python 2.6
except AttributeError:
headers = []
for h in HEADERS:
for r in result:
if __name__ == "__main__":

This comment has been minimized.

Copy link
Owner Author

avoine commented Dec 8, 2011


This script use a the API Key to dump the statistics for a piwik site in a csv formated file.

python -u -k123456789abcdefghijkl -o my_file.csv -p day -d 2011-01-30

-h, --help show this help message and exit
-u URL, --url=URL Piwik url
-k KEY, --key=KEY Piwik key
-p PERIOD, --period=PERIOD Period of the export (year, month, day)
-d DATE, --date=DATE The date to export in the format YYYY-MM-DD
-o FILE, --output=FILE write report to FILE

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.