Introduction
In this blog post, I’ll show you how to implement cross-domain tracking for Meta Pixel using Google Tag Manager (GTM) and custom JavaScript, ensuring Meta correctly attributes conversions across multiple domains.
Why This Matters
✅ More optimized ad delivery and conversions
How It Works
This helps Meta identify the same user session and attribute conversions accurately.
Requirements
Before setting this up, make sure:
Both domains share the same GTM container ID
Meta Pixel is installed on both sites (via GTM or directly)
Step-by-Step Implementation
Step 1: Append fbclid/_fbc/_fbp to Outbound Links (Site A)
- Tag Type: Create A new Custom HTML
- Trigger: All Pages (Safe - Script waits for DOM automatically).
Replace siteB.com with your target domain.
This script reads tracking parameters (fbclid, _fbc, _fbp) from cookies and appends them to outbound links leading to your target domain(s)
<script>
(function () {
// === CONFIG ===
var TARGET_DOMAIN = 'siteB.com'; // <-- Change your destination domain
var KEYS = ['fbclid', '_fbc', '_fbp'];
function getCookie(name) {
var m = document.cookie.match(new RegExp('(?:^|; )' + name + '=([^;]*)'));
return m ? decodeURIComponent(m[1]) : '';
}
function setQueryParam(href, key, value) {
if (!value) return href;
var hash = '';
var hashIndex = href.indexOf('#');
if (hashIndex > -1) {
hash = href.substring(hashIndex);
href = href.substring(0, hashIndex);
}
var qIndex = href.indexOf('?');
var base = href;
var qs = '';
if (qIndex > -1) {
base = href.substring(0, qIndex);
qs = href.substring(qIndex + 1);
}
var parts = qs ? qs.split('&') : [];
var found = false;
for (var i = 0; i < parts.length; i++) {
var kv = parts[i].split('=');
if (decodeURIComponent(kv[0]) === key) {
parts[i] = encodeURIComponent(key) + '=' + encodeURIComponent(value);
found = true;
break;
}
}
if (!found) parts.push(encodeURIComponent(key) + '=' + encodeURIComponent(value));
var newQs = [];
for (var j = 0; j < parts.length; j++) {
if (parts[j]) newQs.push(parts[j]);
}
return base + (newQs.length ? ('?' + newQs.join('&')) : '') + hash;
}
function rewriteLinks() {
var links = document.getElementsByTagName('a');
for (var i = 0; i < links.length; i++) {
var a = links[i];
// hostname resolve (anchor trick)
var parser = document.createElement('a');
parser.href = a.href;
var host = parser.hostname || '';
if (host.indexOf(TARGET_DOMAIN) > -1) {
for (var k = 0; k < KEYS.length; k++) {
var val = getCookie(KEYS[k]);
if (val) {
a.href = setQueryParam(a.href, KEYS[k], val);
}
}
}
}
}
if (document.readyState === 'loading') {
document.addEventListener('DOMContentLoaded', rewriteLinks);
} else {
rewriteLinks();
}
})();
</script>
You can modify targetDomain to handle multiple sites or use a list of domains.
Step 2: Capture Params and Set Cookies (Site B)
- Tag Type: Create a new Custom HTML
- Trigger: All Pages
This script checks if fbclid, _fbc, or _fbp parameters exist in the URL and stores them in first-party cookies so Meta Pixel can access them across domains.
<script>
(function () {
var KEYS = ['fbclid', '_fbc', '_fbp'];
function getQueryParam(name) {
var regex = new RegExp('[?&]' + name + '=([^&]+)');
var m = window.location.search.match(regex);
return m ? m[1] : '';
}
function setCookie(name, value, days, domain) {
if (!value) return;
var cookie = name + '=' + value + ';path=/';
if (domain) cookie += ';domain=' + domain;
if (days) cookie += ';max-age=' + (days * 24 * 60 * 60);
document.cookie = cookie;
}
function getRootDomain() {
var host = window.location.hostname.split('.');
if (host.length >= 2) {
return '.' + host.slice(-2).join('.');
}
return window.location.hostname;
}
var rootDomain = getRootDomain();
for (var i = 0; i < KEYS.length; i++) {
var key = KEYS[i];
var val = getQueryParam(key);
if (val) setCookie(key, val, 90, rootDomain);
}
})();
</script>Step 3: Verify Tracking
If you’re testing using Meta Pixel Helper, open the Network tab and confirm the _fbc parameter value in the event payload is identical on both domains.
Outcome
After implementing this setup:
Users moving between domains retain their original Meta Click ID (_fbc), ensuring consistent attribution.
Conversions properly attribute back to the ad click.
You’ll see improved event matching and conversion accuracy in Meta Ads Manager.
Events show consistent fbc parameter across both domains (verify via Network tab)
Bonus: Server-Side Tagging
When sending events server-side, make sure to forward the same _fbc and _fbp values from your client cookies into your Meta Conversion API (CAPI) requests for proper deduplication.
Conclusion
Cross-domain tracking is essential for accurate Meta attribution in multi-domain user journeys.
By passing and preserving the _fbc parameter between domains, you ensure Meta Ads can correctly identify users and measure conversions.
This method is simple, compliant, and future-proof, and can be expanded easily into server-side setups for even cleaner data.