Skip to main content

Django app for extracting and storing UTM tracking values.

Project description

Django UTM Tracker

Django app for extracting and storing UTM tracking values.

Django support

This package supports Django 4.2+, and Python 3.10+

Background

This app has been designed to integrate the standard utm_* querystring parameters that are used by online advertisers with your Django project.

It does not replace analytics (e.g. Google Analytics) and Adwords tracking, but does have one crucial difference - it allows you to assign a specific user to a campaign advert.

This may be useful if you are trying to assess the value of multiple channels / campaigns.

Supported querystring parameters

Parameter Definition
utm_medium Identifies what type of link was used.
utm_source Identifies which site sent the traffic, and is a required parameter.
utm_campaign Identifies a specific product promotion or strategic campaign.
utm_term Identifies search terms.
gclid Identifies a google click, is used for ad tracking in Google Analytics via Google Ads.
aclk Identifies a Microsoft Ad click (bing), is used for ad tracking.
msclkid Identifies a Microsoft Ad click (MS ad network), is used for ad tracking.
twclid Identifies a Twitter Ad click, is used for ad tracking.
fbclid Identifies a Facebook Ad click, is used for ad tracking.

In addition to the fixed list above, you can also specify custom tags using the UTM_TRACKER_CUSTOM_TAGS setting. Any querystring params that match these tags are stashed in a JSONField called custom_tags.

How it works

The app works as a pair of middleware classes, that extract utm_ values from any incoming request querystring, and then store those parameters against the request.user (if authenticated), or in the request.session (if not).

The following shows this workflow (pseudocode - see test_utm_and_lead_source for a real example):

client = Client()
# first request stashes values, but does not create a LeadSource as user is anonymous
client.get("/?utm_medium=medium&utm_source=source...")
assert utm_values_in_session
assert LeadSource.objects.count() == 0

# subsequent request, with authenticated user, extracts values and stores LeadSource
user = User.objects.create(username="fred")
client.force_login(user, backend=settings.FORCED_AUTH_BACKEND)
client.get("/")
assert not utm_values_in_session
assert LeadSource.objects.count() == 1

Why split the middleware in two?

By splitting the middleware into two classes, we enable the use case where we can track leads without utm_ querystring parameters. For instance, if you have an internal referral program, using a simple token, you can capture this as a LeadSource by adding sentinel values to the request.session:

def referral(request, token):
    # do token handling
    ...
    # medium and source are mandatory for lead source capture
    request.session["utm_medium"] = "referral"
    request.session["utm_source"] = "internal"
    # campaign, term and content are optional fields
    request.session["utm_campaign"] = "july"
    request.session["utm_term"] = token
    request.session["utm_content"] = "buy-me"
    return render(request, "landing_page.html")

Configuration

Add the app to INSTALLED_APPS:

# settings.py
INSTALLED_APPS = [
    ...
    "utm_tracker"
]

UTM_TRACKER_CUSTOM_TAGS = ["tag1", "tag2"]

and add both middleware classes to MIDDLEWARE:

# settings.py
MIDDLEWARE = [
    ...
    "utm_tracker.middleware.UtmSessionMiddleware",
    "utm_tracker.middleware.LeadSourceMiddleware",
]

The UtmSession middleware must come before LeadSource middleware.

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

django_utm_tracker-1.4.0.tar.gz (9.9 kB view details)

Uploaded Source

Built Distribution

django_utm_tracker-1.4.0-py3-none-any.whl (15.7 kB view details)

Uploaded Python 3

File details

Details for the file django_utm_tracker-1.4.0.tar.gz.

File metadata

  • Download URL: django_utm_tracker-1.4.0.tar.gz
  • Upload date:
  • Size: 9.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.1.3 CPython/3.13.3 Darwin/24.5.0

File hashes

Hashes for django_utm_tracker-1.4.0.tar.gz
Algorithm Hash digest
SHA256 c8d51076628ab028531c1042bf8fbe018440c012b51d1a79de18959ec783573a
MD5 b71dd7cfb8d57999b0b46c24abfad95c
BLAKE2b-256 c25337065f2f31fd9daab9bec2d63722aae3eedeb0b193a595b14d478a7688af

See more details on using hashes here.

File details

Details for the file django_utm_tracker-1.4.0-py3-none-any.whl.

File metadata

  • Download URL: django_utm_tracker-1.4.0-py3-none-any.whl
  • Upload date:
  • Size: 15.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.1.3 CPython/3.13.3 Darwin/24.5.0

File hashes

Hashes for django_utm_tracker-1.4.0-py3-none-any.whl
Algorithm Hash digest
SHA256 7b666322c7cab63fc9e9e79fcb90e73d932f4d1d016fd5d58a1f6b51be62cf9b
MD5 d76cebc7d4596f5f8f3d860e1efb78dd
BLAKE2b-256 317991cc44c2f12d80373832200c66bb4cd8544a88769896fe8bc3e526bb2ca6

See more details on using hashes here.

Supported by

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