Should I put the Google Analytics JS in the <head> or at the end of <body>?

user34537 picture user34537 · Jul 4, 2010 · Viewed 43.2k times · Source

Google gives me a piece of javascript and tells me to include it in the <head>.

Can I put it at the end of <body> or will some side effects occur if I do that?

Answer

Chris Arguin picture Chris Arguin · Jul 4, 2010

Putting it at the end of the <head> section helps ensure the your metrics are tracked even when a user doesn't let the page finish loading.

They used to tell you to put it at the bottom of the page, before they added support for handling partial loading of pages.

Directly from Google:

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 <head> section, and we recommend placing the snippet at the bottom of the section for best performance.

See Google Analytics Help: Add the tracking code directly to your site