Sub topic 7
Tag links and set up tracking
This chapter assumes you can read basic Python, know what a query string is, and have admin or analyst access to a web analytics tool such as Google Analytics 4 (GA4). It also assumes you have already chosen your goals and KPIs, because tracking exists to measure those. Here you will build the tagging scheme that lets you compare search, social, and email results in one report.
Why tagging comes first
A seasonal campaign has a fixed window. If a link goes out untagged, or tagged inconsistently, you cannot fix the data afterward. Clicks already recorded stay in the wrong bucket, and the season may be over before you notice.
Analytics tools guess where traffic came from by reading the referrer, which is the page the visitor came from. The guess often fails. Email clients hide referrers, mobile apps open links in embedded browsers, and ads pass through redirects. Tags fix this by putting the answer in the link itself.
UTM parameters are the common standard. UTM stands for Urchin Tracking Module, named after the analytics product that GA grew out of. They are query string parameters that your analytics tool reads when the visitor lands.
| Parameter | Question it answers | Required | Example |
|---|---|---|---|
| `utm_source` | Which specific platform or list sent the visit? | Yes | `google`, `meta`, `newsletter` |
| `utm_medium` | What type of channel is it? | Yes | `cpc`, `paid_social`, `email` |
| `utm_campaign` | Which campaign is this part of? | Yes | `winter_sale_2026` |
| `utm_content` | Which ad, link, or creative variant? | No | `headline_a`, `hero_button` |
| `utm_term` | Which keyword was bought? | No | `winter boots` |
GA4 treats source, medium, and campaign as the core set. The campaign report is only reliable when all three are present on every link.
The naming convention
A convention is a short written rule set that everyone follows. Write it before any ad is built. This is the one from the plan:
utm_source = google | meta | newsletter
utm_medium = cpc | paid_social | email
utm_campaign = winter_sale_2026
utm_content = headline_a | video_15s | hero_buttonFour rules keep it usable:
- Lowercase only. UTM values are case sensitive.
Emailandemailappear as two separate rows in reports. - Underscores, no spaces. Spaces become
%20or+in the URL and make values harder to read and match. - One campaign name everywhere. Search, social, and email all use
winter_sale_2026. The channel is identified by source and medium, never by the campaign name. - Mediums come from a fixed list. Do not invent
facebook_adone week andpaid-socialthe next.
The medium values are not arbitrary. GA4 sorts traffic into default channel groups using rules on source and medium. A medium of cpc places paid search traffic in Paid Search. A medium of email places traffic in Email. A medium that starts with paid combined with a source GA4 recognizes as a social network lands in Paid Social.
Check your own property's channel group definitions before launch. If GA4 does not recognize a source such as meta as a social network, the visits may show up as Unassigned or in another group. If that happens, either use a source value GA4 recognizes, such as facebook or instagram, or create a custom channel group. Test this with one real click, not by assuming.
Build links with code, not by hand
Hand-typed links drift. One person writes winter-sale-2026, another writes Winter_Sale_2026. A small helper removes that risk, because every channel owner calls the same function.
from urllib.parse import urlencode
def utm_url(base, source, medium, campaign, content=None):
params = {
"utm_source": source,
"utm_medium": medium,
"utm_campaign": campaign,
}
if content:
params["utm_content"] = content
return f"{base}?{urlencode(params)}"
print(utm_url("https://example.com/winter-sale", "newsletter", "email", "winter_sale_2026", "hero_button"))
# https://example.com/winter-sale?utm_source=newsletter&utm_medium=email&utm_campaign=winter_sale_2026&utm_content=hero_buttonurlencode escapes special characters for you, so a value containing & or a space cannot break the query string. The function has two limits you should know about:
- It does not lowercase or validate values. It trusts the caller.
- It always appends
?. If the base URL already has a query string, the result is a malformed URL with two?characters.
Validate before you publish
Add a checker that rejects bad links before they reach an ad platform or an email template. This one enforces the rules above: required parameters, lowercase-and-underscore format, and an allowed list of mediums.
import re
from urllib.parse import urlparse, parse_qs
ALLOWED_MEDIUMS = {"cpc", "paid_social", "email"}
VALUE_PATTERN = re.compile(r"^[a-z0-9_]+$")
def check_utm(url):
problems = []
query = parse_qs(urlparse(url).query)
for key in ("utm_source", "utm_medium", "utm_campaign"):
if key not in query:
problems.append(f"missing {key}")
continue
value = query[key][0]
if not VALUE_PATTERN.match(value):
problems.append(f"{key} has bad format: {value!r}")
medium = query.get("utm_medium", [None])[0]
if medium and medium not in ALLOWED_MEDIUMS:
problems.append(f"unknown medium: {medium}")
return problems
good = "https://example.com/winter-sale?utm_source=google&utm_medium=cpc&utm_campaign=winter_sale_2026"
bad = "https://example.com/winter-sale?utm_source=Newsletter&utm_medium=Email"
print(check_utm(good))
for problem in check_utm(bad):
print(problem)
# []
# utm_source has bad format: 'Newsletter'
# utm_medium has bad format: 'Email'
# missing utm_campaign
# unknown medium: EmailRun this over a spreadsheet of every planned link as a pre-launch gate. It fits naturally into the launch checklist covered in a later chapter.
How each channel applies tags
Each channel has a different place where the tags go. Most tracking failures come from one channel handling this differently from the others.
Search
Paid search uses two systems that you should keep apart:
- Auto-tagging. Google Ads can append a
gclidclick ID to every ad click. When your Google Ads and GA4 accounts are linked, GA4 uses it to fill in campaign data and cost data automatically. - Manual UTM tags. You set these in the ad's final URL or in a tracking template.
If both are present, the platform and the analytics tool each have rules for which one wins. Decide this once, record it, and check one live click in the real-time report. Many teams keep auto-tagging on and still add UTM values, so that other tools, such as a CRM, receive a campaign name in the URL. Use utm_term only if you want keywords written into the URL. Otherwise, rely on the platform's own keyword data.
Social
Paid social platforms let you set URL parameters at the ad level. Meta, for example, has a URL parameters field that accepts dynamic placeholders such as {{campaign.name}}. These save time, but they copy the platform's campaign name into utm_campaign. If your Meta campaign is named Winter Sale - Prospecting, your report row will not match winter_sale_2026. For this campaign, type fixed values rather than placeholders, or name platform campaigns to match your convention exactly.
Organic social posts need tags as well. Use a separate medium such as social, so organic and paid traffic do not merge. Add it to your allowed list if you use it. Links in a profile bio or a pinned post are the ones most often forgotten.
Email is where tagging is most valuable, because there is often no referrer at all. Without tags, much of this traffic is reported as Direct. Tag every link in every send, and vary utm_content by position, such as hero_button and footer_link, so you can see which link earned the click. Most email platforms can append UTM values automatically. If yours does, check that it matches your convention, because the default medium is often capitalized or named differently.
Where tagging does not belong
Never put UTM parameters on links between pages of your own site. When a visitor clicks a tagged internal link, GA4 starts a new session and replaces the original source with the new one. A visitor who arrived from a paid ad can then be credited to your homepage banner. Tag only links that bring people from outside to your site.
The tracking flow
Set up the conversion side
Tags tell you where visitors came from. They do not tell you whether the visit was worth anything. You also need conversion tracking, meaning the actions that match your KPIs.
- Define key events in GA4. Mark the actions that count, such as
purchaseorgenerate_lead, as key events so they appear in attribution and campaign reports. - Pass real values. For purchases, send the order value and currency so the report shows revenue per campaign and not only counts.
- Import costs where you can. Linked Google Ads data brings spend in automatically. For social and email, you may import cost data or compare against a spreadsheet, because your KPIs probably include cost per acquisition.
- Test with a real click. Open a tagged link in a private browser window, complete the conversion, and confirm that it appears with the right source, medium, and campaign. GA4's real-time and DebugView reports can show this. Standard reports can take many hours to update.
Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| Email traffic appears as Direct | Links in the email are untagged | Tag every link and test a live send |
| Same channel appears in two rows | Mixed case or inconsistent spelling | Lowercase rule and the validator |
| Paid social lands in an unexpected channel group | Source or medium not matched by the channel group rules | Check group definitions and test one click |
| Campaign names differ between platform and analytics | Dynamic placeholders overwrote your value | Use fixed values or match names exactly |
| Conversions credited to your own banners | UTMs on internal links | Remove tags from internal links |
| Tags missing after a click | A redirect or link shortener dropped the query string | Test the full path from click to landing page |
Exercise
Your team tags a newsletter link as ?utm_source=Newsletter&utm_medium=Email&utm_campaign=Winter Sale 2026, while search ads use utm_campaign=winter_sale_2026. Name two problems this causes in reporting, and give the corrected newsletter tags.
Show answer
First, Newsletter and Email are capitalized, and UTM values are case sensitive, so they will not group with lowercase values from other sends, and Email may not match the medium that GA4 maps to the Email channel. Second, the campaign Winter Sale 2026 differs from winter_sale_2026, so email results appear as a separate campaign from search and cannot be compared in one report. The corrected tags are ?utm_source=newsletter&utm_medium=email&utm_campaign=winter_sale_2026, with an optional utm_content such as hero_button.
Exercise
Why should you avoid putting UTM parameters on a banner that links from your homepage to the sale page?
Show answer
A click on a tagged internal link starts a new session in GA4 and replaces the visitor's original source and medium. A visitor who came from a paid ad would then be credited to the banner, and the ad's contribution would be lost from the campaign report.