Skip to main content

Middleware to track UTM parameters in Django

Project description

📊 django-utm-tracker

A lightweight Django middleware package to track and store UTM parameters in the user's session, enabling attribution tracking for performance marketing and analytics workflows.


🚀 Features

  • ✅ Automatically captures common UTM parameters from URL queries.
  • ✅ Stores them in session for use across multiple views.
  • ✅ Simple, non-intrusive middleware integration.
  • ✅ Fully extensible and pluggable.

🔧 Installation

Install the package using pip:

pip install django-utm-tracker

⚙️ Setup

1. Add Middleware

In your Django settings, add the middleware to your MIDDLEWARE list:

MIDDLEWARE = [
    ...
    'django_utm_tracker.middleware.UTMMiddleware',
]

Make sure it comes before any middleware that processes sessions or modifies redirects.


2. (Optional) Custom UTM Parameters

By default, it tracks:

  • utm_source
  • utm_medium
  • utm_campaign
  • utm_term
  • utm_content

You can override these by defining UTM_PARAMETERS in your settings.py:

UTM_PARAMETERS = [
    'utm_source',
    'utm_medium',
    'utm_campaign',
    'utm_term',
    'utm_content',
    'ref',  # example of a custom parameter
]

🧪 Usage in Views

You can access UTM data from the session like this:

def my_view(request):
    source = request.session.get("utm_source")
    campaign = request.session.get("utm_campaign")
    ...

You can then store these in your models (like a Lead, Order, etc.) if needed.


🧱 Project Structure

django_utm_tracker/
├── __init__.py
├── conf.py
└── middleware.py

💡 Use Cases

  • Tracking UTM for lead generation forms
  • Measuring ad performance across landing pages
  • Associating user behavior with marketing campaigns

✅ Example

For a URL like:

https://yourdomain.com/?utm_source=google&utm_medium=cpc&utm_campaign=spring_sale

The middleware will automatically populate request.session with:

{
    'utm_source': 'google',
    'utm_medium': 'cpc',
    'utm_campaign': 'spring_sale'
}

🧰 Future Enhancements

  • Model integration via signals or mixins
  • Cookie fallback if session is cleared
  • Expiry timeout for UTM session values
  • Admin visibility of tracked UTM data

📜 License

MIT License


👨‍💻 Author

Built with 💙 by Preet Sonpal
Brought to you by Weird Labs

Project details


Download files

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

Source Distribution

utm_tracker_django-1.0.tar.gz (3.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

utm_tracker_django-1.0-py3-none-any.whl (4.3 kB view details)

Uploaded Python 3

File details

Details for the file utm_tracker_django-1.0.tar.gz.

File metadata

  • Download URL: utm_tracker_django-1.0.tar.gz
  • Upload date:
  • Size: 3.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.9.6

File hashes

Hashes for utm_tracker_django-1.0.tar.gz
Algorithm Hash digest
SHA256 a61d8e6eaec230ae3fbf562376656413a38627870249ffe1eb02a66dbbc8a9de
MD5 d749adbe05b4ab6c018285be020955b4
BLAKE2b-256 5bf49d9214cf1f75927a44aecafc68cabe402dde1c84a9dc5c3ac713f80a7ccc

See more details on using hashes here.

File details

Details for the file utm_tracker_django-1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for utm_tracker_django-1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 116e34ae1d3f0cec355bab66bd8d695d981d903c9f01bdfe9720153521629e98
MD5 b19bf5f7fb74b66e776ca0ad0045b8a5
BLAKE2b-256 c2b090b398513097b6a985c631fe83f8f1102bc1555c2786638485cfa1210b86

See more details on using hashes here.

Supported by

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