So I have embedded iFrame forms on my site and need to implement persistent tracking for users, meaning that their UTM parameters pass between pages and then get submitted into hidden fields with the form.
I believe the best way to do this is by setting a cookie to capture the UTM params on the parent site (done), and then reading the cookie to set a query string on the iFrame src. From there, I can pass the hidden values from the iFrame src (that’s also already done).
What I’m having trouble figuring out is how to connect the dots. and have searched around quite a bit without finding a clear answer. And I’m showing my work but certainly open to other suggestions.
So I have these code snippets…
First, just above the iFrame embed, there’s this:
const getCookie = (__gtm_campaign_url) =>{
// Construct a RegExp object as to include the variable name
const re = new RegExp(`(?<=${cookie_name}=)[^;]*`);
try{
return document.cookie.match(re)[0]; // Will raise TypeError if cookie is not found
}catch{
return "this-cookie-doesn't-exist";
}
}
getCookie('__gtm_campaign_url') //
getCookie('_non_existent') // this-cookie-doesn't-exist
Then, to generate the iFrame embed:
var form = 'https://go.mysite.com/some-unique-ids';
var params = window.location.search; // I know this needs to change, just don't know how
var thisScript = document.scripts[document.scripts.length - 1];
var iframe = document.createElement('iframe');
iframe.setAttribute('src', form + params);
iframe.setAttribute('width', '100%');
iframe.setAttribute('height', 500);
iframe.setAttribute('type', 'text/html');
iframe.setAttribute('frameborder', 0);
iframe.setAttribute('allowTransparency', 'true');
iframe.style.border = '0';
thisScript.parentElement.replaceChild(iframe, thisScript);
Finally, within the iFrame, there’s this snippet to parse the cookie and get it into the proper hidden fields. Frankly, this is leftover from something else I’ve tried but didn’t work, so it could/should be eliminated if I could get the cookie parsed into var params = [foo] above, but including it for reference:
// Parse the Cookie
function getCookie('__gtm_campaign_url') {
var name = __gtm_campaign_url + "=";
var decodedCookie = decodeURIComponent(document.cookie);
var ca = decodedCookie.split(';');
for(var i = 0; i <ca.length; i++) {
var c = ca[i];
while (c.charAt(0) == ' ') {
c = c.substring(1);
}
if (c.indexOf(name) == 0) {
return c.substring(name.length, c.length);
}
}
return "";
}
// Parse the URL inside Cookie
function getParameterByName(name) {
name = name.replace(/[[]/, "\[").replace(/[]]/, "\]");
var regex = new RegExp("[\?&]" + name + "=([^&#]*)");
results = regex.exec(getCookie("__gtm_campaign_url"));
return results === null ? "" : decodeURIComponent(results[1].replace(/+/g, " "));
}
// Pass the values to hidden field
document.querySelector("input#input_3_9").value = getParameterByName('utm_source');
document.querySelector("input#input_3_9").value = getParameterByName('utm_term');
document.querySelector("input#836443_81170pi_836443_81170").value = getParameterByName('utm_medium');
document.querySelector("input#input_3_8").value = getParameterByName('utm_campaign');
document.querySelector("input#input_3_11").value = getParameterByName('utm_content');