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

岁酱吖の 提交于 2019-12-13 00:53:32

问题


This is my tag:

<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>

This is the error message


回答1:


I had this issue as well, and I believe it's because GTM, which is based on the ES5 engine, is seeing ES6 code and trying to parse it as ES5. It is likely coming from your l() function declaration within the if block. Try to move that out of the if block, like just before it and compile the tag again, like this:

(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);
        }
    }
})()



回答2:


I can't see any issue with your code, maybe it's the parser being overly awkward. It could also be caused by you wrapping within an IIFE.

How about changing this line below (just to rule it out being weird)..

ic('update', intercomSettings);

To this...

ic('update', w.intercomSettings);


来源:https://stackoverflow.com/questions/48440623/why-am-i-getting-a-javascript-compiler-error-when-trying-to-publish-changes-to-m

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!