如何将父饼干传递到iframe src中?

发布于 2025-02-09 14:05:47 字数 2821 浏览 1 评论 0原文

因此,我已经在我的网站上嵌入了iframe表单,需要为用户实现持久跟踪,这意味着他们的UTM参数传递在页面之间,然后将其提交给带有表单的隐藏字段。

我相信最好的方法是设置cookie以捕获父站点上的utm参数(完成),然后读取cookie以在iframe src上设置查询字符串。从那里,我可以从iframe src传递隐藏的值(这也已经完成)。

我在弄清楚的是如何连接点。并搜索了很多,但没有找到明确的答案。我正在展示自己的工作,但肯定会对其他建议开放。

因此,我有这些代码片段...

首先,在嵌入的iframe上方,有这样的代码片段:

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

然后,要生成嵌入的iframe:

 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);

最后,在iframe内,有此片段可以解析cookie并将其放入适当的隐藏字段。坦白说,这是我尝试过但没有起作用的其他东西的剩余物,因此,如果我可以将cookie解析到上面供参考:

// 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');

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');

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文