does Google analytics make a major effect on time to download a static web page?

后端 未结 15 1500
臣服心动
臣服心动 2021-01-07 22:44

I understand that by simply adding a script to the end of the body tag of a html document one makes it processable by Google analytics. My question is, is this likely to hav

15条回答
  •  天命终不由人
    2021-01-07 23:31

    Although downloading and running the actual ga.js is fast, what I've noticed all across Europe, on different connections/computers/OSes/browsers, is a MAJOR lag (anywhere from 0 to 30 (thirty) seconds) between the last byte of HTTP request and first byte of HTTP response.

    This is understandable, given the immense popularity of GA, but this is happenning before window.onload fires. So, if your page relies on JS and your users hit this lag, they are not going to analyze which component is responsible - they'll assume your site is horribly slow.

    A workaround for this is to register a window.onload function which will add the GA script. Example (using "window.onload=function()" for simplicity):

    window.onload = function() {
        var gaJsHost = (("https:" == document.location.protocol) 
                          ? "https://ssl." 
                          : "http://www.");
        var s = document.createElement('script');
        s.src = gaJsHost + "google-analytics.com/ga.js";
        s.type='text/javascript';
        var bodies = document.getElementsByTagName('body');
        if (bodies.length > 0) {
            bodies[0].appendChild(s);
        } else { // this should never happen, but sometimes does (curse you IE6!)
            document.appendChild(s);
        }
    
        // this says 100ms, but won't happen until ga.js is loaded
        window.setTimeout(function(){
            if (window['_gat']) {
                var pageTracker = _gat._getTracker("UA-xxxxxx-x");
                pageTracker._trackPageview();
            }
        },100);
    }
    

提交回复
热议问题