10

The official Google documentation says to:

Paste your snippet (unaltered, in its entirety) into every web page you want to track. Paste it immediately before the closing </head> tag.

Surely there is some reason that they suggest this placement; I want to know what those reason(s) are.

My thinking is: Why can't it go in the body tag, if it's non-blocking asynchronous code anyway?

My colleague theorized: This is so that the pageViews still get tracked, even if another element further down the page breaks everything.

But if that were true, then why not suggest adding it right after the opening head tag?

3
  • 1
    Why do you question their documentation? Why do you care? Just put it before the closing tag,
    – Steve
    Commented Jun 25, 2015 at 22:44
  • 1
    It might have something to do with the title or meta tags.
    – hildred
    Commented Jun 26, 2015 at 2:34
  • 3
    @Steve: Imagine a scenario where the CMS you use makes it very very easy to add it to the page, but very difficult to add it in the recommended position. The client will ask "why such a simple change takes so long, and what will happen if it's not in the head." Commented Jun 26, 2015 at 15:53

2 Answers 2

7

You can put the code anywhere on the webpage with very little trade-off.

Why google specifically recommend to put it in the <head> section?

Because there's a tiny advantage of putting it there - with it in <head> you can capture users who went away immediately after opening your page.

Why not in body?

You can surely put it in <body> section and you will only lose a very tiny % of visitors (as mentioned above).

Earlier when the code was blocking, the recommendation was to put it just before the closing </body> tag so it doesn't slow down the website. The recommendation was changed when the code was made non-blocking. Also, since this code is so widely used across websites, high chance that the browser will serve the js file from the cache instead of actual download.

2
  • To me this doesn't answer the question "WHY must Universal Analytics code go immediately before the closing head tag?" 'Immediately before' - is not addressed in the answer above. I would also like to know the answer
    – Nat
    Commented Sep 25, 2015 at 6:12
  • 1
    You can put UA code anywhere in the head tag but since it is not as necessary as your title, description or even your linked css files, that's why it is the last thing browser should process, even if it's async.
    – Aakash
    Commented Sep 25, 2015 at 9:27
2

Now, this is not a technical explanation of why it is necessary – but it is a practical reason to do it.

If you want to use your Google Analytics (GA) tracking code for various verification purposes across Google's network of sites – for example,

  • to verify your ownership of a site in Google Search Console; or
  • to link your YouTube channel to a site

you can do this if your Google Account is admin of both GA and the above; but it only looks for the GA tracking code in the prescribed position, before </head>.

Also, not having the code in this position can cause annoying repetitive error messages in the Google Analytics interface.

So, while I'm sorry I can't shed any light on what the underlying (technical, if any) reasons might be, for me the above are good enough reasons to just do it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.