Sub topic 9
Tagging campaign links with UTM parameters
This chapter assumes you know how a URL is built (scheme, host, path, query string) and that you have seen a web analytics report. It also assumes you know the traffic channels from the previous chapter: search ads, social, email and referrals. The goal is to make each of those spokes identifiable when its visitors arrive at your hub, the website.
Why untagged links lose information
When someone clicks a link, the browser usually tells the destination site where the click came from. This is the HTTP referrer. It is often missing or too vague to use:
- Email apps and many mobile apps send no referrer, so the visit looks like direct traffic.
- A social network may send only its own domain, such as
l.facebook.com. That does not tell you which post or ad produced the click. - Two newsletters sent a month apart look identical.
- Links shared in PDFs, chat apps and QR codes often arrive with no referrer.
UTM parameters solve this by putting the attribution data in the link itself. UTM stands for Urchin Tracking Module, named after the analytics product that Google later turned into Google Analytics. Every major analytics tool reads these parameters, so they work as a de facto standard.
Anatomy of a tagged link
A UTM parameter is a name and value pair added to the query string of the destination URL. This is the example from the chapter plan:
https://www.example.com/spring-sale?utm_source=newsletter&utm_medium=email&utm_campaign=spring_saleThe part before ? is the normal page address. Everything after it is the query string. Pairs are joined with &. The page content does not change. Only the analytics tag on the page reads the parameters, and it stores them with the visit.
The five parameters
| Parameter | Question it answers | Required | Example value |
|---|---|---|---|
| `utm_source` | Which specific sender or site? | Yes | `newsletter`, `facebook`, `google` |
| `utm_medium` | Which kind of channel? | Yes | `email`, `cpc`, `social`, `referral` |
| `utm_campaign` | Which initiative or promotion? | Yes | `spring_sale` |
| `utm_term` | Which paid keyword? | No | `garden_tools` |
| `utm_content` | Which variant or placement? | No | `header_button`, `video_a` |
GA4 also accepts utm_id, which carries a campaign identifier. In practice, source, medium and campaign are the three you should always set. Use utm_content to tell apart two links to the same page inside one message. Use utm_term mainly for paid search keywords.
Source and medium work as a pair. A reader of your report should be able to say "newsletter / email" or "facebook / paid_social" and know exactly what it means. Source is the specific origin. Medium is the category of channel. If you mix the two up, for example by setting the medium to facebook, your reports will have no consistent channel roll-up.
How tracking works
The diagram shows the path from link to report.
Two details matter here:
- The analytics tag must run on the landing page. If the page redirects first and the redirect drops the query string, the UTM values are never seen.
- The values are attached to the session (and to the user's first visit, in some models). A later page view inside the same session does not need the parameters.
Naming conventions
Analytics tools treat values as plain text. Email, email and e-mail are three separate mediums, and they appear as three separate rows. Reports then split one channel into pieces, and nobody can say which row is correct. Agree on rules once and write them down:
- Use lowercase only.
- Use underscores or hyphens instead of spaces, and pick one.
- Keep medium to a small fixed list, such as
email,cpc,paid_social,social,referral,affiliate. - Name campaigns with a stable pattern, for example
spring_sale_2026. - Keep a shared spreadsheet of approved values and who owns each campaign.
Medium values also drive automatic channel grouping. GA4's default channel groups use rules on source and medium. For example, a medium of email lands in the Email group and cpc in Paid Search. A medium you invent, such as newsletter_blast, may fall into "Unassigned". Check your analytics tool's documentation for the exact rules before you choose your list.
Building links reliably with code
Typing parameters by hand invites typos. A small function enforces the conventions and handles URLs that already have a query string or a fragment. This Python script uses only the standard library.
import re
from urllib.parse import parse_qsl, urlencode, urlsplit, urlunsplit
def clean(value):
"""Lowercase and replace any run of other characters with one underscore."""
value = value.strip().lower()
return re.sub(r"[^a-z0-9]+", "_", value).strip("_")
def tag_url(url, source, medium, campaign, term=None, content=None):
parts = urlsplit(url)
query = parse_qsl(parts.query, keep_blank_values=True)
utm = {
"utm_source": source,
"utm_medium": medium,
"utm_campaign": campaign,
"utm_term": term,
"utm_content": content,
}
query += [(name, clean(value)) for name, value in utm.items() if value]
return urlunsplit(
(parts.scheme, parts.netloc, parts.path, urlencode(query), parts.fragment)
)
print(tag_url(
"https://www.example.com/spring-sale",
source="Newsletter",
medium="Email",
campaign="Spring Sale",
content="Header Button",
))Running it prints:
https://www.example.com/spring-sale?utm_source=newsletter&utm_medium=email&utm_campaign=spring_sale&utm_content=header_buttonThe function keeps any existing parameters, such as ?ref=abc, and keeps the #fragment at the end, which is where it has to be. It does not check whether the URL already carries UTM parameters. If it might, remove them first, or you will create duplicate keys.
Common errors and edge cases
Tagging internal links
Never add UTM parameters to links between pages of your own site, such as a banner on the home page that points to /spring-sale. When a visitor clicks it, the analytics tool sees new UTM values and may start a new session. It then overwrites the real source, for example the search ad that brought the visitor, with "homepage_banner". Your paid and email numbers shrink, and internal banners appear to bring in traffic. To measure internal promotions, use analytics events or a separate click-tracking method.
Case and spelling drift
utm_medium=Email and utm_medium=email are separate rows. Fix the problem at the source, with a builder and an approved list. Cleaning it up afterwards is harder, and you can rarely merge old rows.
Parameters lost in redirects
Link shorteners, tracking redirects, and http to https or www redirects must pass the query string through. Test every new type of link by clicking it and checking that the final address in the browser still shows the UTM values. Also check that the analytics tool's real-time report shows the visit under the expected source and medium.
Fragments
A # starts the fragment. Browsers do not send the fragment to the server, so anything after it is invisible to most server-side logic. Write /page?utm_source=x#section, never /page#section?utm_source=x. Analytics tags running in the browser can see the full address, but the second form is still a malformed query.
Auto-tagging in ad platforms
Google Ads can add its own click ID (gclid) automatically. With auto-tagging on, the click ID links the visit to the ad platform's cost and keyword data. Manual UTM values on the same link may be ignored or may conflict, depending on your analytics settings. Decide which approach each ad account uses, and do not mix the two without checking the documentation. Other ad platforms offer URL templates that fill UTM values from campaign names. These are safer than typing every link.
Privacy and personal data
Anything in the URL ends up in analytics reports, browser history, server logs and sometimes in third-party tools. Never put email addresses, names or other personal data in UTM values. For example, [email protected] is a privacy problem that is hard to clean up. Use a campaign or list name instead. The later chapter on privacy and consent covers this in more detail.
Long and ugly links
Tagged links are long. Do not paste them as visible text in a printed or spoken context. For QR codes, print materials and social bios, use a short, trusted redirect that preserves the query string, and test it end to end.
Reading the results
Once tagged links are live, the standard acquisition reports group sessions by source, medium and campaign. A simple check is to compare each campaign's sessions to its conversions (goals, such as a form submission). The next chapter covers choosing goals and KPIs. If a campaign row is missing, work through this order:
- Did the link reach the page with the parameters intact?
- Did the analytics tag load before the user left?
- Was the value spelled or cased differently than expected?
- Did a consent banner block the tag? Without consent, some tools do not record the visit at all.
Exercise
A team sends a newsletter that links to the home page banner, and inside the site the banner links to /spring-sale?utmsource=homepage&utmmedium=banner&utmcampaign=springsale. Afterwards, the analytics report shows more "homepage / banner" sessions than expected and fewer email sessions. What is the cause, and what should the team do?
Show answer
The banner link is internal. When a visitor clicks it, the new UTM values replace the original email attribution, so email visits are credited to "homepage / banner". Remove UTM parameters from internal links and measure banner clicks with analytics events instead.
Exercise
Using the tagurl function, which URL results from tagurl("https://www.example.com/guide?ref=abc#top", "LinkedIn", "Paid Social", "Q3 Launch")?
Show answer
https://www.example.com/guide?ref=abc&utmsource=linkedin&utmmedium=paidsocial&utmcampaign=q3_launch#top. The existing ref parameter is kept, the values are lowercased with underscores, and the fragment stays at the end.