Sub topic 3
Building a Tagged URL Correctly
This chapter assumes you know what a query string is, that you have a GA4 property with a web data stream installed, and that you know the purpose of the UTM parameters from the previous chapter. Here the focus is construction: how to assemble a URL that GA4 reads correctly, and how it breaks.
Anatomy of a tagged URL
A tagged URL is an ordinary landing page URL with campaign parameters added to its query string. The parts are:
- The base URL, such as
https://example.com/spring-sale. - A
?that starts the query string. - Key-value pairs written as
key=value. &between pairs.- An optional
#fragmentat the very end.
Here is a complete example with the three core parameters plus utm_content:
https://example.com/spring-sale?utm_source=newsletter&utm_medium=email&utm_campaign=spring_sale_2026&utm_content=hero_buttonEach parameter has a job:
| Parameter | Required | Answers the question | Example |
|---|---|---|---|
| `utm_source` | Yes | Where did the visitor come from? | `newsletter` |
| `utm_medium` | Yes | What kind of channel was it? | `email` |
| `utm_campaign` | Yes | Which campaign was it? | `spring_sale_2026` |
| `utm_content` | No | Which link or creative was clicked? | `hero_button` |
| `utm_term` | No | Which keyword or audience was targeted? | `running_shoes` |
| `utm_id` | No | Which campaign ID does this map to? | `cmp_0426` |
Only the first three are needed for a link to be useful. Google's own URL builder treats them as required. GA4 will still process a URL that has only some of them, but the result is often a confusing source or medium (see the pitfalls below).
Rules for a valid query string
Most construction bugs come from a small set of syntax rules.
One question mark
The first parameter is introduced with ?. Every later parameter is introduced with &. If the base URL already has a query string, append your parameters with &:
https://example.com/products?category=shoes&utm_source=newsletter&utm_medium=email&utm_campaign=spring_sale_2026Writing a second ? produces a URL where the parameter name becomes part of the previous value. The tracking value is then lost or garbled.
The fragment goes last
A fragment is the part after #. Browsers never send it to the server, and it ends the query string. Parameters placed after the # are not part of the query string, so they are not reliably read as UTM parameters.
Wrong: https://example.com/page#pricing?utm_source=newsletter&utm_medium=email&utm_campaign=spring
Correct: https://example.com/page?utm_source=newsletter&utm_medium=email&utm_campaign=spring#pricingEncode special characters
Spaces, ampersands, plus signs, and non-ASCII characters have special meaning in URLs or are not allowed in them. If a value contains one, it must be percent-encoded. A space becomes %20 (or + in form encoding), and & becomes %26. An unencoded & inside a campaign name splits it into two parameters:
Wrong: utm_campaign=q1 sales&promos
Result: utm_campaign = "q1 sales", plus a stray parameter named "promos"The simplest defense is to avoid special characters in values entirely. Use lowercase letters, digits, hyphens, and underscores. Then no encoding is needed.
Keep parameter names exact
Parameter names are case-sensitive in practice. GA4 looks for utm_source, not UTM_Source or utm-source. A misspelled name is treated as an unrelated parameter and ignored.
Do not tag every link
Only tag links that point to your site from outside it: email, social posts, ads on other platforms, partner sites, QR codes. Do not add UTM parameters to links between pages of your own site. That is covered in the pitfalls chapter, but the construction reason is simple: a UTM parameter on an internal link starts a new session and overwrites the original attribution.
Building URLs in code
Hand-typing URLs invites typos. A small helper function enforces the rules once. This one requires the three core parameters, lowercases values, replaces whitespace with underscores, and lets URLSearchParams handle encoding:
function buildCampaignUrl(baseUrl, { source, medium, campaign, id, term, content }) {
if (!source || !medium || !campaign) {
throw new Error('source, medium and campaign are required');
}
const url = new URL(baseUrl);
const params = {
utm_source: source,
utm_medium: medium,
utm_campaign: campaign,
utm_id: id,
utm_term: term,
utm_content: content,
};
for (const [key, value] of Object.entries(params)) {
if (value) {
url.searchParams.set(key, value.trim().toLowerCase().replace(/\s+/g, '_'));
}
}
return url.toString();
}
console.log(buildCampaignUrl('https://example.com/spring-sale', {
source: 'newsletter',
medium: 'email',
campaign: 'Spring Sale 2026',
content: 'hero button',
}));
// https://example.com/spring-sale?utm_source=newsletter&utm_medium=email&utm_campaign=spring_sale_2026&utm_content=hero_buttonSeveral details in this function matter:
new URL(baseUrl)throws if the base is not a valid absolute URL. This catches a missinghttps://early.searchParams.setreplaces an existing value rather than adding a duplicate. If the base URL already containedutm_source, the helper overwrites it.- Existing non-UTM parameters, such as
category=shoes, are preserved, and the&and?characters are handled for you. - A fragment in the base URL stays at the end of the output.
searchParamspercent-encodes any characters that remain, so a value likecaféis encoded correctly.
Limits of the helper
The helper normalizes input, which is a policy choice. Lowercasing every value is right for most teams, but it would corrupt a case-sensitive utm_id if your IDs from an ad platform or CRM mix cases. Check this before passing IDs through toLowerCase().
Also, the helper does not check values against an approved list. If two teammates write newsletter and news_letter, both pass. The next chapter on naming conventions addresses that with controlled vocabularies.
What happens to the URL on the way to GA4
Building a correct URL is not enough. The parameters must survive the trip from the click to the page where GA4 loads. The browser must end up on a landing page whose address still contains the utm_ parameters, because the GA4 tag reads them from document.location when the page loads.
Common places where parameters get lost:
- Redirects that drop the query string. A server rule that redirects
/spring-saleto/sale/springwithout forwarding the query string removes your tags. Redirect rules must pass the query string through. - Link shorteners and tracking wrappers. Most preserve the destination URL exactly, but some email platforms rewrite links for click tracking. Test the final landing URL, not the link as you wrote it.
- Login or consent walls. If a visitor must authenticate first and the flow does not return them to the original URL with its parameters, the tags are gone before the GA4 tag fires.
- Single-page apps that rewrite the URL early. If client-side routing strips the query string before the GA4 tag reads it, no campaign data is collected. This depends on tag load order.
- Mixed
httpandhttpsredirects. A redirect from one to the other is fine if the query string is forwarded, but it is a common spot for rules that are not.
Verify the landing URL in the browser
After clicking a tagged link from its real source, such as an actual email, open the browser console on the landing page and run this snippet. It confirms the parameters arrived intact before you look in GA4:
const params = new URLSearchParams(window.location.search);
const utm = Object.fromEntries(
[...params].filter(([key]) => key.startsWith('utm_'))
);
console.log(utm);
// { utm_source: 'newsletter', utm_medium: 'email', utm_campaign: 'spring_sale_2026', ... }If the object is empty, the parameters were lost before the page loaded. The cause is in the redirect chain or link rewriting, not in GA4.
Choosing which optional parameters to add
Add utm_content or utm_term only when you will report on them.
- Use
utm_contentto tell apart two links in the same message, such ashero_buttonandfooter_link, or to label A/B variants likesubject_aandsubject_b. - Use
utm_termfor a paid keyword, or for an audience label when you lack a keyword. Search ad platforms usually fill this in themselves. - Use
utm_idwhen you need to join GA4 data with a campaign ID from another system. The chapter on additional parameters covers this.
Extra parameters make URLs longer, which matters for SMS, printed QR codes, and some social platforms. A long URL also gives more room for typos. Include a parameter only if a report will use it.
A construction checklist
Before publishing a link, go through these checks:
- The base URL is absolute, uses
https, and points to the final landing page, not to a URL that redirects. utm_source,utm_medium, andutm_campaignare all present and non-empty.- The first parameter follows
?and the rest follow&. - Parameter names are lowercase and spelled exactly.
- Values use the team's naming convention, with no spaces or special characters.
- The
#fragment, if any, is at the very end. - The link is for traffic from outside your site.
- Clicking it from the real channel lands on a page whose address still shows the parameters.
Edge cases worth knowing
- Duplicate parameters. A URL with two
utm_sourcevalues, such as?utm_source=a&utm_source=b, is ambiguous. Browsers'URLSearchParams.getreturns the first, while other tools may return the last. Avoid duplicates by usingsetinstead ofappend. - Empty values.
utm_campaign=with nothing after the equals sign is treated as missing. Required parameters left empty give you incomplete attribution. - Case differences in values.
Emailandemailbecome separate rows in reports, because GA4 treats values as case-sensitive strings. The helper lowercases for this reason. - Very long values. Keep each value short. GA4 limits the length of dimension values, and overly long campaign names are hard to read in tables anyway.
- Personal data. Never put email addresses, names, or other personal data in a parameter. The URL is stored in reports and logs and is visible to the visitor, and Google's terms prohibit sending personally identifiable information to GA4.
Exercise
A teammate sends you this link for an email campaign: https://example.com/offer#pricing?utm_source=newsletter&utm_medium=email&utm_campaign=autumn promo&utm_content=cta&button. List every construction problem and write the corrected URL.
Show answer
There are three problems. First, the parameters come after the #pricing fragment, so they are not part of the query string. Second, the campaign value autumn promo contains a space, which should be replaced by an underscore (autumn_promo) or encoded. Third, the unencoded & in cta&button splits the value, so utm_content becomes cta and a stray parameter button appears. Corrected URL: https://example.com/offer?utm_source=newsletter&utm_medium=email&utm_campaign=autumn_promo&utm_content=cta_button#pricing.
Exercise
You build a tagged link with the helper function, and the URL looks right. A visitor clicks it from your email platform, but GA4 shows the session as direct. What is the first thing to check, and why?
Show answer
Check whether the landing page URL still contains the utm_ parameters after the click. Run the console snippet that reads window.location.search on the landing page. If the parameters are missing, a redirect, link-rewriting step, or login flow dropped the query string before the GA4 tag read it, so GA4 had no campaign data and treated the visit as direct. The fix belongs in the redirect or email platform settings, not in the URL builder.