Cookie在桌面上运行良好但在移动浏览器中保持一致

时间:2018-06-15 19:37:45

标签: javascript

我的网站网址在名为xtor的参数中包含客户ID。我编写了一个代码来从url中检索客户ID,将其保存在cookie中并将其传递给第三方供应商标签。我的问题是cookie在移动浏览器中表现不一致。 示例第三方标记://thirdpartytagging.com/website/123456?Id={{CK-Customer_Id}}其中{{CK-Customer_Id}}是cookie变量具有值id 有时{{CK-Customer_Id}}具有客户ID,但通常是未定义的。 我不确定为什么每次都不传递客户ID。我正在使用谷歌标签管理器实现此代码。

示例网址:https://www.test.com/en/p/123456?utm_term=Shop&utm_source=Search&utm_campaign=Test&xtor=[39816390]&utm_medium=email

!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('&#91') != -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('&#91') != -1)
        mystring = url.replace('&#91', '[').replace('&#93', ']');
    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 });
    }
}
}()

0 个答案:

没有答案