Google Analytics - Blocks HTML/page rendering

BenHelley picture BenHelley · May 25, 2009 · Viewed 7.8k times · Source

I have used the "Better Google Analytics JavaScript that doesn’t block page downloading" to load Google Analytics dynamically so that it will not block HTML / page rendering.

However, it appears occassionaly that my HTML page will block rendering on the Firefox 3.0 (WinXP) status message states:

"Transferring data from www.google-analytics.com"

Any ideas on how to load the Google Analytics JavaScript in a way in which it will not block HTML/page rending?

Answer

James picture James · Dec 4, 2009

You could use Google Analytics [asynchronous loading of tracking codes][1]. The following snippet should help:

<script type="text/javascript">

    var _gaq = _gaq || [];
    _gaq.push(['_setAccount', 'UA-XXXXX-X']);
    _gaq.push(['_trackPageview']);

    (function() {
        var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
        ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') +      '.google-analytics.com/ga.js';
       var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
    })();

</script>