Sub topic 1
Why tag campaign links
Prerequisites
This chapter assumes you understand URLs and query strings, and that you have a GA4 property with the web data stream installed. You should also be able to open reports in GA4.
Why tag campaign links
Google Analytics 4 (GA4) records visits to your website. By default, it can see that a visit came from a search engine or a social site. It cannot tell which email, ad, or post sent the visit unless the link carries that information.
Campaign tagging adds that information to the link itself. Without tags, newsletter traffic may show up as "direct" or "referral," and you cannot compare campaigns against each other.
The UTM parameters and what each one means
A UTM parameter is a key-value pair added to the end of a URL. UTM stands for Urchin Tracking Module, the name of the original tool. Three parameters form the core set:
utm_source: where the traffic comes from, such asnewsletterorgoogle.utm_medium: the channel type, such asemail,cpc, orsocial.utm_campaign: the campaign name, such asspring_sale_2026.
utm_content is optional. It separates links inside the same message, such as a hero button and a footer link.
Building a tagged URL correctly
Add a ? before the first parameter and & between the rest. Values with spaces must be encoded. A helper function avoids manual mistakes:
function buildCampaignUrl(baseUrl, { source, medium, campaign, 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_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',
}));The output is:
https://example.com/spring-sale?utm_source=newsletter&utm_medium=email&utm_campaign=spring_sale_2026&utm_content=hero_buttonExercise
Which parameter would you add to tell apart two links in the same email?
Show answer
utmcontent, for example herobutton for one link and footer_link for the other.