They have a form that once a user submits a form they will be redirected to a subdomain and the attribution is not kept and will get “referral” instead
Able to create a custom code that was able to pass attribution from the primary domain – form submission – subdomain and kept the attribution
Custom Code:
(function() {
console.log(‘MCFX Attribution → Marketo UTM sync starting (multi-form support)…’);
// Function to get MCFX attribution data
function getMCFXAttribution() {
var attribution = {
utm_campaign: ”,
utm_source: ”,
utm_medium: ”,
utm_term: ”,
utm_content: ”,
gclid: ”
};
// Check if MCFX is available
if (typeof mcfx === ‘undefined’) {
console.log(‘MCFX not loaded yet’);
return attribution;
}
// Get attribution from MCFX tracker session
mcfx(function(tracker) {
try {
var mcfxAttribution = tracker.session.get(‘attribution’);
if (mcfxAttribution) {
attribution.utm_campaign = mcfxAttribution.campaign || ”;
attribution.utm_source = mcfxAttribution.source || ”;
attribution.utm_medium = mcfxAttribution.medium || ”;
attribution.utm_term = mcfxAttribution.term || ”;
attribution.utm_content = mcfxAttribution.content || ”;
attribution.gclid = mcfxAttribution.gclid || ”;
console.log(‘MCFX Attribution retrieved:’, attribution);
} else {
console.log(‘No MCFX attribution data found’);
}
} catch (error) {
console.error(‘Error getting MCFX attribution:’, error);
}
});
// Fallback: Read from URL parameters if MCFX data is empty
if (!attribution.utm_campaign && !attribution.utm_source) {
var urlParams = new URLSearchParams(window.location.search);
attribution.utm_campaign = urlParams.get(‘utm_campaign’) || ”;
attribution.utm_source = urlParams.get(‘utm_source’) || ”;
attribution.utm_medium = urlParams.get(‘utm_medium’) || ”;
attribution.utm_term = urlParams.get(‘utm_term’) || ”;
attribution.utm_content = urlParams.get(‘utm_content’) || ”;
attribution.gclid = urlParams.get(‘gclid’) || ”;
console.log(‘Using URL parameters as fallback:’, attribution);
}
return attribution;
}
// Function to populate Marketo hidden fields for a specific form
function populateMarketoUTMFields(form) {
var formId = form.getId ? form.getId() : form.id;
console.log(‘Populating UTM fields for form:’, formId);
var attribution = getMCFXAttribution();
if (!attribution) {
console.log(‘No attribution data available’);
return false;
}
// Map MCFX attribution to Marketo field names
var fieldMapping = {
‘utmcampaign’: attribution.utm_campaign,
‘utmsource’: attribution.utm_source,
‘utmmedium’: attribution.utm_medium,
‘utmterm’: attribution.utm_term,
‘utmcontent’: attribution.utm_content
};
// Populate each field using Marketo Forms API
var valuesToSet = {};
var hasValues = false;
for (var fieldName in fieldMapping) {
if (fieldMapping.hasOwnProperty(fieldName) && fieldMapping[fieldName]) {
valuesToSet[fieldName] = fieldMapping[fieldName];
hasValues = true;
console.log(‘Preparing to set:’, fieldName, ‘=’, fieldMapping[fieldName]);
}
}
if (hasValues && form.setValues) {
form.setValues(valuesToSet);
console.log(‘UTM fields set for form’, formId);
} else if (!hasValues) {
console.log(‘No attribution values to set’);
}
return true;
}
// Track which forms we’ve already set up
var processedForms = [];
// Wait for Marketo Forms 2.0 to be ready
function init() {
if (typeof MktoForms2 !== ‘undefined’) {
console.log(‘Marketo Forms 2.0 detected’);
// Handle all current and future forms
MktoForms2.whenReady(function(form) {
var formId = form.getId();
// Check if we’ve already processed this form
if (processedForms.indexOf(formId) !== -1) {
console.log(‘Form’, formId, ‘already processed, skipping’);
return;
}
processedForms.push(formId);
console.log(‘Marketo form ready:’, formId);
// Wait a bit for MCFX to be fully loaded
setTimeout(function() {
populateMarketoUTMFields(form);
}, 1000);
// Also populate before form submission
form.onSubmit(function() {
console.log(‘Form’, formId, ‘submitting, ensuring UTM fields are populated…’);
populateMarketoUTMFields(form);
});
});
console.log(‘Marketo Forms listener attached’);
} else {
console.log(‘Marketo Forms not loaded yet, retrying…’);
setTimeout(init, 500);
}
}
// Start initialization
if (document.readyState === ‘loading’) {
document.addEventListener(‘DOMContentLoaded’, init);
} else {
init();
}
console.log(‘MCFX Attribution → Marketo UTM sync script loaded’);
})();
Able to pass attribution to a form submission that redirects to a subdomain and keeps the attributions
Ticket: https://app.webfx.com/todos/268414175