/django-google-analytics

Django Google Analytics app allowing for server side/non-js tracking.

Primary LanguagePythonOtherNOASSERTION

Django Google Analytics

Django Google Analytics brings the power of server side/non-js Google Analytics to your Django projects

  1. Install or add django-google-analytics to your Python path.

  2. Add google_analytics to you INSTALLED_APPS setting.

  3. Add URL include to your project's urls.py file:

    (r'^djga/', include('google_analytics.urls')),
    
  4. Specify a Google Analytics tracking code, i.e.:

    GOOGLE_ANALYTICS = {
        'google_analytics_id': 'xxx',
    }
    

    where xxx is your tracking code.

  5. If you intend tracking through middleware and Celery remember to install Celery and run its worker process.

There are two ways to add tracking to your pages.

Using <img/> and sticking it in your base.html:

{% load google_analytics_tags %}
<div style="display:none">
    <img src="{% google_analytics %}" width="0" height="0" />
</div>

Using Django's middleware, you can process every request and use Celery to make the request to Google Analytics:

MIDDLEWARE_CLASSES = (
    'google_analytics.middleware.GoogleAnalyticsMiddleware',
)

You have to add google_analytics to your CELERY_IMPORTS:

CELERY_IMPORTS = ('google_analytics.tasks')

You can also specify paths that will be excluded when tracking:

GOOGLE_ANALYTICS_IGNORE_PATH = ['/health/', ]