Skip to main content

Google Analytics Django

A comprehensive Django package for seamless integration of Google Analytics into your Django projects, supporting Google Analytics 4 (GA4).

Published on Django Packages

Features

  • Support for Universal Analytics (analytics.js) and Google Analytics 4 (gtag.js)
  • Configurable options for anonymizing IP, cookie expiration, sampling rates, and more
  • Middleware for setting custom headers and server-side tracking
  • Debug mode to prevent tracking in development environments
  • Automatic exclusion of staff users from tracking
  • Easy-to-use template tags for quick integration
  • Extensive settings for fine-grained control over tracking behavior
  • Event tracking and custom dimension/metric support

Installation

  1. Install the package using pip:
pip install google-analytics-django
  1. Add 'google_analytics_django' to your INSTALLED_APPS in settings.py:
INSTALLED_APPS = [
    # ...
    'google_analytics_django',
    # ...
]
  1. Add the middleware to your MIDDLEWARE in settings.py:
MIDDLEWARE = [
    # ...
    'google_analytics_django.middleware.GoogleAnalyticsMiddleware',
    # ...
]

Configuration

Add the following settings to your Django project's settings.py file:

# Required
GOOGLE_ANALYTICS_PROPERTY_ID = 'UA-XXXXXXXX-X'  # or 'G-XXXXXXXXXX' for GA4

# Optional (shown with default values)
GOOGLE_ANALYTICS_DOMAIN = 'auto'
GOOGLE_ANALYTICS_ANONYMIZE_IP = False
GOOGLE_ANALYTICS_SAMPLE_RATE = 100
GOOGLE_ANALYTICS_SITE_SPEED_SAMPLE_RATE = 1
GOOGLE_ANALYTICS_COOKIE_EXPIRES = 63072000  # 2 years in seconds
GOOGLE_ANALYTICS_DISPLAY_FEATURES = False
GOOGLE_ANALYTICS_USE_GTAG = True  # Set to False to use analytics.js
GOOGLE_ANALYTICS_DEBUG_MODE = False

Usage

Template Tag

In your base template, load the template tags and add the Google Analytics script:

{% load google_analytics_tags %}
<head>
    <!-- ... other head elements ... -->
    {% google_analytics %}
</head>

Middleware

The middleware is automatically active once added to your MIDDLEWARE setting. It adds two custom headers to the response:

  • X-GA-TRACKING-ID: The Google Analytics client ID (if available)
  • X-CLIENT-IP: The client's IP address

These headers can be used for server-side tracking if needed.

Advanced Features

Debug Mode

Set GOOGLE_ANALYTICS_DEBUG_MODE = True in your settings to prevent tracking in development environments.

Staff Exclusion

By default, staff users (users with is_staff=True) are not tracked. You can modify this behavior in the should_track function in utils.py.

Event Tracking

Use the standard Google Analytics JavaScript API to track events:

gtag("event", "button_click", {
  event_category: "engagement",
  event_label: "hero_cta",
});

Custom Dimensions and Metrics

Set up custom dimensions and metrics in your Google Analytics property, then use them in your tracking code:

gtag("config", "UA-XXXXXXXX-X", {
  custom_map: { dimension1: "user_type" },
});
gtag("event", "page_view", { user_type: "member" });

Troubleshooting

If you encounter issues:

  1. Check that your GOOGLE_ANALYTICS_PROPERTY_ID is correct.
  2. Ensure the middleware is properly added to your MIDDLEWARE setting.
  3. Verify that the template tag is correctly placed in your base template.
  4. Check browser console for any JavaScript errors.

Contributing

Contributions are welcome! Please follow these steps:

  1. Fork the repository
  2. Create a new branch (git checkout -b feature/amazing-feature)
  3. Commit your changes (git commit -m 'Add some amazing feature')
  4. Push to the branch (git push origin feature/amazing-feature)
  5. Open a Pull Request

Please ensure your code adheres to the project's coding standards and include tests for new features.

License

This project is licensed under the MIT License - see the LICENSE file for details.

Support

If you need help or have any questions, please open an issue on the GitHub repository or contact the maintainers.


Release files for google-analytics-django 0.1.4

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for google-analytics-django 0.1.4
File Size Uploaded
google_analytics_django-0.1.4.tar.gz 10.7 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for google-analytics-django 0.1.4
File Interpreter ABI Platform
google_analytics_django-0.1.4-py3-none-any.whl Python 3 none any Details

Total release size: 20.8 kB

Release files / google_analytics_django-0.1.4.tar.gz

Download URL google_analytics_django-0.1.4.tar.gz
Size 10.7 kB
Tags Source
SHA-256 checksum
How to use checksums
8d649d6c38ead6b7f5c96192a8728937791d9d1c6d147f76639ea3f7abe2ea4b
BLAKE2b-256 checksum
How to use checksums
0e611ad59e2e540cf3e877d783a2fade990e32890e6ba9052701a09254a4cdf7
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.1 CPython/3.12.1

Release files / google_analytics_django-0.1.4-py3-none-any.whl

Download URL google_analytics_django-0.1.4-py3-none-any.whl
Size 10.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
809e919dd00777562c58ef34b18d931ff5e465b3b3ea4972dfa9be41f806eca3
BLAKE2b-256 checksum
How to use checksums
e62e1b513c4b5bb00089b51be42d36d124d993978398ce8b3a968b8298e875b2
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.1 CPython/3.12.1

Release history Release notifications | RSS feed

This release

0.1.4 This release

2 release files

0.1.3

2 release files

0.1.2

2 release files

0.1.1

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page