Sub topic 3
Tracking setup and UTM tagging
Every later chapter in this tutorial depends on one thing: each visit, lead, and sale must be traceable to the campaign that produced it. This chapter covers the plumbing that makes that possible. It assumes you know what a channel and a campaign are, and that you have seen a web analytics tool or a CRM (a customer relationship management system that stores leads and customers). You don't need to have configured either.
What tracking has to accomplish
Tracking answers three questions about each person who reaches your business:
- Where did they come from? This is the source, medium, and campaign.
- What did they do? Examples are viewing a page, submitting a form, or buying.
- Can that origin still be connected to them when they become a lead or a customer?
The third question is where most setups fail. Analytics tools see the click, and CRMs see the sale. Unless the origin data travels from one to the other, your reports can count visits but can't link them to revenue.
The setup has four layers:
| Layer | Job | Typical tool |
|---|---|---|
| Link tagging | Label each inbound link with its origin | UTM parameters, ad-platform auto-tagging |
| Page collection | Read the labels and record visits and events | Analytics tag, tag manager |
| Capture | Save the origin when the visitor becomes a lead | Cookies and hidden form fields |
| Storage | Keep origin fields on the lead and customer record | CRM source fields |
UTM parameters
UTM stands for Urchin Tracking Module, named after the analytics company Google acquired. A UTM parameter is a key-value pair appended to a URL's query string, the part after the ?. Analytics tools read these pairs when the page loads and attach them to the session.
There are five standard parameters:
| Parameter | Meaning | Example |
|---|---|---|
| `utm_source` | The specific place the link was shown | `newsletter`, `linkedin`, `partner_acme` |
| `utm_medium` | The type of channel | `email`, `cpc`, `social`, `referral` |
| `utm_campaign` | The initiative the link belongs to | `spring_launch` |
| `utm_content` | Which variant or placement was clicked | `hero_button`, `footer_link` |
| `utm_term` | The paid search keyword | `crm_software` |
Source, medium, and campaign are the working core. Content and term are optional.
Here is the tagged URL from the plan:
https://example.com/pricing?utm_source=newsletter&utm_medium=email&utm_campaign=spring_launchWhen someone clicks it, the analytics tool records a session with source newsletter, medium email, and campaign spring_launch. Without the tags, many email clients and apps send the visit with no referrer, and the visit is filed as "direct" traffic. This is traffic that looks like it arrived with no known origin.
What to tag and what not to tag
Tag every link you control that points to your site from outside it. That includes email, paid social, newsletters, partner placements, QR codes, and links in PDFs.
Do not tag links between your own pages. If an internal link carries UTM parameters, the analytics tool starts a new session or overwrites the original source. A visitor who arrived from a paid ad would then appear to have come from your own homepage banner. Use event tracking for internal clicks instead.
Paid search and some ad platforms use auto-tagging. This adds a click ID such as gclid to the URL automatically. Auto-tagging gives richer data than manual UTMs, but the analytics tool only reads the click ID if the two systems are linked. Check that link before launch. If you add manual UTMs on top, confirm which set of values your analytics tool prefers, because the two can disagree.
Naming conventions
UTM values are plain text, and analytics tools treat them as case-sensitive. Email, email, and E-mail become three separate rows in a report. One person writing spring_launch and another writing Spring-Launch splits one campaign into two.
Fix this with a short written convention that everyone follows:
- Use lowercase only.
- Use underscores in place of spaces and hyphens.
- Choose
utm_mediumfrom a fixed list, for exampleemail,cpc,social,referral,display,affiliate. - Build campaign names the same way each time, for example
{year}_{quarter}_{initiative}such as2026_q2_spring_launch. - Never put personal data, such as an email address, in a UTM value. Query strings are logged in many places, including server logs, browser history, and third-party tools.
Keep the list of allowed values in a shared spreadsheet or a script. A typo in a medium value can drop a whole campaign out of your channel report.
Generating tags in code
Building URLs by hand invites mistakes. This function builds them and applies the conventions. It keeps any query parameters already on the URL.
from urllib.parse import urlparse, parse_qsl, urlencode, urlunparse
ALLOWED_MEDIUMS = {"email", "cpc", "social", "referral", "display", "affiliate"}
def clean(value):
"""Lowercase, trim, and replace spaces and hyphens with underscores."""
return value.strip().lower().replace(" ", "_").replace("-", "_")
def tag_url(url, source, medium, campaign, content=None, term=None):
medium = clean(medium)
if medium not in ALLOWED_MEDIUMS:
raise ValueError(f"medium {medium!r} is not in the allowed list")
parts = urlparse(url)
query = dict(parse_qsl(parts.query))
query["utm_source"] = clean(source)
query["utm_medium"] = medium
query["utm_campaign"] = clean(campaign)
if content:
query["utm_content"] = clean(content)
if term:
query["utm_term"] = clean(term)
return urlunparse(parts._replace(query=urlencode(query)))
print(tag_url("https://example.com/pricing", "Newsletter", "email", "Spring Launch"))
# Output:
# https://example.com/pricing?utm_source=newsletter&utm_medium=email&utm_campaign=spring_launchThe function rejects an unknown medium instead of silently creating a new row in your reports. urlencode also escapes characters that would otherwise break the URL.
Collecting the data on your site
Tagged links only help if something reads them. Most teams use one of two approaches:
- Analytics tag directly on the page. A snippet from the analytics vendor is pasted into the site's code. This is simple but needs a developer for each change.
- Tag manager. A tag manager is a tool where you configure which scripts load and when, without editing site code. You define a trigger such as "form submitted" and attach a tag that sends an event to analytics.
Beyond page views, define a small set of conversion events: the actions that matter at each funnel stage. A starting list might be newsletter_signup, demo_request, trial_start, and purchase. Name them once, in a shared document, and keep them stable. Renaming an event breaks the history that trend reports rely on.
Carrying the source into the CRM
Analytics shows how people behaved, but your CRM holds the revenue. To connect them, copy the origin onto the lead record:
- When a page loads with UTM parameters, a small script saves them in a first-party cookie, which is a cookie set by your own domain.
- The form includes hidden fields named
utm_source,utm_medium, andutm_campaign. - A script fills those fields from the cookie before submission.
- The CRM maps each field to a property on the lead.
Decide which origin you store. The cookie usually holds the first touch, the original source, and some teams also save the most recent one in separate fields (first_utm_source, last_utm_source). The attribution chapter later in this tutorial depends on having both, so store them separately from the start. Adding them later does not recover past leads.
Where tracking breaks
Tracking data is always incomplete. Know the common gaps so you can read reports with the right level of doubt.
- Consent and privacy rules. In regions covered by laws such as GDPR, analytics cookies need visitor consent. Visitors who decline are not recorded, so counts are lower than real traffic.
- Ad blockers and browser limits. Some browsers and extensions block analytics scripts or shorten cookie lifetimes. Returning visitors may then look like new ones.
- Redirects that strip parameters. A redirect from
example.comtowww.example.com, or through a link shortener, can drop the query string. Test every tagged link by clicking it and checking the landing URL. - Dark social. Links shared in private messages or copied by hand carry no tags and show up as direct traffic.
- Cross-device journeys. A visitor who clicks an ad on a phone and buys on a laptop appears as two people.
- Inconsistent tagging. This is the most common cause, and the only one you fully control.
A good habit is a launch checklist: link tagged, link tested, conversion event firing, form fields mapping to the CRM. Run through it for every new campaign.
Exercises
Exercise
A teammate tags a "Learn more" banner on your homepage that links to your pricing page with ?utm_source=homepage&utm_medium=banner&utm_campaign=pricing_push. Reports start showing a large share of pricing-page conversions from "homepage / banner". What went wrong, and what should be used instead?
Show answer
Internal links must not carry UTM parameters. The tags started a new session (or overwrote the existing source), so visitors who really arrived from ads, email, or search were reassigned to "homepage / banner". Remove the UTMs from the banner link and measure banner clicks with an analytics event (for example banner_click with a label), which records the click without changing the visitor's source.
Exercise
Using the tag_url function, what URL does tag_url("https://example.com/demo?ref=1", "LinkedIn", "Social", "2026 Q2 Webinar", content="top banner") return, and why is the order of parameters what it is?
Show answer
https://example.com/demo?ref=1&utm_source=linkedin&utm_medium=social&utm_campaign=2026_q2_webinar&utm_content=top_banner. The function parses the existing query (ref=1) into a dictionary first, so it is kept. It then adds the UTM keys in the order they are assigned (source, medium, campaign, content). Each value is lowercased, and spaces become underscores.