WebPagetest Forums
Google analytics - lazy load - Printable Version

+- WebPagetest Forums (https://www.webpagetest.org/forums)
+-- Forum: Web Performance (/forumdisplay.php?fid=3)
+--- Forum: Optimization Discussions (/forumdisplay.php?fid=5)
+--- Thread: Google analytics - lazy load (/showthread.php?tid=10652)

Google analytics - lazy load - doc_uk - 02-24-2012 12:11 PM

I am using the Google-supplied analytics code which I believed to be loading asyncronously.
I have code to load 3 ajax pages (which load after window.load via jquery) and show after the blue document complete line - see http://www.webpagetest.org/result/120222_JX_3AYSQ/1/details/
http://www.webpagetest.org/result/120224_0Y_3BFF9/1/details/ (strange dns issue with Google Analytics there?)

Would using the same technique on the Google Analytics gain anything, such as maybe the initial render time?



RE: Google analytics - lazy load - pmeenan - 02-25-2012 01:45 AM

It shouldn't impact the start render time but it might pull in your onload a bit. The risk is that the later you push your analytics the less coverage you get in your data so you'll start missing page views, bails, etc - Just from user behavior.

RE: Google analytics - lazy load - doc_uk - 03-05-2012 11:54 AM

fwiw I have now set the Google analytics to this:-

var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-12345678-1']);

($(window).load( 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);

The difference from the std Google code is ($(window).load( function() { and an extra closing rounded bracket on the last line.

It all loads after page load, it appears to be worth about 0.1 seconds to my site.