Introduction

When running multi-step marketing funnels, it’s crucial to retain UTM parameters from the first touchpoint all the way through to conversion. However, transferring these parameters between different domains or pages on the same domain isn’t always straightforward. In this guide, we’ll walk you through how to capture, store, and pass UTM parameters across both same-domain and cross-domain URLs using practical methods, and you can get screenshots with a step guide and code snippets.

Step 1: Create a GTM Variable to Capture UTM Parameters

Before storing or passing data, you must first capture UTM parameters from the URL. For better capturing, each UTM parameter should have a distinct GTM variable.

  • Go to GTM → Variables → New (User-Defined Variable)
  • Name: UTM Parameters – URL
  • Type: URL
  • Component Type: Query
  • Query Key:
    For utm_source, enter utm_source
    Repeat for utm_medium, utm_campaign, utm_term, utm_content
    (Create separate variables for each UTM parameter)

Create a GTM Variable to Capture UTM Parameters

Step 2: Store UTM Parameters in a Cookie

After capturing the UTM parameters, you will have to store them so that they persist across all your domains. Having consistent parameters makes cross-domain UTM monitoring and multi-step funnel attribution easier. We’ll use a Custom HTML Tag to store UTM parameters in a cookie when they are present.

A. Create a Cookie Storage Tag

  • Go to GTM → Tags → New → Custom HTML Tag
  • Name: Store UTM Parameters in Cookie
  • Trigger: All Pages (or a custom trigger if needed)
  • Paste this script:

<script>
// Enhanced UTM capture with local storage fallback
(function() {
var urlParams = new URLSearchParams(window.location.search);
var utmParams = {};

// Capture standard + custom UTMs
[‘utm_source’, ‘utm_medium’, ‘utm_campaign’, ‘utm_term’, ‘utm_content’].forEach(function(param) {
if (urlParams.has(param)) utmParams[param] = urlParams.get(param);
});

if (Object.keys(utmParams).length > 0) {
// 1. Set cross-domain cookie (30 min expiry)
var expires = new Date();
expires.setTime(expires.getTime() + 30 * 60 * 1000);
document.cookie = ‘pm_utm=’ + encodeURIComponent(JSON.stringify(utmParams)) +
‘; expires=’ + expires.toUTCString() +
‘; path=/’ +
‘; domain=.powermonkeyfitness.com’ +
‘; SameSite=Lax; Secure’;

// 2. Set localStorage (persists across same-domain navigations)
localStorage.setItem(‘pm_utm’, JSON.stringify(utmParams));
console.log(‘UTMs stored:’, utmParams);
}
})();
</script>

Store UTM Parameters in a Cookie

Step 3: Retrieve UTM Parameters on the Second Domain

After storage, we ensure the stored UTM parameters are attached to links connected to the secondary domain. Therefore, every link connected to another domain has a UTM trail for better cross-domain UTM tracking. When a user clicks a link to the second domain, we’ll append the stored UTM parameters.

  • Go to GTM → Tags → New → Custom HTML Tag
  • Name: Pass UTM Parameters to Second Domain
  • Trigger: All Pages (or a custom trigger if needed)
  • Paste this script:

<script>
// Enhanced link processor with URL pattern matching
(function() {
function getUTMs() {
try {
// 1. Check cookie first
var cookie = document.cookie.split(‘;’).find(function(c) {
return c.trim().startsWith(‘pm_utm=’);
});
if (cookie) return JSON.parse(decodeURIComponent(cookie.split(‘=’)[1]));

// 2. Check localStorage
var localData = localStorage.getItem(‘pm_utm’);
if (localData) return JSON.parse(localData);

return null;
} catch(e) {
console.warn(‘UTM parse error:’, e);
return null;
}
}

// Process links after slight delay to ensure cookie availability
setTimeout(function() {
var utmParams = getUTMs();
if (!utmParams) return;

console.log(‘Applying UTMs:’, utmParams);

// Match all links to your domains (including relative paths)
var links = document.querySelectorAll(‘a[href*=”powermonkeyfitness.com”], a[href*=”powermonkeycamp.com”], a[href^=”/”]’);

links.forEach(function(link) {
try {
// Skip if no href or non-HTTP link
if (!link.href || link.href.startsWith(‘javascript:’)) return;

// Handle relative paths
var fullUrl = link.href.startsWith(‘http’) ? link.href :
‘https://’ + window.location.host + (link.href.startsWith(‘/’) ? link.href : ‘/’ + link.href);

var url = new URL(fullUrl);
var modified = false;

// Add missing UTMs
Object.keys(utmParams).forEach(function(key) {
if (!url.searchParams.has(key)) {
url.searchParams.set(key, utmParams[key]);
modified = true;
}
});

if (modified) {
link.href = url.toString();
console.log(‘Modified link:’, link.href);
}
} catch(e) {
console.warn(‘Error processing link:’, link.href, e);
}
});
}, 100); // Small delay to ensure cookie availability
})();
</script>

Retrieve UTM Parameters on the Second Domain

Step 4: Testing & Debugging

Confirming that your UTM tracking GTM setup is working accurately is essential. If the set-up is effective, you will note that the correct UTM data will be passed on from click to conversion.

Test on the First Domain

  • Visit https://domain1.com/?utm_source=test&utm_medium=email
  • Check if the cookie/localStorage is set (use Chrome DevTools → Application tab).

Test on the Second Domain

  • Click a link from domain1.com to domain2.com.
  • Verify that the URL now includes the UTM parameters (e.g., domain2.com/?utm_source=test&utm_medium=email).

Conclusion

The accuracy of your marketing data depends on the correct transfer and preservation of UTM parameters. Whether you are guiding your users through landing pages or sub-domains, ensuring your UTM parameters are transferred correctly will help you monitor your client’s complete journey without missing out on any information. Most importantly, by leveraging practices such as testing and debugging, retrieving UTM parameters on the second domain, storing UTM parameters in a cookie, and creating a GTM Variable to capture UTM parameters, you can make the most of your analytics and make informed decisions.


Leave a Reply