跟踪代码管理器中的JavaScript编译器错误

时间:2018-05-31 00:35:42

标签: javascript cookies google-tag-manager

我正在尝试使用Cookie通过GTM为每个会话设置用户综合浏览量。我正在使用自定义JavaScript变量:

function readCookie(name) {
    var cookieName = name + "=";
    var cookieSplit = document.cookie.split(';');
    for (var i = 0; i < cookieSplit.length; i++) {
        var cookies = cookieSplit[i];
        while (cookies.charAt(0) === ' ') cookies = cookies.substring(1, cookies.length);
        if (cookies.indexOf(cookieName) === 0) return cookies.substring(cookieName.length, cookies.length);
    }
    return null; 
}
function viewAppend() {
    var oldCookie = readCookie('viewCount');
    if (oldCookie === null) {
        document.cookie = "viewCount=1; path=/";
    } else {
        var views = oldCookie + 1;
        document.cookie = "viewCount="+views+"; path=/";
    }
}
viewAppend();

我一直得到相同的编译器错误:“第12行出错,第1行:解析错误。”)“预期。”

我似乎无法弄清楚我做错了什么,但感谢任何帮助。

------编辑------ 通过下面的评论,这是我目前的代码。当前错误是:“第16行出错,第40行:解析错误。预期出现分号”

function doStuff() {
    function readCookie(name) {
        var cookieName = name + "=";
        var cookieSplit = document.cookie.split(';');
        for(var i=0;i < cookieSplit.length;i++) {
            var cookies = cookieSplit[i];
            while (cookies.charAt(0) === ' ') cookies = cookies.substring(1,cookies.length);
            if (cookies.indexOf(cookieName) === 0) return cookies.substring(cookieName.length,cookies.length);
        }
        return null;
    }
    function viewAppend() {
        var oldCookie = readCookie('viewCount');
        if (oldCookie === null) {
            document.cookie = "viewCount="1"; path=/";
        } else {
            var views = parseInt(oldCookie) + 1;
            document.cookie = "viewCount="+views+"; path=/";
        }
    }
}

2 个答案:

答案 0 :(得分:2)

你在这一行引用了问题:

        document.cookie = "viewCount="1"; path=/";

它应该是:

        document.cookie = "viewCount=1; path=/";

你不需要在cookie的值周围加上引号(如果你这样做,你可以转义它们或在整个字符串周围使用单引号)。

答案 1 :(得分:0)

好吧,我回到绘图板并试图以另一种方式解决问题。起初我试图将所有内容构建到GTM中的单个自定义JavaScript变量中。那是愚蠢的。我决定这样做:

首先,我在GTM中构建了一个自定义HTML标记,用于读取/写入在所有页面上触发的PageView cookie。

<script>
function readCookie(name) {
    var cookieName = name + "=";
    var cookieSplit = document.cookie.split(';');
    for(var i=0;i < cookieSplit.length;i++) {
        var cookies = cookieSplit[i];
        while (cookies.charAt(0) === ' ') cookies = cookies.substring(1,cookies.length);
        if (cookies.indexOf(cookieName) === 0) return cookies.substring(cookieName.length,cookies.length);
    }
    return null;
}
function viewAppend() {
    var oldCookie = readCookie('viewCount');
    if (oldCookie === null) {
        document.cookie = "viewCount=1; path=/";
    } else {
        var views = parseInt(oldCookie) + 1;
        document.cookie = "viewCount="+views+"; path=/";          
    }
}
viewAppend();
</script>

然后我构建了一个自定义Javascript变量,该变量读取cookie并将其作为整数返回。

function doStuff() {
function readCookie(name) {
    var cookieName = name + "=";
    var cookieSplit = document.cookie.split(';');
    for(var i=0;i < cookieSplit.length;i++) {
        var cookies = cookieSplit[i];
        while (cookies.charAt(0) === ' ') cookies = cookies.substring(1,cookies.length);
        if (cookies.indexOf(cookieName) === 0) return cookies.substring(cookieName.length,cookies.length);
    }
    return null;
}
var oldCookie = readCookie('viewCount');
var views = parseInt(oldCookie);
return views;
}

然后我简单地构建了我的pagievew标记,只要在Window Load上的pageviews变量大于4,就会触发该标记以指示参与用户。

感谢@Barmar帮助思考问题。你的问题肯定会挑战我如何接近它。