Skip to main content

Django package for tracking campaign conversions with UTM parameters

Project description

django-attribution

PyPI version codecov

Track UTM parameters and marketing campaigns to identify which sources drive conversions and revenue

Core Concepts

Identity

An identity represents a visitor who came to your site from a trackable marketing source.

An identity can be:

  • Browsing without logging in (tracked by cookie)
  • Linked to a logged-in user account
  • Merged when an anonymous visitor logs in (their history gets consolidated with their user account)
Touchpoint

A touchpoint captures where someone came from when they visit your site with tracking data.

Includes:

  • UTM parameters (utm_source=google, utm_campaign=summer_sale)
  • Click IDs (gclid, fbclid, etc.)
  • URL they landed on and referrer
Conversion

A conversion is when someone does something valuable - signs up, makes a purchase, starts a trial.

  • Links to the identity who converted
  • Can have a monetary value and currency
  • Can be marked as confirmed/unconfirmed (useful for pending payments)
  • Gets attributed back to touchpoints to see which campaigns drove results

Installation

pip install django-attribution

Django Configuration

INSTALLED_APPS = [
    # ... other apps ...
    "django_attribution",
    # ... other apps ...
]
python manage.py migrate
MIDDLEWARE = [
    # ... other middlewares ...
    "django_attribution.middlewares.TrackingParameterMiddleware",
    "django_attribution.middlewares.AttributionMiddleware",
    # ... other middlewares ...
]

Usage

Recording Conversions

from django_attribution.shortcuts import record_conversion

# Simple conversion
def signup_view(request):
    # ... signup logic ...
    record_conversion(request, 'signup')

# Two-step flow with confirmation
def order_view(request):
    # ... order processing ...
    order = Order.objects.create(total=99.99)

    record_conversion(
        request,
        'order_placed',
        value=order.total,
        source_object=order,  # Link to order for later reference
        is_confirmed=False
    )

# Later, in payment confirmation view:
def payment_webhook(request):
    # ... payment processing ...
    order = Order.objects.get(id=order_id)

    # Find and confirm the conversion
    conversion = Conversion.objects.get(
        source_content_type=ContentType.objects.get_for_model(Order),
        source_object_id=order.id,
        event='order_placed'
    )
    conversion.is_confirmed = True
    conversion.save()

Event Restrictions

Use decorators or mixins to enforce allowed events. This prevents typos and ensures consistency:

from django_attribution.decorators import conversion_events
from django_attribution.mixins import ConversionEventsMixin

# Function-based view
@conversion_events('signup', 'purchase')
def my_view(request):
    record_conversion(request, 'signup')  # Allowed
    record_conversion(request, 'purchase')  # Allowed
    record_conversion(request, 'newsletter')  # Raises ValueError

# Class-based view
class CheckoutView(ConversionEventsMixin, View):
    conversion_events = ['purchase']

    def post(self, request):
        record_conversion(request, 'purchase')  # Allowed
        record_conversion(request, 'signup')  # Raises ValueError

Available Parameters

The record_conversion function accepts:

  • request: Django request object
  • event_type: Conversion event name (required)
  • value: Monetary value (optional)
  • currency: Currency code (optional, defaults to settings)
  • custom_data: Additional metadata (optional)
  • source_object: Related model instance (optional)
  • is_confirmed: Whether confirmed (optional, defaults to True)

Attribution Analysis

See which campaigns drove your conversions:

from django_attribution.models import Conversion
from django_attribution.attribution_models import first_touch, last_touch

# Last-touch attribution (most recent campaign gets credit)
conversions = Conversion.objects.valid().with_attribution(last_touch) # .valid() = is_active + is_confrimed

for conversion in conversions:
    print(f"Conversion: {conversion.event}")
    print(f"Source: {conversion.attribution_data.get('utm_source')}")
    print(f"Campaign: {conversion.attribution_data.get('utm_campaign')}")

# First-touch attribution (first campaign gets credit)
conversions = Conversion.objects.valid().with_attribution(first_touch)

# Custom attribution window (default is 30 days)
conversions = Conversion.objects.valid().with_attribution(last_touch, window_days=7)

# Different windows per source
source_windows = {
    'google': 14,
    'email': 7,
}

conversions = Conversion.objects.with_attribution(
    last_touch,
    window_days=30,  # default window
    source_windows=source_windows
)

Configuration

Optional settings to customize behavior in your Django settings.py:

DJANGO_ATTRIBUTION = {
    "CURRENCY": "USD",

    # Cookie settings
    "COOKIE_MAX_AGE": 60 * 60 * 24 * 90,  # 90 days
    "COOKIE_NAME": "_dj_attr_id",

    "FILTER_BOTS": True,

    # Skip tracking utm params on these URLs
    "UTM_EXCLUDED_URLS": [
        "/admin/",
        "/api/",
    ],

    # Max length for UTM parameters
    "MAX_UTM_LENGTH": 200,
}

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_attribution-0.1.6.tar.gz (26.0 kB view details)

Uploaded Source

Built Distribution

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

django_attribution-0.1.6-py3-none-any.whl (21.5 kB view details)

Uploaded Python 3

File details

Details for the file django_attribution-0.1.6.tar.gz.

File metadata

  • Download URL: django_attribution-0.1.6.tar.gz
  • Upload date:
  • Size: 26.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for django_attribution-0.1.6.tar.gz
Algorithm Hash digest
SHA256 ca1597222b9b49b683e26d5cb805d90dd4ee6dd05445299a9a5a70ff49720f8a
MD5 8b374bcadb8d2f60f77357e38ce5740c
BLAKE2b-256 8d4933a0a1ceb195a438ac6ed4fe86c44c879e0bc8cccd60b550090ad552b771

See more details on using hashes here.

Provenance

The following attestation bundles were made for django_attribution-0.1.6.tar.gz:

Publisher: publish.yml on YounesOMK/django-attribution

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file django_attribution-0.1.6-py3-none-any.whl.

File metadata

File hashes

Hashes for django_attribution-0.1.6-py3-none-any.whl
Algorithm Hash digest
SHA256 ec95a82c9176986995690822b600fc230de89de17083bd2c999c79996ac074ec
MD5 fc631b71befd7770e1399d965b674d29
BLAKE2b-256 7d0e3ba8bcecdbbf23160dae499b392de9e806dac3a2d5612aff3dd629bd642c

See more details on using hashes here.

Provenance

The following attestation bundles were made for django_attribution-0.1.6-py3-none-any.whl:

Publisher: publish.yml on YounesOMK/django-attribution

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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