Mobile app version of vmapp.org
Login or Join
Alves908

: WHY must Universal Analytics code go immediately before the closing head tag? The official Google documentation says to: Paste your snippet (unaltered, in its entirety) into every web page

@Alves908

Posted in: #GoogleAnalytics #UniversalAnalytics

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?

10.02% popularity Vote Up Vote Down


Login to follow query

More posts by @Alves908

2 Comments

Sorted by latest first Latest Oldest Best

 

@Annie201

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.

10% popularity Vote Up Vote Down


 

@Si4351233

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.

10% popularity Vote Up Vote Down


Back to top | Use Dark Theme