Package: django-utm-tracker
Django UTM TrackerDjango app for extracting and storing UTM tracking values.Django supportThis package support Django 3.2+, and Python 3.8+BackgroundThis app has been designed to integrate the standardutm_*querystring
parameters that are used by online advertisers with your Django project.It doesnotreplace 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 parametersParameterDefinitionutm_mediumIdentifies what type of link was used.utm_sourceIdentifies which site sent the traffic, and is a required parameter.utm_campaignIdentifies a specific product promotion or strategic campaign.utm_termIdentifies search terms.gclidIdentifies a google click, is used for ad tracking in Google Analytics via Google Ads.aclkIdentifies a Microsoft Ad click (bing), is used for ad tracking.msclkidIdentifies a Microsoft Ad click (MS ad network), is used for ad tracking.twclidIdentifies a Twitter Ad click, is used for ad tracking.fbclidIdentifies a Facebook Ad click, is used for ad tracking.In addition to the fixed list above, you can also specify custom tags
using theUTM_TRACKER_CUSTOM_TAGSsetting. Any querystring params that
match these tags are stashed in a JSONField calledcustom_tags.How it worksThe app works as a pair of middleware classes, that extractutm_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 - seetest_utm_and_lead_sourcefor a real example):client=Client()# first request stashes values, but does not create a LeadSource as user is anonymousclient.get("/?utm_medium=medium&utm_source=source...")assertutm_values_in_sessionassertLeadSource.objects.count()==0# subsequent request, with authenticated user, extracts values and stores LeadSourceuser=User.objects.create(username="fred")client.force_login(user,backend=settings.FORCED_AUTH_BACKEND)client.get("/")assertnotutm_values_in_sessionassertLeadSource.objects.count()==1Why split the middleware in two?By splitting the middleware into two classes, we enable the use case where we
can track leads withoututm_querystring parameters. For instance, if you have
an internal referral program, using a simple token, you can capture this as aLeadSourceby adding sentinel values to therequest.session:defreferral(request,token):# do token handling...# medium and source are mandatory for lead source capturerequest.session["utm_medium"]="referral"request.session["utm_source"]="internal"# campaign, term and content are optional fieldsrequest.session["utm_campaign"]="july"request.session["utm_term"]=tokenrequest.session["utm_content"]="buy-me"returnrender(request,"landing_page.html")ConfigurationAdd the app toINSTALLED_APPS:# settings.pyINSTALLED_APPS=[..."utm_tracker"]UTM_TRACKER_CUSTOM_TAGS=["tag1","tag2"]and add both middleware classes toMIDDLEWARE:# settings.pyMIDDLEWARE=[..."utm_tracker.middleware.UtmSessionMiddleware","utm_tracker.middleware.LeadSourceMiddleware",]TheUtmSessionmiddleware must come beforeLeadSourcemiddleware.