5

I previously successfully setup intercom using Google Tag Manager. Haven't touched it for a couple of months and go to make a new tag but I'm presented with this error coming from the intercom tag:

Error at line 6, character 243: this language feature is only supported for ECMASCRIPT6 mode or better: block-scoped function declaration. Use --language_in=ECMASCRIPT6 or ECMASCRIPT6_STRICT or higher to enable ES6 features.

My script for intercom hasn't changed since it originally worked:

<script>
  window.intercomSettings = {
    app_id: "key"
  };
</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/key';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>

If anyone has encountered this error and has any advise or tips on how to solve it, it would be much appreciated.

4

3 回答 3

14

在 GTM 将代码包装到最小化的 JavaScript 文件中以将其注入页面之前,代码会经过一个 linter;这里的问题是谷歌有时会更改 linter 的规则以使其更加严格(这在 GTM 论坛上出现过几次)。由于这没有在任何地方公布,它偶尔会破坏以前工作的标签。

这里的问题似乎是您在 if/else 块中标记了一个函数。当您“美化”您的文件时,您可以看到这一点:

<script>
    window.intercomSettings = {
        app_id: "key"
    };
</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/key';
                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>

“l”函数在“else”块中声明,而 Google 的 linter 不喜欢这样(因为以前版本的 Javascript 在技术上没有函数的块作用域,这仅在 ES6 中引入)。

虽然我确信有一个正确的方法来解决这个问题,但一个简单的方法是将“l”的声明移到块之外:

<script>
    (function() {
        var w = window;

        function l() {
            var s = d.createElement('script');
            s.type = 'text/javascript';
            s.async = true;
            s.src = 'https://widget.intercom.io/widget/key';
            var x = d.getElementsByTagName('script')[0];
            x.parentNode.insertBefore(s, x);
        }
        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;


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

看起来它不应该有副作用,并且标签现在可以工作(至少在我尝试时)。

于 2018-02-13T12:07:59.250 回答
4

这是遵循@ruben-stolk 的建议的完整答案。您还需要在新函数的末尾插入一个分号,如下面的代码片段所示。这在 GTM 中进行了测试并通过了 lint 错误。

<script>
  window.intercomSettings = {
    app_id: "xe395ivj"
  };
</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;var l=function(){var s=d.createElement('script');s.type='text/javascript';s.async=true;s.src='https://widget.intercom.io/widget/xe395ivj';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>

于 2018-02-26T02:05:34.930 回答
1

您可以尝试做的是将您的代码从

function namedFunction(x) {}

var namedFunction = function(x){}
于 2018-02-26T15:11:49.167 回答