django-googlytics

Add google analytics snippet to your django templates.


Keywords
django, google, analytics
License
MirOS
Install
pip install django-googlytics==0.2.1

Documentation

Django-Googlytics

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.

Install

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'

Usage

Use the template tag:

{{ googlytics_code }}

to inject the goole analytics snippet wherever you want:

Settings

GOOGLE_ANALYTICS_KEY

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.

GOOGLE_ANALYTICS_IGNORE_ADMIN

Default: False

Set it to True to prevent google analytics from tracking (logged-in) admins:

GOOGLE_ANALYTICS_IGNORE_ADMIN = True