2 from django.contrib.sites.models import Site
3 from piwik.django.models import PiwikSite
4 from django.conf import settings
6 from functools import update_wrapper
9 from random import random
11 logging.basicConfig(level=logging.WARN)
12 logger = logging.getLogger(__name__)
16 url = settings.PIWIK_URL + u"/piwik.php?"
17 url += u'&'.join([k + u"=" + str(v) for k, v in kw.items()])
23 def piwik_track(klass):
24 current_site = Site.objects.get_current()
25 piwik_site = PiwikSite.objects.filter(site=current_site.id)
27 if len(piwik_site) == 0:
28 logger.warn("No PiwikSite is configured for Site " + current_site.name)
31 id_piwik = piwik_site[0].id_site
32 call_func = klass.__call__
33 host = urlparse.urlsplit(settings.PIWIK_URL).netloc
35 def wrap(self, request, *args, **kw):
36 conn = httplib.HTTPConnection(host)
37 conn.request('GET', piwik_url(
39 apiv=PIWIK_API_VERSION,
40 rand=int(random() * 0x10000),
41 cip=request.META['REMOTE_ADDR'],
42 url='http://' + request.META['HTTP_HOST'] + request.path,
43 urlref=request.META['HTTP_REFERER'] if 'HTTP_REFERER' in request.META else '',
47 return call_func(self, request, *args, **kw)
49 update_wrapper(wrap, call_func)