MCFX Capture – nxcli.io iframe form + attributions

Posted by: Nico Cagalitan Date Posted: June 19, 2026
Back to Case Studies

Issue

The issue is that the client’s form is under the nxcli.io form application, and is embedded via iframe, which will not be tracked normally; thus, it needs a custom tracking to help the iframe form to fire lead events to LeadManagerFX.

Solution

The solution is by creating a custom code that communicates between the primary domain and the iframe domain, and also passes the attribution.

Achievement

Able to track and give attributions to the iframe form through custom tracking

Snippet/Screenshot/Link

Ticket: https://app.webfx.com/todos/269094658

Custom Code:

iframe domain:


(function() {

var config = {
allowedOrigins: [
'https://www.briggsjcb.com',
'https://briggsjcb.com'
],
maxRetries: 60,
retryDelay: 500
};

var receivedAttribution = null;
var attributionApplied = false;

function isAllowed(origin) {
return config.allowedOrigins.indexOf(origin) !== -1;
}

function overrideRCFXAttribution(attr) {
if (typeof mcfx === 'undefined') {
return false;
}

var success = false;

mcfx(function(tracker) {
try {
tracker.session.set('attribution', {
source: attr.source || '',
medium: attr.medium || '',
campaign: attr.campaign || '',
term: attr.term || '',
content: attr.content || '',
gclid: attr.gclid || '',
fbclid: attr.fbclid || '',
msclkid: attr.msclkid || ''
});

var updated = tracker.session.get('attribution');

if (updated.source === attr.source) {
success = true;
attributionApplied = true;
} else {
console.warn('%c[RCFX IFRAME] Attribution may not have stuck, will retry...', 'color: orange');
}
} catch (error) {
console.error('%c[RCFX IFRAME] Error overriding attribution:', 'color: red', error);
}
});

return success;
}

function sendConfirmation(attr) {
if (!window.parent || window.parent === window) return;

var message = {
type: 'rcfx_attribution_applied',
source: 'nxcli_iframe',
attribution: attr,
timestamp: Date.now()
};

config.allowedOrigins.forEach(function(origin) {
try {
window.parent.postMessage(message, origin);
console.log('%c[RCFX IFRAME] Confirmation sent to parent', 'color: green');
} catch (error) {
console.error('%c[RCFX IFRAME] Error sending confirmation:', 'color: red', error);
}
});
}

function processAttribution(attr) {
receivedAttribution = attr;

var retryCount = 0;

function attemptOverride() {
var success = overrideRCFXAttribution(attr);

if (success) {
sendConfirmation(attr);

// Re-apply every 2 seconds in case RCFX resets attribution to referral
setInterval(function() {
mcfx(function(tracker) {
var current = tracker.session.get('attribution');
if (current && current.source === 'www.briggsjcb.com' && attr.source !== 'www.briggsjcb.com') {
overrideRCFXAttribution(attr);
}
});
}, 2000);

} else if (retryCount < config.maxRetries) {
retryCount++;
setTimeout(attemptOverride, config.retryDelay);
} else {
console.error('%c[RCFX IFRAME] Failed to override after ' + config.maxRetries + ' retries', 'color: red; font-weight: bold');
}
}

attemptOverride();
}

window.addEventListener('message', function(event) {
if (!isAllowed(event.origin)) return;

if (event.data && event.data.type === 'rcfx_attribution_sync') {
if (!receivedAttribution || !attributionApplied) {
processAttribution(event.data.attribution);
}
}
});

function notifyReady() {
if (!window.parent || window.parent === window) return;

config.allowedOrigins.forEach(function(origin) {
try {
window.parent.postMessage({
type: 'rcfx_iframe_ready',
url: window.location.href
}, origin);
} catch (error) {}
});
}

function init() {
notifyReady();

// Keep notifying parent every 2 seconds until attribution is received
var notifyInterval = setInterval(function() {
if (!receivedAttribution) {
notifyReady();
} else {
clearInterval(notifyInterval);
}
}, 2000);

// Stop after 1 minute
setTimeout(function() {
clearInterval(notifyInterval);
}, 60000);
}

if (document.readyState === 'loading') {
document.addEventListener('DOMContentLoaded', init);
} else {
setTimeout(init, 500);
}

})();

