2017-02-10 107 views
0

因此,我目前能夠從訪問者中捕獲GCLID值並將其傳遞到我們的表單中。我的問題是,使用相同的腳本(附後),是否也可以捕獲utm值? 例如,如果訪問者來到我們從兵廣告的網站,網址將會像www.example.com/?utm_source=bing & utm_medium =中共中央在cookie中存儲gclid和bing utm標記,將值傳遞給

我需要能夠存儲的值utm_source(bing)放在一個cookie中,並將這個值傳遞給我們的表單。

商店GCLID在餅乾(前</body>標籤):這是目前的工作對我來說與GCLID

代碼

<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值。到目前爲止,我一直無法做到這一點。

任何幫助或方向將不勝感激。請讓我知道是否需要更好地解釋事情。謝謝!

回答

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>