If you’re sending traffic between multiple domains (like a marketing site and a checkout site, or a blog and a separate booking platform), your UTM parameters usually get lost the moment a visitor clicks a link to the other domain. This script fixes that by automatically appending your tracking parameters to outbound links.
What This Does
This script scans all links on your page, checks if any of them point to domains you specify, and automatically appends the current page’s UTM parameters (or any other query params you choose) to those links. So if someone lands on your site from a Facebook ad and then clicks through to your booking domain, the tracking data goes with them.
The Code
javascript
<script>
(function() {
var domainsToDecorate = [
'domain1.com', //add or remove domains (without https or trailing slash)
'domain2.net'
],
queryParams = [
'utm_medium', //add or remove query parameters you want to transfer
'utm_source',
'utm_campaign',
'something_else'
]
// do not edit anything below this line
var links = document.querySelectorAll('a');
// check if links contain domain from the domainsToDecorate array and then decorates
for (var linkIndex = 0; linkIndex < links.length; linkIndex++) {
for (var domainIndex = 0; domainIndex < domainsToDecorate.length; domainIndex++) {
if (links[linkIndex].href.indexOf(domainsToDecorate[domainIndex]) > -1 && links[linkIndex].href.indexOf("#") === -1) {
links[linkIndex].href = decorateUrl(links[linkIndex].href);
}
}
}
// decorates the URL with query params
function decorateUrl(urlToDecorate) {
urlToDecorate = (urlToDecorate.indexOf('?') === -1) ? urlToDecorate + '?' : urlToDecorate + '&';
var collectedQueryParams = [];
for (var queryIndex = 0; queryIndex < queryParams.length; queryIndex++) {
if (getQueryParam(queryParams[queryIndex])) {
collectedQueryParams.push(queryParams[queryIndex] + '=' + getQueryParam(queryParams[queryIndex]))
}
}
return urlToDecorate + collectedQueryParams.join('&');
}
// a function that retrieves the value of a query parameter
function getQueryParam(name) {
if (name = (new RegExp('[?&]' + encodeURIComponent(name) + '=([^&]*)')).exec(window.location.search))
return decodeURIComponent(name[1]);
}
})();
</script>JavaScriptSetup in GTM
Step 1: Create a Custom HTML Tag
- Go to Tags → New
- Select Custom HTML
- Paste the code above
- Name it “Cross-Domain Link Decorator”
Step 2: Edit the Configuration
Before saving, update two arrays in the script:
domainsToDecorate — list the domains you want to append params to:
javascript
var domainsToDecorate = [
'booking.yoursite.com',
'checkout.yoursite.com'
]JavaScriptqueryParams — list which parameters should carry over:
javascript
var queryParams = [
'utm_medium',
'utm_source',
'utm_campaign',
'utm_term',
'utm_content'
]JavaScriptStep 3: Set the Trigger
Use a trigger like DOM Ready or Window Loaded rather than Page View, since the script needs the page’s links to already be rendered before it runs.
Step 4: Save and Publish
Save the tag, test it, then publish your container.
How It Works
- The script grabs every
<a>link on the page - For each link, it checks if the link’s URL contains one of your specified domains
- It skips anchor links (URLs with
#) since those usually point to sections on the same page - If a match is found, it reads the current page’s UTM parameters from the URL
- It appends those parameters to the matching link’s href
So if a visitor is on yoursite.com/?utm_source=facebook&utm_campaign=summer_sale and clicks a link to booking.yoursite.com, the link automatically becomes booking.yoursite.com/?utm_source=facebook&utm_campaign=summer_sale.
Why You Need This
Without link decoration, here’s what typically happens:
- User clicks a Facebook ad → lands on your marketing site with UTM params
- User clicks through to your separate booking/checkout domain
- UTM params are lost because they only existed on the first domain
- Your booking domain shows “direct traffic” instead of the actual source
With this script, the params travel with the user across domains, so your conversion tracking stays accurate.
Testing
- Enable GTM Preview Mode
- Visit your page with test UTM parameters:
yoursite.com/?utm_source=test&utm_medium=test - Inspect the links pointing to your specified domains
- Confirm the UTM parameters were appended to the href
- Click through and verify the params appear on the destination domain
Common Use Cases
- Marketing site → Booking platform: Pass campaign data from your homepage to a separate scheduling tool
- Blog → E-commerce store: Track which blog posts drive purchases on your store domain
- Landing page → CRM form: Maintain attribution when directing users to a hosted form on another domain
Quick Notes
This only decorates <a> tags: If your site uses JavaScript-based navigation or buttons that aren’t standard links, you’ll need additional handling.
Run timing matters: Since this only decorates links present in the DOM at execution time, dynamically loaded links (like those added after an AJAX call) won’t get decorated unless you re-run the script or trigger it again.
Domain matching is partial: The script uses indexOf(), so make sure your domain strings are specific enough to avoid accidentally matching unintended URLs.
That’s it. Once set up, your UTM parameters and click IDs will carry across domains automatically, keeping your attribution data accurate no matter how many domains are involved in your customer journey.

0 Comments