javascript get utm codes

Unlock JavaScript UTM Tracking: Extract & Use Campaign Parameters

By Richard Taylor
AD

In the dynamic world of web development, tracking user behavior and source of traffic is not only beneficial but a necessity. Marketing teams use UTM parameters (Urchin Traffic Monitor) to track the success of online campaigns, enabling them to make informed decisions. But how do we access these UTM codes on the client-side? This is where JavaScript comes into play. Let's delve into how to retrieve UTM codes using JavaScript.

Before we dive into the code, it's essential to understand how UTM parameters work. UTM codes are appended to the end of your URL, like so: `https://www.example.com/?utm_source=newsletter&utm_medium=email&utm_campaign=march_sale`. The values after the `=` are what we'll be extracting using JavaScript.

Accessing Query String Parameters

To access UTM codes, we first need to retrieve the query string from the URL. The URLSearchParams interface of the URL API allows us to work with the URL query string. Here's how you can create a new instance of URLSearchParams:

AD
Amazing Javascript Game Source Codes for Free
Amazing Javascript Game Source Codes for Free
an image of a computer screen with text on it and a window in the background
an image of a computer screen with text on it and a window in the background
JavaScript Functions Made Easy: Quick Guide for Developers! ⚙️
JavaScript Functions Made Easy: Quick Guide for Developers! ⚙️

const urlParams = new URLSearchParams(window.location.search);

Extracting UTM Parameters

After creating the URLSearchParams object, you can extract each UTM parameter as follows:

Login Page In Javascript With Source Code
Login Page In Javascript With Source Code
JavaScript|DOM操作(要素の取得, 追加, 削除) - わくわくBank
JavaScript|DOM操作(要素の取得, 追加, 削除) - わくわくBank
GeekDad Daily Deal: 2018 Essential JavaScript Coding Bundle
GeekDad Daily Deal: 2018 Essential JavaScript Coding Bundle

const utmSource = urlParams.get('utm_source');

This will return the value of the `utm_source` parameter, which in the URL above, would be 'newsletter'. Repeat the above process for `utm_medium` and `utm_campaign`:

AD

const utmMedium = urlParams.get('utm_medium');

change body background with js every second
change body background with js every second
a man standing on top of a hill holding a yellow box in his hand and the words, okay control / inventory / us plugin
a man standing on top of a hill holding a yellow box in his hand and the words, okay control / inventory / us plugin
an image of a web page with different types of text and numbers on the screen
an image of a web page with different types of text and numbers on the screen
Javascript console tips & tricks
Javascript console tips & tricks
Using ECMAScript 6 (ES6 / ES2015) in Node.js by Sean Massa
Using ECMAScript 6 (ES6 / ES2015) in Node.js by Sean Massa
an image of a computer screen with the words code crumbs
an image of a computer screen with the words code crumbs

const utmCampaign = urlParams.get('utm_campaign');

Handling Missing Parameters

It's not uncommon for some UTM parameters to be missing. To handle this, you can provide a default value or a fallback mechanism.

For instance, if you want to provide a default value of 'Unknown' for missing parameters, you can modify the previous code snippets as follows:

const utmSource = urlParams.get('utm_source') || 'Unknown';

Logging and Using UTM Codes

Now that you've successfully retrieved the UTM codes, you can use them for various purposes. You might want to log them to the console for debugging or use them in your application.

To log the UTM codes to the console:

console.log('UTM Source:', utmSource, 'UTM Medium:', utmMedium, 'UTM Campaign:', utmCampaign);

Keep in mind, logging UTM codes is a great way to understand user behavior but should not be the only method. Consider sending this data to your analytics platform or combining it with other data sources for more comprehensive user tracking.

In the end, understanding how users reach your website and engaging with them based on this understanding is key to successful online marketing. JavaScript enables you to utilize this data on the client-side, enhancing your users' experience and your marketing strategy.

an image of a computer screen with code numbers on it and the text,'to do this is a different file
an image of a computer screen with code numbers on it and the text,'to do this is a different file
the back side of a computer screen with an image of a black and purple background
the back side of a computer screen with an image of a black and purple background
Hanging Up On Callbacks: Generators in ECMAScript 6
Hanging Up On Callbacks: Generators in ECMAScript 6
different types of logos and their meanings
different types of logos and their meanings
GeekDad Academy: The Essential JavaScript Coding Bundle
GeekDad Academy: The Essential JavaScript Coding Bundle
Duomly - Online School
Duomly - Online School
the back cover of a book with text on it and an image of a blue background
the back cover of a book with text on it and an image of a blue background
Simple Javascript Tagging
Simple Javascript Tagging
Dynamic Class Toggle in DOM
Dynamic Class Toggle in DOM
an image of a clock and the word, learn how to use jlp plugins
an image of a clock and the word, learn how to use jlp plugins
JavaScript You Need to Know for a Job
JavaScript You Need to Know for a Job
Javascript Code For Anagram Check, Coding, Programming Languages
Javascript Code For Anagram Check, Coding, Programming Languages
Guide to Linting JavaScript with JSHint
Guide to Linting JavaScript with JSHint
GeekDad Academy: JavaScript from Beginner to Expert
GeekDad Academy: JavaScript from Beginner to Expert
AD