I just started using Google Analytics. Reading the instructions it says that the best place for them to be placed is just before the end of the closing head tag:
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'xxx']);
_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>
</head>
I thought I had read advice before that said I should place them right at the end of the document. Also I am using jQuery and javascript so would it be better to place the code so that it fires only after document ready?
Any help and advice would be much appreciated.
Thanks,
I’ve always placed it just before the end of the body tag. That way it won’t load the javascript file until the end (which means the important parts of your page load faster)
UPDATE
I’m sure that Google Analytics said to put it at the end of the body (at least way back). However, they have changed their code snippet so that it loads asynchronously on newer browsers so it shouldn’t matter so much.
Most I’ve heard on web page optimisation has always said to put the javascript towards the end of the page because of the way most browsers deal with loading and parsing javascript (i.e. not in parallel with other external resource like the loading and rendering of, say, images)