📊 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_sourceutm_mediumutm_campaignutm_termutm_contentutm_contentutm_contentutm_contentutm_contentutm_contentgclidaclkfbclid'msclkiddclidyclidgclsrcutm_idutm_referrertwcli
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
Release files for utm-tracker-django 1.1.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| utm_tracker_django-1.1.1.tar.gz | 3.6 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| utm_tracker_django-1.1.1-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 7.9 kB
Release files / utm_tracker_django-1.1.1.tar.gz
| Download URL | utm_tracker_django-1.1.1.tar.gz |
|---|---|
| Size | 3.6 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
4d28bdf6c0926cb109039055f8158fdb0e6ec669ca11ff317022186a3e149c56
|
|
BLAKE2b-256 checksum How to use checksums |
3cf8d200a66a19d6a9905864b955a8abfea9f252f71fd726f392798c278afe99
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.9.6
|
Release files / utm_tracker_django-1.1.1-py3-none-any.whl
| Download URL | utm_tracker_django-1.1.1-py3-none-any.whl |
|---|---|
| Size | 4.3 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
f9862a8e294c6eabd70198e6b9f35b8decc376025626165bd43d13c08483871c
|
|
BLAKE2b-256 checksum How to use checksums |
f4643e4755716a76a1171d3659150272f381f05ed51144a728cdd3ce19aaa087
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.9.6
|