django-tracking

March 3, 2010 by · Leave a Comment 

I just finished intalling django-tracking in one of my clients projects in webfaction. Here are my steps.

PYTHONPATH=$HOME/webapps/my_django_app/lib/python2.5 easy_install-2.5 -s $HOME/webapps/my_django_app/bin -d $HOME/webapps/my_django_app/lib/python2.5 django-tracking

This installed django-traking under the my project python path,
you could install it globally with:

easy_install-2.5 -s $HOME/bin -d $HOME/lib/python2.5 django-tracking

The rest is quick, just follow the instructions as in django-tracking.
1) Add tracking to your list of INSTALLED_APPS in settings.py:

INSTALLED_APPS = (
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',

'tracking',
)

2) django-tracking Options:
Visitor Tracking
Add tracking.middleware.VisitorTrackingMiddleware to your MIDDLEWARE_CLASSES in settings.py. It must be underneath the AuthenticationMiddleware, so that request.user exists.

Automatic Visitor Clean-Up
If you want to have Django automatically clean past visitor information out your database, put tracking.middleware.VisitorCleanUpMiddlewarein your MIDDLEWARE_CLASSES.

IP Banning
Add tracking.middleware.BannedIPMiddleware to your MIDDLEWARE_CLASSES in settings.py. I would recommend making this the very first item in MIDDLEWARE_CLASSES so your banned users do not have to drill through any other middleware before Django realizes they don’t belong on your site.

Visitors on Page (template tag)
Make sure that django.core.context_processors.request is somewhere in your TEMPLATE_CONTEXT_PROCESSORS tuple. This context processor makes the request object accessible to your templates. This application uses the request object to determine what page the user is looking at in a template tag.

Active Visitors Map

If you’re interested in seeing where your visitors are at a given point in time, you might enjoy the active visitor map feature. Be sure you have added a line to your main URLconf, as follows:

#you must add this even if you are not using Active Visitors Map other wise you might get and error.
from django.conf.urls.defaults import *

urlpatterns = patterns('',
   (r'^tracking/', include('tracking.urls')),  
)

view all available django-tracking settings

Add the following to admin.py to view the data collected in django admin.

from tracking.models import Visitor

class VisitorAdmin(admin.ModelAdmin):
    list_display = ('ip_address','referrer','url','page_views','last_update',)
    ordering = ('last_update',)

admin.site.register(Visitor, VisitorAdmin)

About Creart

Speak Your Mind

Tell us what you're thinking...
and oh, if you want a pic to show with your comment, go get a gravatar!

*