将gclid和bing utm标记存储在Cookie中,传递值以形成

时间:2017-02-10 17:42:21

标签: javascript php cookies google-adwords bing

所以我现在能够从访问者那里获取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>

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

<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 :(得分: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>