Google analytics in header or footer?

My questions is, do you put Google Analytics Javascript code in the header or at the very bottom of the page (before </body>). I've heard people say it's best to put Google Analytics Javascript code at the very end of your html. Others say its best to put it in the header. Is there a best practice?


Solution 1:

Put it in your header. It's asynchronous so it won't block your page from loading and by placing it in your header you'll be more likely to get a accurate stats.

Solution 2:

This what google says about it:

Once you find the code snippet, copy and paste it into your web page, just before the closing </head> tag*. If your website uses templates to generate pages, enter it just before the closing tag in the file that contains the section. For the best performance across all browsers we suggest you position other scripts in your site in one of these ways:

  1. before the tracking code snippet in the <head> section of your HTML
  2. after both the tracking code snippet and all page content (e.g. at the bottom of the HTML body)

and here comes the *:

*One of the main advantages of the asynchronous snippet is that you can position it at the top of the HTML document. This increases the likelihood that the tracking beacon will be sent before the user leaves the page. It is customary to place JavaScript code in the section, and we recommend placing the snippet at the bottom of the section for best performance.

Solution 3:

The difference is simple, and the answer depends of your needs Suppose someone clicked your site by mistake and close it really quick.

If the script is in the top, you have a visitor, If the script is in the bottom, you don't have a visitor.

I use it in the bottom, cause to me if someone don't look to my site, is not a visitor.