我的网站网址在名为xtor的参数中包含客户ID。我编写了一个代码来从url中检索客户ID,将其保存在cookie中并将其传递给第三方供应商标签。我的问题是cookie在移动浏览器中表现不一致。 示例第三方标记://thirdpartytagging.com/website/123456?Id={{CK-Customer_Id}}其中{{CK-Customer_Id}}是cookie变量具有值id 有时{{CK-Customer_Id}}具有客户ID,但通常是未定义的。 我不确定为什么每次都不传递客户ID。我正在使用谷歌标签管理器实现此代码。
!function () {
function setCookie(value) {
var expireInDays = 7;
var d = new Date();
d = new Date(d.getTime() + 1000 * 60 * 60 * 24 * expireInDays);
var expires = "expires=" + d.toUTCString();
var cookie = document.cookie = "id=" + value + ";" + expires + "; path=/; domain=" + location.hostname.replace(/^www\./i, "");
return cookie;
}
var customerId;
var url = window.location.href;
if ((url.indexOf('xtor=') != -1) && ((url.indexOf('[') != -1) || (url.indexOf('%5B') != -1) || (url.indexOf('&lbrack') != -1) || (url.indexOf('[') != -1))){
var mystring;
if (url.indexOf('%5B') != -1)
mystring = url.replace('%5B', '[').replace('%5D', ']');
else if (url.indexOf('&lbrack') != -1)
mystring = url.replace('&lbrack', '[').replace('&rbrack', ']');
else if (url.indexOf('[') != -1)
mystring = url.replace('[', '[').replace(']', ']');
else
mystring = url;
var matches = mystring.match(/\[(.*?)\]/);
if (matches) {
customerId = matches[1];
setCookie(customerId);
var dl = window.dataLayer = window.dataLayer || [];
dl.push({ 'cookie_cusid': customerId });
}
}
}()