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 utm-tracker-django

⚙️ Setup

1. Add Middleware

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

MIDDLEWARE = [
    ...
    'utm_tracker_django.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
  • utm_content
  • utm_content
  • utm_content
  • utm_content
  • utm_content
  • gclid
  • aclk
  • fbclid'
  • msclkid
  • dclid
  • yclid
  • gclsrc
  • utm_id
  • utm_referrer
  • twcli

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

utm_tracker_django/
├── __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.1.1.tar.gz (3.6 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.1.1-py3-none-any.whl (4.3 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: utm_tracker_django-1.1.1.tar.gz
  • Upload date:
  • Size: 3.6 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.1.1.tar.gz
Algorithm Hash digest
SHA256 4d28bdf6c0926cb109039055f8158fdb0e6ec669ca11ff317022186a3e149c56
MD5 a6a77e155d8e49dcc7241be86b4936f3
BLAKE2b-256 3cf8d200a66a19d6a9905864b955a8abfea9f252f71fd726f392798c278afe99

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for utm_tracker_django-1.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 f9862a8e294c6eabd70198e6b9f35b8decc376025626165bd43d13c08483871c
MD5 b38c92b8357d4d15a37a38649daed42e
BLAKE2b-256 f4643e4755716a76a1171d3659150272f381f05ed51144a728cdd3ce19aaa087

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