In digital marketing and performance analysis, tracking parameters like UTM parameters and Click IDs play a critical role in better understanding website visitors. In this article, I will discuss how we can store parameters visible in the address bar during a visitor's first visit and use them later. I will share the complete script at the end of the article, but my recommendation is to generate your own code with the help of AI. Small details might be overlooked, but this way, it will be easier for you to grasp the logic.
This script's main purpose is to capture tracking parameters (e.g., utm_source, utm_medium, gclid, fbclid) brought with the URL during a visitor's first visit and store them in local storage and session storage. This way, these data do not get lost as the user navigates your site and can be used when needed. Otherwise, tracking parameters appearing in the address bar on the first page visit will be lost on subsequent page visits.
At the beginning of the code, you define an array of parameters:
Here, we have chosen the primary UTM parameters and the two most frequently used ad click IDs. You can include as many parameters as you wish. It's up to you which queries within the URL you want to collect. Let's say you add a parameter adgroup=generic to your ad campaign. By adding 'adgroup' to the array above, you will also store the value corresponding to this query.
gclid = Google Click IDfbclid = Facebook Click IDWhen a user arrives at the site from a link, the script searches for these parameters in the URL and collects what it finds into an object. Afterwards, we can store them in two different ways and track separate sources.
During the first visit, these parameters are stored in localStorage for 30 days. This means that when a user enters the site via a campaign link, this information is not lost. After 30 days, it is automatically cleared. If we were to remove this 30-day limit, these parameters would remain in the browser until the user clears their browser data, but since most ad platforms have an attribution window of 28-30 days, keeping them for this period is beneficial.
In the script I shared, you can update the line below to determine the storage duration in local storage.
Additionally, parameters are also written to sessionStorage. This allows parameters to be easily accessible until the user closes the tab. In each new session, the initially found parameters are stored again. Here, you can set up a special tracking system to compare the first and last sessions.
If you wish, you can access the captured parameters from other scripts via window.trackingParams.
<body>…</body>:See you in the next article.