Best way to integrate Google Analytics
This is the best solution to integrate Google Analytics into your site. It uses ideas and code from the following 2 sites:
http://www.mattiasgeniar.be/webdevelopment/_gat-is-not-defined-google-analytics-error/
http://www.maifith.com/news/_gat-is-undefined-new-analytics-code
Here is the improved snippet:
<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>
Improvements:
- Does not use document.write
- Fixes the “_gat is not defined” issue
- Delayed GA access (1.5 second) so the page load time is not affected
- Very concise
Another improvement would be to cache ga.js locally to avoid the DNS lookup.