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
  • 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

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.1.0.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.0-py3-none-any.whl (4.3 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: utm_tracker_django-1.1.0.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.0.tar.gz
Algorithm Hash digest
SHA256 17c9daa43076c1bb2fe85303819b23ad4a9c252a1ffe6b9538b8041f82c0e5ab
MD5 f0e9d7ea55753ae491a53bb5e2721f6d
BLAKE2b-256 86b1c113d2c6ec601fd3afebf49f44d9026b538870e362db6d9b1043b2d237a1

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for utm_tracker_django-1.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 0882ea43a259b0eddb12dcc18e0fce73d2c47bb27b061e907a4cadf3e52f3628
MD5 aa70c5b24a5f61dfa7c0137c15c566be
BLAKE2b-256 cc81941ee086ecb033fe43771099aab9c06479d208c8a57c08be8bbc28ccc10e

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