Browse Source

Exclude requests to admin site when tracking via PiwikTrackingMiddleware by default

tags/0.1.8
Johann Schmitz 3 years ago
parent
commit
8c5ed94818
Signed by: ercpe <johann@j-schmitz.net> GPG Key ID: A084064277C501ED
2 changed files with 13 additions and 0 deletions
  1. +2
    -0
      README.md
  2. +11
    -0
      src/django_pypiwik/middleware.py

+ 2
- 0
README.md View File

@@ -15,6 +15,7 @@ If you have only one site, or all of your sites sending requests to the same piw

`django-pypiwik` installs the `Piwik configuration` model. Use this model to connect your Django site with an Piwik site.

When using the `PiwikTrackingMiddleware`, the `PIWIK_TRACKING_MIDDLEWARE_EXCLUDE_ADMIN` option controls whether requests in the Django admin site should be tracked or not.

# Usage

@@ -79,6 +80,7 @@ You can also use the `track_page_view` decorator in your `urls.py` to wrap third
## PiwikTrackingMiddleware (since 0.1.6)

You can also use the `PiwikTrackingMiddleware` to track page views by including `django_pypiwik.middleware.PiwikTrackingMiddleware` in your `MIDDLEWARE_CLASSES`. The middleware sends a server-to-server tracking call in the `process_response` hook.
The `PIWIK_TRACKING_MIDDLEWARE_PARAMS` option (a dict) in your settings.py let you control other tracking parameters to send to Piwik.

Please note that the call is synchronous - if your tracking server is slow to response, the response time of your application will suffer.



+ 11
- 0
src/django_pypiwik/middleware.py View File

@@ -1,5 +1,6 @@
# -*- coding: utf-8 -*-
from django.conf import settings
from django.core.urlresolvers import reverse, NoReverseMatch

from django_pypiwik.tracker import DjangoPiwikTracker

@@ -7,6 +8,16 @@ from django_pypiwik.tracker import DjangoPiwikTracker
class PiwikTrackingMiddleware(object):

def process_response(self, request, response):
exclude_admin = getattr(settings, 'PIWIK_TRACKING_MIDDLEWARE_EXCLUDE_ADMIN', True) == True

try:
admin_url = reverse('admin:index')
except NoReverseMatch:
admin_url = None

if exclude_admin and admin_url and request.path.startswith(admin_url):
return response

tracker = DjangoPiwikTracker.for_current_site(request=request)
tracker.track_page_view(**getattr(settings, 'PIWIK_TRACKING_MIDDLEWARE_PARAMS', {}))
return response

Loading…
Cancel
Save