Sub topic 13
Tag Campaign Links with UTM Parameters
This chapter assumes you have a landing page with a clear goal and one metric (see the first chapter), and that you can place links in emails, ads, and social posts. You do not need an analytics account yet. The next chapter covers reading the results.
Why tag links at all
When a visitor arrives at your page, the analytics tool sees a request and, usually, a referrer. The referrer is the page the visitor came from. That is often missing or too vague. Email apps, messaging apps, and many mobile apps strip it, so the visits get filed as "direct". Even when it survives, "facebook.com" does not tell you which post or ad drove the visit.
UTM parameters solve this. A UTM parameter is a name and value pair that you add to the end of a link's URL. The analytics tool reads the pairs when the page loads and attaches them to the visit. The name comes from Urchin Tracking Module, an early analytics product, but you can treat it as a plain convention that most analytics tools support.
Anatomy of a tagged link
Here is the campaign link used throughout this tutorial.
https://www.example.com/spring-sale?utm_source=newsletter&utm_medium=email&utm_campaign=spring_sale&utm_content=hero_buttonThe part before ? is the normal page address. After ? comes a query string, which is a list of name=value pairs separated by &. The page itself ignores these pairs and looks the same to the visitor. Only the analytics tool uses them.
| Parameter | Answers the question | Example value | Required |
|---|---|---|---|
| `utm_source` | Where did the visit come from? | `newsletter`, `linkedin` | Yes |
| `utm_medium` | What kind of channel was it? | `email`, `cpc`, `social` | Yes |
| `utm_campaign` | Which campaign was it? | `spring_sale` | Yes |
| `utm_content` | Which link or creative was clicked? | `hero_button`, `banner_a` | No |
| `utm_term` | Which paid keyword triggered the ad? | `landing_page_tool` | No |
"Required" here means required by good practice. Google Analytics 4 can record a visit with only some of them, but a link missing source, medium, or campaign produces reports that are hard to group.
Choose a naming convention before you tag
Most UTM problems come from inconsistent values, not from syntax. Analytics tools treat values as exact text. Email, email, and e-mail become three separate rows in your report.
Agree on these rules and write them in a shared sheet:
- Use lowercase only.
- Use underscores instead of spaces (
spring_sale, notSpring Sale). Spaces become%20in a URL and look messy. - Keep a short fixed list of mediums, such as
email,social,cpc,referral. - Use the platform name as the source (
linkedin,newsletter), not a sentence. - Make
utm_campaignmatch the campaign name you use internally, so reports and plans line up. - Use
utm_contentto tell apart links within one message, such ashero_buttonandfooter_link.
Build links in practice
You can type the links by hand, but a spreadsheet prevents typos. Keep one row per link with columns for the base URL, source, medium, campaign, and content. A formula joins them into the final link. Google's free Campaign URL Builder does the same job for one link at a time.
The script below builds a tagged link from its parts and encodes each value safely. It runs in any modern browser console or in Node.js.
function buildCampaignLink(base, params) {
const url = new URL(base);
Object.entries(params).forEach(function ([name, value]) {
url.searchParams.set(name, String(value).trim().toLowerCase());
});
return url.toString();
}
console.log(
buildCampaignLink('https://www.example.com/spring-sale', {
utm_source: 'newsletter',
utm_medium: 'email',
utm_campaign: 'spring_sale',
utm_content: 'hero_button'
})
);URL and searchParams handle the ? and & characters and encode special characters. Lowercasing in one place enforces your convention automatically.
Where tagged links go, and where they do not
Tag every link you control that points to the landing page from outside it:
- Email campaign buttons and text links.
- Paid ad destination URLs.
- Social posts and bio links.
- QR codes on print material. Tag the URL before you generate the code, because the code cannot be edited afterward.
- Partner and influencer links.
Do not tag links that sit on your own site and lead to the landing page, such as a navigation item. In many setups, a tagged internal link starts a new session and overwrites the original source. A visitor who came from an ad would then be credited to "homepage banner".
Many ad platforms also offer auto-tagging, such as Google Ads' gclid. Auto-tagging and manual UTM values can coexist, but check your analytics settings to see which one wins when both are present.
Carry UTM values into the lead form
Analytics counts visits and conversions in aggregate. Your lead list, however, is a separate record. To see which campaign each lead came from, copy the UTM values into hidden form fields. A hidden field is a form input the visitor never sees, but which is submitted with the form.
First, add hidden fields named utm_source, utm_medium, and utm_campaign in your builder's form settings. The names must match the script exactly. Then add this snippet in the builder's custom code area, in the page footer or before the closing </body> tag.
const params = new URLSearchParams(window.location.search);
['utm_source', 'utm_medium', 'utm_campaign'].forEach(function (name) {
const field = document.querySelector('input[name="' + name + '"]');
if (field && params.has(name)) {
field.value = params.get(name);
}
});The script reads the query string, finds each matching input, and fills it in. If a field is missing or a parameter is absent, the script skips it and does nothing.
Common errors and edge cases
- Redirects drop the query string. If your domain redirects
httptohttpsor non-wwwtowww, a poorly configured redirect can discard everything after?. Click your tagged link and check that the final address still contains the parameters. - Hidden fields are empty. The form may load after the script runs, or the field names differ in case or spelling. Inspect the form with browser developer tools and confirm the
nameattributes. - Values are case-sensitive.
Spring_Saleandspring_salesplit the data. Fix this at the source with the convention above. - Link shorteners. Shorteners are fine as long as they preserve the full destination. Tag first, then shorten.
- Anchors. A
#sectionfragment must come after the query string:...?utm_source=x#pricing. Placed before it, the parameters are not sent. - Consent. If a visitor declines analytics cookies, the UTM values may not be recorded by analytics. The hidden-field approach still works, but treat the values as marketing data under your privacy notice (covered in a later chapter).
- Session rollover. A visitor who returns later through another link is attributed to the new link. The hidden fields record only the link used on that visit.
Check your links before launch
- Open each tagged link in a private browser window.
- Confirm the page loads normally and the address bar still shows the parameters.
- In your analytics tool's real-time view, confirm the visit appears with the expected source, medium, and campaign.
- Submit the form with test data and confirm the hidden fields arrive in your destination.
- Record each link in your tracking sheet with its channel and date.
Exercise
A teammate tags a link as ?utm_source=Facebook&utm_medium=Social Ad&utm_campaign=Spring Sale. Name three problems with it and write a corrected version for the page https://www.example.com/spring-sale.
Show answer
The values mix capital letters, so Facebook may split from facebook in reports. Social Ad and Spring Sale contain spaces, which become %20 and are easy to mistype. Social Ad also does not follow a fixed medium list, so use a standard value such as social or cpc. A corrected link is https://www.example.com/spring-sale?utm_source=facebook&utm_medium=social&utm_campaign=spring_sale.
Exercise
Your form has hidden fields named utm_source, utm_medium, and utm_campaign, but leads arrive with them empty. The tagged link works in analytics. List two things to check.
Show answer
First, confirm the hidden fields' name attributes exactly match the names in the script, including case. Second, confirm the script runs after the form exists on the page, for example by placing it in the footer code area, and that no redirect strips the query string before the page loads. Browser developer tools show both the input names and the final address.