Sub topic 3
Tagging links so you can follow the traffic
This chapter assumes you know what a marketing campaign is and can work with percentages and ratios. It also assumes you have seen a basic analytics report, such as a list of sessions by source. Earlier chapters set a measurable goal and mapped the funnel. Here you set up tracking, so that clicks, leads, and sales can later be traced back to the campaign that produced them.
Why untagged links lose information
When a visitor arrives on your site, the analytics tool can see only what the browser reports. The main item is the referrer, the address of the page the visitor came from. That is often missing or too vague to be useful:
- Email apps and some messaging apps usually send no referrer, so the visit is filed as "direct" traffic.
- Many social apps open links in an in-app browser, which may hide or change the referrer.
- A referrer can say "facebook.com" but cannot say which post, ad, or audience sent the click.
- Two different campaigns on the same platform look identical.
The result is a report where a large share of visits has an unknown origin. You then cannot calculate click-through rate, cost per lead, or return on ad spend for each campaign, because you cannot tell which visits belong to which spend.
A tag fixes this by putting the origin inside the link itself. The visitor's browser carries the tag to your site, and the analytics tool reads it from the address.
What a UTM tag is
UTM parameters (UTM stands for Urchin Tracking Module, named after an early analytics product) are labeled values added to the end of a link. They begin after a ? and are separated by &. Each one has the form name=value.
https://example.com/spring-offer?utm_source=newsletter&utm_medium=email&utm_campaign=spring_launchThe part before the ? is the normal page address. Everything after it is the tag. Visitors still land on the same page, and the tag does not change what they see.
The three core parameters
| Parameter | Question it answers | Example values |
|---|---|---|
| `utm_source` | Which specific place sent the visit? | `newsletter`, `facebook`, `google` |
| `utm_medium` | What type of channel was it? | `email`, `paid_social`, `cpc`, `affiliate` |
| `utm_campaign` | Which campaign was it part of? | `spring_launch` |
Think of them as a hierarchy. The medium is the channel type, the source is the specific property inside it, and the campaign is the initiative that ties several links together. For one launch you might have paid_social as the medium with facebook and instagram as sources, all under the single campaign spring_launch.
Optional parameters
Two more parameters are widely supported:
utm_contentseparates links inside the same campaign, for examplehero_bannerversustext_link, or two ad creatives in an A/B test.utm_termrecords the keyword for paid search campaigns.
Add them only when you will actually compare the variants. Each extra value is one more thing to keep consistent.
Build a naming convention before you tag anything
Analytics tools treat tag values as plain text. They do not guess that two spellings mean the same thing. The values Facebook, facebook, and fb can appear as three separate rows in your report, each with a fraction of the true total.
Write down a short convention and keep it where everyone who builds links can find it. A workable set of rules:
- Use lowercase only.
- Use underscores instead of spaces, because a space must be encoded in a link and makes it harder to read.
- Choose one fixed list of mediums, such as
email,paid_social,cpc,organic_social,referral, and use nothing outside it. - Name campaigns so they sort and read well, for example
spring_launch_2027rather thanSpring Launch!!. - Never put personal data, such as an email address or a customer name, in a tag. Links get shared, logged, and stored in many places.
A shared spreadsheet with one row per link is enough for a first campaign. It gives you a record of what each tag means, so that months later you can still decode utm_content=v2b.
Build tagged links without typos
Typing long links by hand invites errors, such as a missing & or a stray space. Generating them from code removes that risk and enforces your convention at the same time. The script below builds a link, lowercases and cleans each value, and encodes the result correctly. It uses only the Python standard library.
from urllib.parse import urlencode, urlsplit, urlunsplit, parse_qsl
def clean(value: str) -> str:
"""Lowercase, trim, and replace spaces and hyphens with underscores."""
return value.strip().lower().replace(" ", "_").replace("-", "_")
def tag_link(url: str, source: str, medium: str, campaign: str,
content: str | None = None, term: str | None = None) -> str:
parts = urlsplit(url)
# Keep any query parameters the link already has.
params = parse_qsl(parts.query, keep_blank_values=True)
params += [
("utm_source", clean(source)),
("utm_medium", clean(medium)),
("utm_campaign", clean(campaign)),
]
if content:
params.append(("utm_content", clean(content)))
if term:
params.append(("utm_term", clean(term)))
return urlunsplit(parts._replace(query=urlencode(params)))
links = [
tag_link("https://example.com/spring-offer",
"Newsletter", "email", "Spring Launch"),
tag_link("https://example.com/spring-offer",
"facebook", "paid social", "spring_launch", content="video_ad"),
tag_link("https://example.com/spring-offer?ref=partner",
"instagram", "paid_social", "spring_launch"),
]
for link in links:
print(link)Running it prints:
https://example.com/spring-offer?utm_source=newsletter&utm_medium=email&utm_campaign=spring_launch
https://example.com/spring-offer?utm_source=facebook&utm_medium=paid_social&utm_campaign=spring_launch&utm_content=video_ad
https://example.com/spring-offer?ref=partner&utm_source=instagram&utm_medium=paid_social&utm_campaign=spring_launchThe inputs Newsletter, Spring Launch, and paid social came out in the agreed form. The third link kept its existing ref=partner parameter, which a naive "append ?utm_..." approach would have broken by adding a second ?.
How the tag travels from click to report
The tag is read at the moment the visitor lands. Your analytics tool saves it with the session, and every later event in that session, such as a form submission or a purchase, can be reported under the same source, medium, and campaign.
Two details matter here. First, the tag must still be present when the page loads, so any redirect between the click and your page must keep the query string. Second, the tag attaches to a session, and sessions end. If a visitor leaves and returns a week later by typing your address, the second visit is "direct" and does not carry the campaign. That limit comes back in the attribution chapter.
Where tags help in the funnel
Tags matter most in the middle of the funnel, where you connect spend to behavior.
- Clicks: a platform's own click count and your analytics session count for the same campaign can be compared. Tagged traffic lets you do that.
- Leads: tagged sessions let you count form submissions per campaign and compute click-to-lead rate for each source.
- Sales: if a purchase happens in a tagged session, revenue can be assigned to the campaign, which is what return on ad spend needs.
Impressions and reach are not covered by tags. They happen on the ad platform before any click, so you read them from that platform's report. Tags start working only once someone arrives on your site.
Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| Same channel appears under several names | Inconsistent case or spelling (`Email`, `email`, `e-mail`) | Adopt the convention and generate links from one script or sheet |
| Paid traffic appears as "direct" | Tag lost in a redirect, or link shortener that drops the query string | Test the final landing address after every redirect |
| Campaign traffic is credited to your own site | UTM tags on internal links | Remove tags from internal links |
| A link breaks or the tag is cut off | Two `?` characters, or an unescaped `&` or space inside a value | Build links with a tool that encodes values |
| Paid ads show no tagged traffic | The ad platform's destination field was left untagged | Add the tags to the ad's destination link, or use its tracking template |
| Tag values reveal customer details | Personal data placed in a value | Use anonymous labels only |
Some ad platforms can add their own tracking values automatically, such as a click identifier. These can work alongside UTM tags, but they behave differently between platforms. When both are present, check the documentation of your analytics tool to see which one takes priority.
Summary of the workflow
- Write the naming convention and the list of allowed mediums.
- Generate one tagged link per placement, and record each one in a sheet.
- Test each link and confirm it appears correctly in real-time reports.
- Launch, and leave the tags unchanged for the whole campaign so the data stays comparable.
Exercise
A colleague creates two links for the same email newsletter. One uses utm_source=Newsletter&utm_medium=Email and the other uses utm_source=newsletter&utm_medium=email. Both use utm_campaign=spring_launch. What will the report likely show, and what two steps prevent it?
Show answer
Because tag values are treated as plain text and often case-sensitive, the report will likely split the newsletter traffic into two rows, each showing only part of the real total, which makes click and lead counts per source misleading. Prevent it by writing a naming convention that requires lowercase values, and by generating every link from one script or shared sheet so that the spelling cannot drift.
Exercise
You tag a banner on your homepage that links to your spring offer page, using utm_source=homepage&utm_medium=banner&utm_campaign=spring_launch. Visitors who clicked a paid ad earlier in the same visit now show up as coming from "homepage". Why does this happen, and what should you do?
Show answer
An internal link with UTM tags starts a new session in many analytics tools and replaces the original source with the tag, so the paid ad loses credit. Remove the UTM tags from internal links. If you need to measure banner clicks, use the analytics tool's event tracking instead of campaign tags.
Exercise
Using the tag_link function from this chapter, what call produces a link to https://example.com/offer for a partner email, where the campaign is "Summer Sale", the source is "Partner Mail", and the medium is "email"? Write the expected result.
Show answer
tag_link("https://example.com/offer", "Partner Mail", "email", "Summer Sale") returns https://example.com/offer?utm_source=partner_mail&utm_medium=email&utm_campaign=summer_sale.