Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

People also ask

Where should Google Analytics code be placed?

Answer: Just before the closing </body> tag of your website.

Which is best & recommend place to place Google Analytics JavaScript code?

Google used to recommend putting it just before the </body> tag, because the original method they provided for loading ga. js was blocking. The newer async syntax, though, can safely be put in the head with minimal blockage, so the current recommendation is just before the </head> tag.

Where do I put the Google Analytics code in my header?

Probably the quickest and easiest way to do it, it requires a simple code edit. Just go to Appearance > Editor > header. php and add your Google Analytics code right before the ending </head> tag or after the opening <body> tag.


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


2021 Update

The general help page now states to "Copy and paste the global site tag after the opening <head> tag on each page you want to measure". So they now want it before virtually everything else. This may be due to changes to the tracking scripts.

2020 Update

Google currently suggests three different locations, which is frustrating. I haven't found technical justification for any of them directly from Google.

  • For static sites the general help page tells us to place the code snippet "before the closing </head> tag".

  • For dynamic sites (with documents generated by the server), the same site says to put it "immediately after the opening <body> tag".

  • The developer site, however, still has us putting it "near the top of the <head> tag and before any other script or CSS tags" for all sites.

These are substantially different locations, and what's best for a particular user may depend on whether collection of partial page loads is desired. Some testing may be in order.


You can put it anywhere you want. I always put tracking code at the end of the page and I've never had any problems.


You can put it anywhere you want on page, and ll run anywhere on the page whether it’s in the head or the body. But according to Google support (Add the tracking code directly to your site) it is recommended to it in the head tag, paste it immediately before the closing </head> tag.

Following paragraph from a good article explaining why to put code in head tag

The Pageview is recorded only after that code is loaded up. So the sooner you load the code, the sooner that Pageview is recorded. Let’s say you have a big blog page, and it’s a slow loading one, taking even 10 to 20 seconds to load everything up. If your Google Code doesn’t start until the end of the page, it can get held up, just like the old non-asynchronous code used to hold up OTHER lines of code. Except now it’s holding up the tracking code. If a visitor to your site hits the page, and then leaves it before the tracking code fires the Pageview, then you lose that visitor. They now become a new direct visit to whatever page of the site they landed on. This can make all sorts of data on your site incorrect.


2021 addition regarding page load speed

Before 2021 answer was quite straight foward. Generaly Google Analytics gathers most reliable data when it is run as early as possible when the page is loading. But there are 2 things to take into account:

  1. Many 2021 implementations of GA use Google Tag Manager, which also loads other, sometimes heavy, scripts. That was not the case a few years ago.
  2. Introduction of Core Web Vitals and mobile first indexing by Google and their importance for SEO means that accuracy of Google Analytics is no longer only thing to consider when placing the code. The other one is page load time.

In some situations, when you place GA with GTM on script heavy sites, it is easy to impact load times of the page and Core Web Vitals with the implementation. You can see this impact in Google PageSpeed Insights on many popular sites - before 2021 it almost did not matter. Now, changing the position of GTM (and GA with it) can make a big difference in PageSpeed Insights results. See how often "remove render-blocking javascript" advice contains GTM and GA tags in PageSpeed Insights.

So, to sum up, in 2021 there will even be cases that you would want to place GA code at the end of body. You have to be extra careful with your decision, especially when implementing it with GTM and other scripts.


In the head, just before the closing </ head> tag, then you won't have any problems verifying your website in Webmaster Tools.