Sep 15th 2008 × Google analytics script via domready and mootools
Hate it when your site display is being slow while it waits for google analytics?
Google Analytics is just great. But, regretfully–it often causes a delay in my sites’ display. Loading the remote script can pause the domready (as the browser waits to see if the script it is evaluating won’t affect the DOM in some way). The result: often page load times increase by a few seconds.
But there is a simple fix. Here is sample the code provided by google to include into your page:
<script type="text/javascript"> var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www."); document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E")); </script> <script type="text/javascript"> var pageTracker = _gat._getTracker("UA-1199722-1"); pageTracker._trackPageview(); </script>
The solution for a nice fast page load: go to domready!
Simply rewrite this as a part of your normal domready function:
// google analytics via mootools / domready. window.addEvent("domready", function() { var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www."); new Asset.javascript(gaJsHost + "google-analytics.com/ga.js", { onload: function() { var pageTracker = _gat._getTracker("UA-1199722-1"); // your id here pageTracker._initData(); pageTracker._trackPageview(); } }); }); // end domready
Google won’t ever interfere with your page load again!



