I just whipped together another really simple Django application I call django-google-analytics that simplifies the process of inserting Google Analytics tags into your Django projects.
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)
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 <body>
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.
google_analytics
application to your INSTALLED_APPS
section of your settings.py
. This mode requires that you be using the Django sites framework too, so make sure you have that set up as well../manage.py syncdb
to add the database tables/admin/
) and click into a site objectsUA-xxxxxx-x
and save the site.base.html
) insert this tag at the very top: {% load analytics %}
{% analytics %}
google_analytics
application to your INSTALLED_APPS
section of your settings.py
.base.html
, insert this tag at the very top: {% load analytics %}
{% analytics "UA-xxxxxx-x" %}
the UA-xxxxxx-x
is a unique Google Analytics code for you domain when you sign up for a new account.I’m not sure how to avoid this, but if you only want to use the second method and you do a syncdb
you’re going to get the model added to your database whether you want it or not.
My best advice at the moment would be to ignore that (smile), but if anyone knows how I can make the database portion an optional install, that’d be much appreciated advice!