简体   繁体   English

Google跟踪代码管理器自定义JavaScript错误

[英]Google Tag Manager custom JavaScript errror

I have return below JavaScript function to read cookie name and value. 我已经在JavaScript函数下面返回了读取cookie名称和值的信息。 I am able to fetch name and value when I use the script in Chrome and Firefox browser console. 当我在Chrome和Firefox浏览器控制台中使用脚本时,可以获取名称和值。

                  getCookie('xyz') ; 

              function getCookie(name)
               {
            var re = new RegExp(name + "=([^;]+)");
            var value = re.exec(document.cookie);
         return (value != null) ? unescape(value[1]) : null;
            }

but when I use same code in Google Tag Manager by using Custom JavaScript variable I am getting error "parenthesis ( required". 但是当我通过使用自定义JavaScript变量在Google跟踪代码管理器中使用相同的代码时,出现错误“括号(必需)”。

I am not able to figure out what is error as this code is running everywhere except in Google Tag Manager. 由于该代码在除Google跟踪代码管理器之外的所有地方运行,因此我无法弄清楚什么是错误。

The solution is to use the built-in "Cookie" Variable (go to variables, new, 1st Party Cookie, enter a name for the variable and in the config the name of the cookie). 解决方案是使用内置的“ Cookie”变量(转到变量,新的,第一方Cookie,输入变量的名称,并在配置中输入Cookie的名称)。 No need for homegrown solutions. 无需本地解决方案。

To strictly answer the question, custom javascript variables must be written as an anonymous function with a return value: 要严格回答该问题,必须将自定义javascript变量编写为具有返回值的匿名函数:

function() {
return "something";
}

I'm sure there is a workaround to pass parameters (ie the cookie name) but that's not typically how custom javascript variables are used and you do not need this for your use case. 我确信有一种解决方法可以传递参数(即cookie名称),但这通常不是自定义javascript变量的使用方式,因此在您的用例中不需要此方法。

if you insist on your function you could always return it via a custom javascript variable: 如果您坚持使用函数,则始终可以通过自定义javascript变量将其返回:

function() {
return function (name) {
            var re = new RegExp(name + "=([^;]+)");
            var value = re.exec(document.cookie);
         return (value != null) ? unescape(value[1]) : null;
            }
}

Store it in a custom javascript variable called getCookie and access in in custom HTML tags via {{getCookie}}('xyz'). 将其存储在名为getCookie的自定义javascript变量中,然后通过{{getCookie}}('xyz')在自定义HTML标记中访问。 Not a very good idea but possible. 这不是一个好主意,但可能。

I usually deal with cookie sets & gets via a custom HTML tag as I believe the custom Javascript requires a return value. 我通常会处理cookie集并通过自定义HTML标签获取,因为我相信自定义Javascript需要返回值。 So something along the lines of this: 因此,与此类似:

<script>
(function() {

function getCookie(name) {
    var nameEQ = name + "=";
    var ca = document.cookie.split(';');
    for(var i=0;i < ca.length;i++) {
        var c = ca[i];
        while (c.charAt(0)==' ') c = c.substring(1,c.length);
        if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
    }
    return null;
}

console.log(getCookie("xyz"));
</script>

})();

The above would output a log to the console with the value of a cookie named xyz whenever the tag's trigger is fired. 每当触发标签的触发器时,上面的命令就会向控制台输出一个日志,其中包含名为xyz的cookie的值。

If you wanted to store and use this cookie value within GTM you would then need to pass this variable on and store it, either using the cookie variable or via the dataLayer. 如果您想在GTM中存储和使用此cookie值,则需要使用cookie变量或通过dataLayer将此变量传递并存储。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM