Skip to main content

Simple Google Analytics integration for Django projects

Project description

django-ga-tracking is a simple Django pluggable app that provides a context processor and template for adding Google Analytics tracking code to your pages.

Alternatives and differences

There are alternatives like django-google-analytics and Django Analytics Middleware, but this project begun as both solutions seem to introduce unnecessary complexity and/or lack the ability to configure analytics via Django settings module. The latter factor was particularly important for this project because hard-coding configuration or storing configuration in a databaes column was something we absolutely wanted to avoid.

Installation

Install using pip:

pip install django-ga-tracking

Basic usage

Add ga_tracking to INSTALLED_APPS.

Next, add the ga_tracking_id context processor to TEMPLATE_CONTEXT_PROCESSORS:

TEMPLATE_CONTEXT_PROCESSORS = (
    ....
    'ga_tracking.context_processors.ga_tracking_id',
)

Define a GA_TRACKING_ID setting and assign your tracking ID to it. For example:

GA_TRACKING_ID = 'UA-12345678-9'

Finally, add the tracker code template somewhere in your own template:

{% include "ga_tracking/ga.html" %}

Customization

Because django-ga-tracking uses a simple template to add the code, you can customize it simply by overriding the template with your own. The ga.html template contains the default <script> block as provided by Google.

The script block is not rendered at all if the GA_TRACKING_ID setting is not present, so you can, for example, set the setting only in production to avoid development sites from being tracked, or use different tracking codes on different deployments. For example:

# in settings.py
import os

GA_TRACKING_ID = os.environ.get('GA_TRACKING_ID')

Reporting bugs

Please report bugs to Bitbucket issue tracker.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

django-ga-tracking-0.0.1.zip (5.9 kB view details)

Uploaded Source

django-ga-tracking-0.0.1.tar.gz (3.5 kB view details)

Uploaded Source

File details

Details for the file django-ga-tracking-0.0.1.zip.

File metadata

File hashes

Hashes for django-ga-tracking-0.0.1.zip
Algorithm Hash digest
SHA256 48014843dbc64aa59d5ad75fa09eb4e9820bbde58e5fb3e214aaf0b38e5f4166
MD5 c82fc0a0b9166cb4b56596609fdb0125
BLAKE2b-256 ad4753aa1885d219661c7ca2777294076f30ff3727dad410a0c19b1146acbe0c

See more details on using hashes here.

File details

Details for the file django-ga-tracking-0.0.1.tar.gz.

File metadata

File hashes

Hashes for django-ga-tracking-0.0.1.tar.gz
Algorithm Hash digest
SHA256 fc51e9990aad9aa7b819793fe0b4e284f93195a2104565eb3fe04d982b424314
MD5 5e318721111a06bb2a0717d48dd0a6e4
BLAKE2b-256 e0ef2ce9c743d1493a67875c5db12920133e8b6e4456d32432ffbccef967c204

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page