UTM parameters done right: a naming convention and a 50-line linter
We're the team behind FastAnalytics, a cookieless, EU-hosted web analytics tool. This post is about UTM parameters in general: everything here works with GA4 and with any other analytics tool. Open the campaign report o
We're the team behind FastAnalytics, a cookieless, EU-hosted web analytics tool. This post is about UTM parameters in general: everything here works with GA4 and with any other analytics tool.
Open the campaign report of almost any website that has been running ads for a year and you will find something like this:
Facebook 412
facebook 388
fb 97
facebook.com 41
FB_ads 12
That's one traffic source split into five rows. None of it is an analytics bug. Every row is a link that someone typed by hand, on a different day, with a slightly different idea of how to write "facebook".
UTM parameters themselves are simple. Keeping them consistent across a team, an agency and six months of campaigns is the hard part. This post covers three things that fix most of it: a written convention, a linter that checks links before they're published, and a quick test that the parameters survive redirects.
The five parameters in 30 seconds
A UTM link is a normal URL with tags after the ?:
https://example.com/pricing?utm_source=newsletter&utm_medium=email&utm_campaign=black-friday
| Parameter | Answers | Example |
|---|---|---|
utm_source |
Who sent the visit? |
newsletter, instagram, google
|
utm_medium |
What type of channel? |
email, social, cpc
|
utm_campaign |
Which campaign? | black-friday |
utm_term |
Which paid keyword? (optional) | cookieless analytics |
utm_content |
Which link or creative? (optional) | header-button |
If you want the long version with examples for every channel, we wrote a complete UTM guide. Here we'll focus on keeping them clean.
Step 1: write the convention down
Most messy reports come from a convention that only exists in one person's head. Put it in a file that lives next to your campaigns (a README, a Notion page, a pinned message) and keep it short:
UTM CONVENTION
- Everything lowercase, words separated by hyphens: black-friday, not Black Friday
- utm_source = the platform name, never a domain: facebook, not facebook.com
- utm_medium = one of: social, email, cpc, display, affiliate, referral, qr, sms
- utm_campaign = what you're promoting, never the platform: spring-sale, not instagram
- utm_term and utm_content only when you need to compare keywords or creatives
- No UTM on links between pages of your own site
That last rule is the one that hurts the most when it's broken. A UTM on an internal link (a banner on your homepage pointing to /pricing?utm_source=homepage) overwrites the real source of the visit. The person came from Google, clicked your banner, and your report now says they came from "homepage".
Lowercase matters because many tools treat Facebook and facebook as two different values. Rather than checking how each tool behaves, it's easier to never create the difference.
Step 2: lint links before they go out
A convention only helps if somebody checks it. Here's a dependency-free Node script that reads a list of links (one per line) and flags anything that breaks the rules above. It runs on any recent version of Node (it uses the built-in URL class).
// utm-lint.js — checks a list of campaign links against a UTM naming convention.
// Usage: node utm-lint.js links.txt
const fs = require('fs');
const MEDIUMS = new Set(['social', 'email', 'cpc', 'display', 'affiliate', 'referral', 'qr', 'sms']);
const REQUIRED = ['utm_source', 'utm_medium', 'utm_campaign'];
const SLUG = /^[a-z0-9]+(-[a-z0-9]+)*$/; // lowercase, hyphens, no spaces
function lint(raw) {
const problems = [];
let url;
try { url = new URL(raw); } catch { return ['not a valid URL']; }
const p = url.searchParams;
for (const key of REQUIRED) {
if (!p.has(key)) problems.push(`missing ${key}`);
}
for (const [key, value] of p) {
if (!key.startsWith('utm_')) continue;
if (p.getAll(key).length > 1) problems.push(`${key} appears more than once`);
if (key !== 'utm_term' && !value.includes('.') && !SLUG.test(value)) {
problems.push(`${key}="${value}" should be lowercase-with-hyphens`);
}
}
const source = p.get('utm_source');
const medium = p.get('utm_medium');
if (medium && !MEDIUMS.has(medium.toLowerCase())) {
problems.push(`utm_medium="${medium}" is not in the agreed list`);
}
if (source && source.includes('.')) {
problems.push(`utm_source="${source}" looks like a domain, use the platform name`);
}
if (source && source === p.get('utm_campaign')) {
problems.push('utm_source and utm_campaign are the same');
}
return [...new Set(problems)];
}
const file = process.argv[2];
const links = fs.readFileSync(file, 'utf8').split(/\r?\n/).map(l => l.trim()).filter(Boolean);
let failed = 0;
for (const link of links) {
const problems = lint(link);
if (problems.length) {
failed++;
console.log(`✗ ${link}\n - ${problems.join('\n - ')}`);
} else {
console.log(`✓ ${link}`);
}
}
console.log(`\n${links.length - failed}/${links.length} links OK`);
process.exitCode = failed ? 1 : 0;
Give it a links.txt like this:
https://example.com/pricing?utm_source=newsletter&utm_medium=email&utm_campaign=black-friday
https://example.com/?utm_source=Instagram&utm_medium=social&utm_campaign=Black Friday
https://example.com/?utm_source=facebook.com&utm_medium=paid-social&utm_campaign=launch
https://example.com/blog/?utm_source=footer&utm_medium=referral&utm_campaign=footer
https://example.com/?utm_source=linkedin&utm_medium=social
And you get:
✓ https://example.com/pricing?utm_source=newsletter&utm_medium=email&utm_campaign=black-friday
✗ https://example.com/?utm_source=Instagram&utm_medium=social&utm_campaign=Black Friday
- utm_source="Instagram" should be lowercase-with-hyphens
- utm_campaign="Black Friday" should be lowercase-with-hyphens
✗ https://example.com/?utm_source=facebook.com&utm_medium=paid-social&utm_campaign=launch
- utm_medium="paid-social" is not in the agreed list
- utm_source="facebook.com" looks like a domain, use the platform name
✗ https://example.com/blog/?utm_source=footer&utm_medium=referral&utm_campaign=footer
- utm_source and utm_campaign are the same
✗ https://example.com/?utm_source=linkedin&utm_medium=social
- missing utm_campaign
1/5 links OK
The script exits with code 1 when something fails, so you can drop it into CI or a pre-commit hook if your links live in a repo (newsletter templates, landing pages, a links.csv your marketing team maintains).
A few things it deliberately doesn't do:
-
It doesn't check for internal links. A campaign link always points to your own domain, so the URL alone can't tell you whether it's placed on your site or outside it. That rule needs a human (or a crawl of your own pages looking for
utm_inhrefs). - It doesn't fix links for you. Auto-lowercasing is tempting, but if the wrong value is already live on a printed flyer, silently "fixing" the new links splits the report again. Better to see the error and decide.
-
It doesn't check
utm_term. Search keywords contain spaces and capitals by nature, and ad platforms often fill them in automatically.
Change MEDIUMS to whatever your team agreed on. The list above matches the channel grouping we use, but there's no universal standard.
Step 3: check what survives the redirect
URL shorteners, link-in-bio pages, email click tracking and some CMS redirects sit between the click and your page. Most of them keep the query string. Some don't, and you only notice weeks later when the campaign shows zero visits.
curl will tell you in one line where a link actually ends up:
curl -sIL -o /dev/null -w '%{url_effective}\n' "https://your-short-link.example/abc"
-L follows every redirect, -I only fetches headers, and %{url_effective} prints the final URL. If the utm_ parameters aren't in that output, the redirect is eating them. Test it once per tool, not once per link.
How analytics tools read them
Every mainstream web analytics tool reads utm_source, utm_medium and utm_campaign. They differ in what they do with the rest:
- GA4 groups visits into channels using its own rules based on source and medium, so a medium outside its expected values can land in "Unassigned". Worth checking against your convention.
- Fathom stores all five parameters on the session, and Umami has a UTM report that breaks down all five.
-
Simple Analytics collects source, medium and campaign by default, and also accepts shorter forms such as
ref=as the source. - Plausible also reads UTM tags in its sources and campaigns reports.
-
FastAnalytics (ours) reports
utm_source,utm_mediumandutm_campaign, and groups them into channels (email, paid social, organic social, QR and so on). To be clear about the limits:utm_termandutm_contentstay in the link but don't have their own reports yet. If comparing creatives withutm_contentis central to your work, Fathom or Umami cover that today.
Whichever tool you use, the convention matters more than the tool. A clean set of links will give you a readable report in any of them, and a messy one won't be saved by any of them.
Checklist
- [ ] Convention written down and shared with everyone who creates links
- [ ] Lowercase and hyphens everywhere
- [ ] A short, fixed list of mediums
- [ ] Source = platform, campaign = what you promote
- [ ] No UTM on internal links
- [ ] Links linted before publishing
- [ ] Each shortener or redirect tool tested once with
curl
If you'd rather click than script, our free UTM builder applies the same rules and shows which channel each link will be grouped into. No account needed.
AI-assisted: drafted with an AI assistant and reviewed and tested by the FastAnalytics team. The script was run with Node 22.
Cover photo by Karine Avetisyan on Unsplash.
Originally published by Dev.to WebDev. Aggregated on AIWithGhost for educational purposes — full credit and traffic to the original publisher.