Primary Domain:


(function() {

var config = {
iframeOrigin: 'https://1ee73c7f96.nxcli.io',
retryDelay: 500,
maxRetries: 40,
syncInterval: 1000
};

var iframe = null;
var attribution = null;
var syncIntervalId = null;

function getRCFXAttribution() {
if (typeof mcfx === 'undefined') {
return null;
}

var attr = null;

mcfx(function(tracker) {
try {
var session = tracker.session.get('attribution');

if (session) {
attr = {
source: session.source || '',
medium: session.medium || '',
campaign: session.campaign || '',
term: session.term || '',
content: session.content || '',
gclid: session.gclid || '',
fbclid: session.fbclid || '',
msclkid: session.msclkid || ''
};
console.log('%c[RCFX PARENT] Attribution from RCFX:', 'color: green; font-weight: bold', attr);
} else {
console.log('%c[RCFX PARENT] No attribution in RCFX session yet', 'color: orange');
}
} catch (error) {
console.error('%c[RCFX PARENT] Error reading RCFX:', 'color: red', error);
}
});

// Fallback to URL parameters if RCFX attribution is unavailable
if (!attr || !attr.source) {
var params = new URLSearchParams(window.location.search);
attr = {
source: params.get('utm_source') || '',
medium: params.get('utm_medium') || '',
campaign: params.get('utm_campaign') || '',
term: params.get('utm_term') || '',
content: params.get('utm_content') || '',
gclid: params.get('gclid') || '',
fbclid: params.get('fbclid') || '',
msclkid: params.get('msclkid') || ''
};

var hasData = Object.keys(attr).some(function(k) { return attr[k] !== ''; });
if (hasData) {
console.log('%c[RCFX PARENT] Using URL fallback:', 'color: orange', attr);
} else {
return null;
}
}

return attr;
}

function findIframe() {
var iframes = document.querySelectorAll('iframe');
for (var i = 0; i < iframes.length; i++) {
var src = iframes[i].src || '';
if (src.indexOf('nxcli.io') !== -1) {
return iframes[i];
}
}
return null;
}

function sendToIframe() {
if (!iframe || !iframe.contentWindow || !attribution) return false;

try {
iframe.contentWindow.postMessage({
type: 'rcfx_attribution_sync',
source: 'briggsjcb_parent',
attribution: attribution,
timestamp: Date.now()
}, config.iframeOrigin);
return true;
} catch (error) {
console.error('%c[RCFX PARENT] postMessage error:', 'color: red', error);
return false;
}
}

function startSync() {
if (syncIntervalId) clearInterval(syncIntervalId);

syncIntervalId = setInterval(function() {
sendToIframe();
}, config.syncInterval);

console.log('%c[RCFX PARENT] Continuous sync started', 'color: green');
}

window.addEventListener('message', function(event) {
if (event.origin !== config.iframeOrigin) return;

if (event.data && event.data.type === 'rcfx_attribution_applied') {
if (syncIntervalId) {
clearInterval(syncIntervalId);
console.log('%c[RCFX PARENT] Attribution confirmed — sync stopped', 'color: green');
}
}
});

function init() {
var retryCount = 0;

function attempt() {
if (!iframe) iframe = findIframe();
if (!attribution) attribution = getRCFXAttribution();

if (iframe && attribution) {
console.log('%c[RCFX PARENT] Ready — starting sync', 'color: green; font-weight: bold');

sendToIframe();
startSync();

iframe.addEventListener('load', function() {
setTimeout(function() { sendToIframe(); }, 500);
});

return;
}

if (retryCount < config.maxRetries) {
retryCount++;
setTimeout(attempt, config.retryDelay);
} else {
console.warn('%c[RCFX PARENT] Timeout after ' + config.maxRetries + ' retries', 'color: red');
}
}

attempt();
}

if (document.readyState === 'loading') {
document.addEventListener('DOMContentLoaded', init);
} else {
setTimeout(init, 500);
}

})();