When managing multiple websites, preserving attribution across domains often requires passing tracking parameters such as gclid, utm_*, or fbclid through outbound links. However, decorating every link indiscriminately can create privacy concerns, clutter URLs, and introduce unnecessary processing.
This guide explains how to implement conditional link decoration using JavaScript, ensuring that tracking parameters are appended only when there is a clear attribution signal.
What This Setup Achieves
The script decorates outbound links only if at least one of the following conditions is met:
- A known first-party platform cookie exists
- The landing page URL contains advertising or campaign parameters
If neither condition is satisfied, the script exits without modifying any links.
This approach keeps URLs clean while maintaining accurate attribution for paid and campaign-driven traffic.
High-Level Logic Overview
- Detect specific first-party cookies
- Detect tracking parameters in the page URL
- Exit early if no attribution signal is present
- Scan all anchor links on the page
- Decorate only links pointing to approved destinations
- Append missing tracking parameters safely
Step 1: Create a Cookie Detection Helper
The script starts with a helper function that checks whether a specific cookie exists in the browser.
<script>
(function () {
function hasCookie(name) {
var cookies = document.cookie.split(';');
for (var i = 0; i < cookies.length; i++) {
if (cookies[i].trim().indexOf(name + '=') === 0) {
return true;
}
}
return false;
}
This function allows you to conditionally enable tracking behavior based on identifiers set by your own platform, such as authentication, session, or consent cookies.
Step 2: Check for First-Party Platform Cookies
Next, define a list of first-party cookies that indicate a known user or platform interaction.
var platformCookies = ['platform_cookie_name', 'another_cookie_name'];
var hasPlatformCookie = false;
for (var i = 0; i < platformCookies.length; i++) {
if (hasCookie(platformCookies[i])) {
hasPlatformCookie = true;
break;
}
}
You should replace these placeholders with cookies relevant to your own stack.
Step 3: Detect Tracking Parameters in the URL
The script then checks whether the current page URL contains known advertising or campaign parameters.
var trackingParams = [
'gclid',
'gbraid',
'wbraid',
'utm_source',
'utm_medium',
'utm_campaign',
'fbclid',
'msclkid'
];
var urlParams = new URLSearchParams(window.location.search);
var hasTrackingParam = false;
for (var i = 0; i < trackingParams.length; i++) {
if (urlParams.has(trackingParams[i])) {
hasTrackingParam = true;
break;
}
}
This ensures link decoration only occurs for users who arrived via a trackable marketing channel.
Step 4: Exit Early When No Attribution Signal Exists
If neither a platform cookie nor a tracking parameter is present, the script stops immediately.
if (!hasPlatformCookie && !hasTrackingParam) return;
This early exit is essential for performance optimization and privacy-conscious implementations.
Step 5: Define Allowed Destination Domains
Only links pointing to explicitly approved destinations should be decorated.
var domainsToDecorate = ['example-domain-1.com', 'example-domain-2.com'];
Using a whitelist prevents tracking parameters from leaking to unintended third-party sites.
Step 6: Identify Eligible Links on the Page
The script scans all anchor elements and filters out links that should not be modified.
var links = document.querySelectorAll('a[href]');
for (var i = 0; i < links.length; i++) {
var link = links[i];
if (link.href.indexOf('#') !== -1) continue;
Fragment-only links are ignored to avoid breaking in-page navigation.
Step 7: Decorate Matching Links Safely
For each eligible link, the script appends missing tracking parameters from the current page URL.
var matchesDomain = false;
for (var j = 0; j < domainsToDecorate.length; j++) {
if (link.href.indexOf(domainsToDecorate[j]) !== -1) {
matchesDomain = true;
break;
}
}
if (!matchesDomain) continue;
try {
var linkUrl = new URL(link.href, window.location.origin);
for (var k = 0; k < trackingParams.length; k++) {
var param = trackingParams[k];
if (urlParams.has(param) && !linkUrl.searchParams.has(param)) {
linkUrl.searchParams.set(param, urlParams.get(param));
}
}
link.href = linkUrl.toString();
} catch (e) {
console.warn('Failed to decorate link:', link.href);
}
}
})();
</script>
The URL API ensures parameters are appended correctly and avoids malformed URLs.
When to Use This Approach
This conditional link decoration pattern is ideal for:
- Cross-domain attribution between owned properties
- Affiliate or comparison-style traffic flows
- Paid media landing pages
- Consent-aware tracking architectures
It provides strong attribution control without unnecessary data propagation.
Summary
Conditional link decoration allows you to preserve marketing attribution only when it matters, without polluting URLs or violating data minimization principles. By combining cookie detection, URL parameter checks, and domain whitelisting, this setup delivers a robust and privacy-conscious solution for cross-domain tracking.
This implementation is flexible, extensible, and suitable for most modern analytics stacks.
