Analytics.js callback executed before library is loaded

老子叫甜甜 提交于 2020-05-17 07:08:06

问题


I'm facing a very weird issue with analytics.js. Basically, I have a basic implementation at the top of the page, and inside another JS file that is loaded right after the analytics.js is downloaded and initialized I have a function that validates if ga has been enabled loaded.

In that function I get the ga function in a local variable named gaFn, and I use the callback function of the analytics library to make sure the analytics library has been loaded to use the tracker, my problem is that sometimes the callback function, is getting triggered before ga is completely ready to be used.

The parameters it receives are undefined, and also checking in the console some other methods such getAll are not defined in the global ga object.

Any idea what is causing this error or how can I solve it?

    <script>
        (function (i, s, o, g, r, a, m) {
        i['GoogleAnalyticsObject'] = r; i[r] = i[r] || function () {
            (i[r].q = i[r].q || []).push(arguments)
        }, i[r].l = 1 * new Date(); a = s.createElement(o),
            m = s.getElementsByTagName(o)[0]; a.async = 1; a.src = g; m.parentNode.insertBefore(a, m)
        })(window, document, 'script', 'https://www.google-analytics.com/analytics.js', 'ga');

        ga('create', 'UA-XXXXXXX-1', 'auto', 'tracker0');
        ga('send', 'pageview');
    </script>

I noticed it happens when I define a name for my tracker, but I cannot access it even if I use the getAll method.

来源:https://stackoverflow.com/questions/61530268/analytics-js-callback-executed-before-library-is-loaded

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