为什么我尝试在 Google 跟踪代码管理器中发布对我的内部通信代码的更改时出现 JavaScript 编译器错误?

Why am I getting a JavaScript compiler error when trying to publish changes to my intercom tag in Google Tag Manager?

这是我的标签:

<script>
    window.intercomSettings = {
        app_id: "fanwstw2"
    };
</script>
<script>
    (function() {
        var w = window;
        var ic = w.Intercom;
        if (typeof ic === "function") {
            ic('reattach_activator');
            ic('update', intercomSettings);
        } else {
            var d = document;
            var i = function() {
                i.c(arguments)
            };
            i.q = [];
            i.c = function(args) {
                i.q.push(args)
            };
            w.Intercom = i;

            function l() {
                var s = d.createElement('script');
                s.type = 'text/javascript';
                s.async = true;
                s.src = 'https://widget.intercom.io/widget/fanwstw2';
                var x = d.getElementsByTagName('script')[0];
                x.parentNode.insertBefore(s, x);
            }
            if (w.attachEvent) {
                w.attachEvent('onload', l);
            } else {
                w.addEventListener('load', l, false);
            }
        }
    })()
</script>

这是错误信息

我看不出您的代码有任何问题,可能是解析器过于笨拙。也可能是你在 IIFE 中包装造成的。

如何更改下面的这一行(只是为了排除它是奇怪的)..

ic('update', intercomSettings);

为此...

ic('update', w.intercomSettings);

我也遇到了这个问题,我认为这是因为基于 ES5 引擎的 GTM 正在查看 ES6 代码并试图将其解析为 ES5。它很可能来自 if 块中的 l() 函数声明。尝试将其移出 if 块,就像它之前一样,然后再次编译标签,如下所示:

(function() {
    var w = window;
    var ic = w.Intercom;

    // moved this out of if block
    function l() {
        var s = d.createElement('script');
        s.type = 'text/javascript';
        s.async = true;
        s.src = 'https://widget.intercom.io/widget/fanwstw2';
        var x = d.getElementsByTagName('script')[0];
        x.parentNode.insertBefore(s, x);
    }

    if (typeof ic === "function") {
        ic('reattach_activator');
        ic('update', intercomSettings);
    } else {
        var d = document;
        var i = function() {
            i.c(arguments)
        };
        i.q = [];
        i.c = function(args) {
            i.q.push(args)
        };
        w.Intercom = i;

        if (w.attachEvent) {
            w.attachEvent('onload', l);
        } else {
            w.addEventListener('load', l, false);
        }
    }
})()