首页 文章

将gclid和bing utm标记存储在cookie中,将值传递给表单

提问于
浏览
0

所以我现在能够从访问者那里获取GCLID值并将其传递给我们的表单就好了 . 我的问题是,使用相同的脚本(下面附带),是否也可以捕获utm值?例如,如果访问者通过Bing广告访问我们的网站,则该网址将类似于www.example.com/?utm_source=bing&utm_medium=cpc

我需要能够将utm_source(bing)的值存储在cookie中,并将此值传递给我们的表单 .

目前使用GCLID为我工作的代码:

将GCLID存储在cookie中(在 </body> 标记之前):

<script type="text/javascript">
function setCookie(name, value, days){
    var date = new Date();
    date.setTime(date.getTime() + (days*24*60*60*1000)); 
    var expires = "; expires=" + date.toGMTString();
    document.cookie = name + "=" + value + expires + ";path=/";
}
function getParam(p){
    var match = RegExp('[?&]' + p + '=        ([^&]*)').exec(window.location.search);
    return match && decodeURIComponent(match[1].replace(/\+/g, ' '));}
    var gclid = getParam('gclid');
    if(gclid){
    var gclsrc = getParam('gclsrc');
    if(!gclsrc || gclsrc.indexOf('aw') !== -1){
        setCookie('gclid', gclid, 90);
    }
}
</script>

将值传递给表单(在 Headers 中):

<script> 
window.onload = function getGclid() {        
                document.getElementById("00N3100000H5IBe").value = (name = new    
                RegExp('(?:^|;\\s*)gclid=([^;]*)').exec(document.cookie)) ? 
                name.split(",")[1] : "";
            }
// window.onload() may not be supported by all browsers.  
// If you experience problems submitting the GCLID as a
// hidden field, consider using an alternate method to
// call this function on page load.
</script>

我能够修改当前脚本以捕获utm值,但它不会捕获gclid值 . 到目前为止,我一直无法做到这两点 .

任何帮助或方向将不胜感激 . 如果我需要更好地解释一下,请告诉我 . 谢谢!

1 回答

  • 0

    下面的代码将GCLID代码放在隐藏字段或UTM_Source中 . 当你第一次需要设置cookie然后把它读出来的时候我身体里面就有了这一切 .

    祝好运!

    注意:'Field7'是我表单上隐藏字段的ID . 您必须将其更改为您的表单 .

    <script type="text/javascript">
    function setCookie(name, value, days){
        var date = new Date();
        date.setTime(date.getTime() + (days*24*60*60*1000));
        var expires = "; expires=" + date.toGMTString();
        document.cookie = name + "=" + value + expires + ";path=/";
    }
    function getParam(p){
        var match = RegExp('[?&]' + p + '=([^&]*)').exec(window.location.search);
        return match && decodeURIComponent(match[1].replace(/\+/g, ' '));
    }
    function readCookie(name) {
    var n = name + "=";
    var cookie = document.cookie.split(';');
    for(var i=0;i < cookie.length;i++) {      
      var c = cookie[i];      
      while (c.charAt(0)==' '){c = c.substring(1,c.length);}      
      if (c.indexOf(n) == 0){return c.substring(n.length,c.length);}
    }
    return null;
    }
    // Define the variables
    var gclid = getParam('gclid');
    var utm_source = getParam('utm_source');
    
    // Check if there is a GCLID first, then check if there is a utm_source
    if(gclid){
    alert(gclid);
          setCookie('gclid', gclid, 90);
            } else if(utm_source) {
    alert(utm_source);
            setCookie('utm_source', utm_source, 90);
    }
    
    </script>
    <script type="text/javascript"> // Now set the hidden field on the form by looking for the correct cookie name
    window.onload = function() {      
        if (gclid) {
     document.getElementById('Field7').value = readCookie('gclid');
    } else if (utm_source) {
     document.getElementById('Field7').value = readCookie('utm_source');
    }
    }
    </script>
    

相关问题