谷歌分析已停止跟踪

时间:2015-06-18 09:33:13

标签: javascript google-analytics analytics

我一段时间以来一直在与这个问题作斗争。我认为这一切都始于我将我的网站添加到谷歌网站管理员的工具:在某些时候我开始收到一条警报,说我的网站缺少跟踪代码,但实际上它已经存在,即使它说它丢失了,我仍然得到了我的分析统计数据。

enter image description here

那时,代码包含在一个包含我所有javascript的.js文件中。它被包含在</body>标记之前,并以这种方式加载:

<script type="text/javascript">
    function downloadJSAtOnload() {
        var element = document.createElement("script");
        element.src = "js/build/production.min.js";
        document.body.appendChild(element);
    }

    if (window.addEventListener)
        window.addEventListener("load", downloadJSAtOnload, false);
    else if (window.attachEvent)
        window.attachEvent("onload", downloadJSAtOnload);
    else
        window.onload = downloadJSAtOnload;
</script>
</body>

我在某个博客(http://www.giftofspeed.com/defer-javascripts/)上发现了这个脚本,我使用它来推迟javascript加载,使页面加载速度更快。

在某些时候我认为推迟ga代码是问题(因为它在production.min.js文件中),所以我已经将ga代码移到了外面。现在它就像这样

<script type="text/javascript">
    function downloadJSAtOnload() {
        var element = document.createElement("script");
        element.src = "js/build/production.min.js";
        document.body.appendChild(element);
    }

    if (window.addEventListener)
        window.addEventListener("load", downloadJSAtOnload, false);
    else if (window.attachEvent)
        window.attachEvent("onload", downloadJSAtOnload);
    else
        window.onload = downloadJSAtOnload;
</script>
<script>
//my google analytics code here
</script>
</body>

一旦我这样做了,警报就解决了,但跟踪不再发生了。

enter image description here

我从小就使用过GA,我从来没有像这样的问题。这可能是什么?我觉得我已经尝试过了。

1 个答案:

答案 0 :(得分:0)

现代浏览器的方法

https://github.com/jfriend00/docReady/blob/master/docready.js

您的自定义:

<script type="text/javascript">
    function downloadJSAtOnload() {
        var element = document.createElement("script");
        element.src = "js/build/production.min.js";
        document.body.appendChild(element);
    }

(function(funcName, baseObj) {
    "use strict";
    // The public function name defaults to window.docReady
    // but you can modify the last line of this function to pass in a different object or method name
    // if you want to put them in a different namespace and those will be used instead of 
    // window.docReady(...)
    funcName = funcName || "docReady";
    baseObj = baseObj || window;
    var readyList = [];
    var readyFired = false;
    var readyEventHandlersInstalled = false;

    // call this when the document is ready
    // this function protects itself against being called more than once
    function ready() {
        if (!readyFired) {
            // this must be set to true before we start calling callbacks
            readyFired = true;
            for (var i = 0; i < readyList.length; i++) {
                // if a callback here happens to add new ready handlers,
                // the docReady() function will see that it already fired
                // and will schedule the callback to run right after
                // this event loop finishes so all handlers will still execute
                // in order and no new ones will be added to the readyList
                // while we are processing the list
                readyList[i].fn.call(window, readyList[i].ctx);
            }
            // allow any closures held by these functions to free
            readyList = [];
        }
    }

    function readyStateChange() {
        if ( document.readyState === "complete" ) {
            ready();
        }
    }

    // This is the one public interface
    // docReady(fn, context);
    // the context argument is optional - if present, it will be passed
    // as an argument to the callback
    baseObj[funcName] = function(callback, context) {
        // if ready has already fired, then just schedule the callback
        // to fire asynchronously, but right away
        if (readyFired) {
            setTimeout(function() {callback(context);}, 1);
            return;
        } else {
            // add the function and context to the list
            readyList.push({fn: callback, ctx: context});
        }
        // if document already ready to go, schedule the ready function to run
        // IE only safe when readyState is "complete", others safe when readyState is "interactive"
        if (document.readyState === "complete" || (!document.attachEvent && document.readyState === "interactive")) {
            setTimeout(ready, 1);
        } else if (!readyEventHandlersInstalled) {
            // otherwise if we don't have event handlers installed, install them
            if (document.addEventListener) {
                // first choice is DOMContentLoaded event
                document.addEventListener("DOMContentLoaded", ready, false);
                // backup is window load event
                window.addEventListener("load", ready, false);
            } else {
                // must be IE
                document.attachEvent("onreadystatechange", readyStateChange);
                window.attachEvent("onload", ready);
            }
            readyEventHandlersInstalled = true;
        }
    }
})("downloadJSAtOnload", window);
// modify this previous line to pass in your own method name 
// and object for the method to be attached to

</script>