Google + Analytics = Googlytics
An application to use the google analytics tracking snippet in a Django project.
There are a couple of other apps like this one, but I wasn't fully satisfied so I've implemented googlytics on top of the ideas expressed in this StackOverflow Q&A.
Features:
- Ability to turn off googlytics in development.
- Ability to turn off googlytics for (logged-in) admin users.
- Tests.
- Simplicity.
Add the app to the INSTALLED_APPS
:
INSTALLED_APPS = (
...
'googlytics',
...
)
Add its context processor to TEMPLATE_CONTEXT_PROCESSORS
:
TEMPLATE_CONTEXT_PROCESSORS = (
...
'googlytics.context_processors.googlytics',
...
)
Then in your development settings add:
GOOGLE_ANALYTICS_KEY = ''
And in your production settings add the real google analytics key:
GOOGLE_ANALYTICS_KEY = 'UA-XXX-X'
Use the template tag:
{{ googlytics_code }}
to inject the goole analytics snippet wherever you want:
A string with the google analytics key:
GOOGLE_ANALYTICS_KEY = 'UA-XXX-X'
It can also be set to an empty string (''
) to avoid displaying the tracking snippet.
Default: False
Set it to True
to prevent google analytics from tracking (logged-in) admins:
GOOGLE_ANALYTICS_IGNORE_ADMIN = True