10

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?

  • It uses the async attribute of the script tag. I think FF 3.0 doesn't support it. – Bart van Heukelom Dec 22 '09 at 11:44
  • The article link is broken. Here is a link to the last cached version from April 2014 http://web.archive.org/web/20130402061350/http://lyncd.com/2009/03/better-google-analytics-javascript/ – Karl Glennon Jul 03 '14 at 08:00

5 Answers5

7

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>
James
  • 117
  • 1
  • 4
4

Put the Google Analytics code as the last thing before the </body> tag, like Google recommends?

chaos
  • 122,029
  • 33
  • 303
  • 309
  • I have and if you notice from the lyncd.com article code, it dynamically loads the JS into the head element. –  May 25 '09 at 04:58
  • Okay. When you're seeing the "Transferring data from www.google-analytics.com" message, has anything on your page actually been blocked from rendering, or does the page appear to have finished apart from that message hanging around? – chaos May 25 '09 at 05:00
  • When I see the message, the page has a noticeable block in rendering the page. I have JS that turns some DIV display from "none" to "block" but that code doesn't occur because the page get stuck on this "transfering data from www.google-analytics.com" –  May 25 '09 at 05:03
  • I see. Well, you could try arranging to have the script appended slightly after page load, on a short setTimeout; that at least will let other immediately pending operations run before it waits on Analytics. If that doesn't work or isn't feasible, you'd probably have to work out a way to pull the Analytics script using AJAX, which seems a bit challenging. – chaos May 25 '09 at 05:13
2

You may be seeing this bug. Before 3.6, FF sometimes incorrectly blamed google analytics for slowing down the page...

https://bugzilla.mozilla.org/show_bug.cgi?id=487638

Brian
  • 2,342
  • 2
  • 23
  • 18
1

But it at the bottom (just before the </body>) and delay it:

<script type="text/javascript" src="http://www.google-analytics.com/ga.js"></script>
<script type="text/javascript">
    if (typeof(_gat)=='object')
        setTimeout(function(){
            _gat._getTracker("UA-1234567-8")._trackPageview()}, 1500);
</script>

Have a look at my explanation about why I think this is the "best way to integrate analytics".

cherouvim
  • 31,725
  • 15
  • 104
  • 153
1

The DEFER attribute may work for you

<script DEFER type="text/javascript" src="http://www.google-analytics.com/ga.js">

<script DEFER type="text/javascript">... tracker code ...</script>
SpliFF
  • 38,186
  • 16
  • 91
  • 120