Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should Google Analytics go in the head or bottom of an HTML page?

Tags:

Google advises putting the google analytics script right before closing the </head>.

However, I would prefer to combine it with the rest my javascript that are now all together in a cached, external file, which is loaded at the bottom of my HTML file. Can I do it my way? If so, what am I risking then/what's the cost of putting the below code not in the head but at the bottom of the HTML?

<script type="text/javascript">  var _gaq = _gaq || [];   _gaq.push(['_setAccount', 'UA-22180365-1']);   _gaq.push(['_setDomainName', 'none']);   _gaq.push(['_setAllowLinker', true]);   _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> 
like image 809
Sam Avatar asked Mar 20 '11 18:03

Sam


People also ask

Should I put Google Analytics in header or footer?

There are a few exceptions, but for the most part, you'll always want to put your GA tracking code immediately after the <head> tag in the header of each page of your site. If you put the code in the footer, especially if you run other scripts on your site, Analytics might not track your users accurately.

Where Google Analytics code should be placed?

As per Google Analytics' instructions, your tracking code should always be placed just after the opening of the <head> tag of the website.

Which page should the tracking code be installed to?

You can put it anywhere you want really, and It'll run anywhere on the page whether it's in the head or the body. It's recommended you put it in the head though, so it can be as accurate as possible. You see, the Pageview is recorded only after that code is loaded up.


2 Answers

You're risking nothing really. Putting <script> tags at the end of the <body> is recommendable because of the blocking nature while executing the Javascript it encloses. Since the Javascript for GA is dynamically inserted and therefore non-blocking, you can put it anywhere.

I guess the only "problem" Google sees is, when putting that code at the bottom of your site, you might not catch visitors / pageloads cancel the request before completed. If someone calls your HTML document and cancels the request, the GA code might not get encountered (and therefore, not executed).

like image 91
jAndy Avatar answered Oct 05 '22 23:10

jAndy


You're risking nothing really. Putting <script> tags at the end of the is recommendable because of the blocking nature while executing the Javascript it encloses...

That is simply not true, jAndy.

You won't be able to get some features, such as real-time reporting, or Google Webmaster Tools verification, unless the code sits within the <head>. The whole point of the asynchronous code is that it does it can load while the rest of the page is loading. There is no UX risk from placing it within the <head>, and it is best-practice to do so. source: Google Analytics Support

@Stratboy I doubt it's the GA code that's breaking the layout. A properly tagged script - especially one that does not display any content, like GA's - would not affect the layout. I'd look elsewhere for your layout issues. Perhaps try validating it with W3C for some clues?

like image 26
MMMdata Avatar answered Oct 05 '22 23:10

MMMdata