Sidebar Network

django-google-analytics A simple Django application that eases Google Analytics integration with your projects.

The Basics

I manage a lot of Django projects that present slightly-different forms to users depending on the site/domain they’re visiting. There’s also a bunch of custom submission code that differs from form to form, but that’s neither here nor there.

I need different Google Analytics codes depending on the sites and after sticking these tags into every single template, I thought it would be cool to be able to manage these Google analytics accounts from the Django admin page. I also added a mode of operation that excludes the admin interface altogether (you can just use the template tag)

Aren’t there other solutions already?

In my searches I ran across two methods that have been around for a while but which I didn’t find to be adequate. The first is a Google Analytics middleware by Will Larson. This method inspects each request, searches it for a tag and dynamically insert the google analytics code.

The bad thing is that the template for the tags are hard coded into the middleware, and as far as I know, this kind of defeats the purpose of caching, as a regular expression and some other code must be called for every request. You’ve also got to purposefully exclude the admin interface, which most people are never going to want to track anyway.

Finally, you can’t tie GA codes to sites (you can specify them in different settings files, though) and have them inserted properly. I like the ability to administer as much as possible from the admin (when ideal).

The second method I found was a snippet that defined a simple template tag which replicates the second mode of operation you see below. It’s a little raw, but it works. The main downside with this is that, again, you cannot edit the codes from the admin interface and you can’t tie them to specific sites.

Project Homehttp://code.google.com/p/django-google-analytics/
Grab the Repository – svn checkout http://django-google-analytics.googlecode.com/svn/trunk/
 

Recent Commits

  • 6 was committed 4 months ago.
    * Removing a print statement and fixing a small bug if you haven’t set any site codes yet
  • 5 was committed 4 months, 3 weeks ago.
    Fixing small errors regarding the optional model
  • 4 was committed 4 months, 3 weeks ago.
    Adding the LICENSE, USAGE and making the model optional
  • 3 was committed 4 months, 3 weeks ago.
    If the code is left blank, nothing should be rendered
  • 2 was committed 4 months, 3 weeks ago.
    Initial commit of the project.
  • 1 was committed 4 months, 3 weeks ago.
    Initial directory structure.