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!
Dimitar, thanks for posting this info. I thinks it’s the solution to slow loading mootools style accordion on my website, but I’m unsure how to implement your script.
I removed the Google tracking code from the page with the accordion, http://cargocultdesign.com/web-design-services.html and added your code (and my Google ID #) to http://cargocultdesign.com/js/site.js which links to web-design-services.html, but Analytics is no longer gather info for the page.
Appreciate any help. Where should I insert the code?
you should put this WITHIN the site domready function – after “}, $(‘sidebar’));”
regards
Thanks Dimitar!! I knew there was some interferance with the domready but I couldn’t tap into it. My script actually stopped loading all together! Thanks buddy!!!