How high in the <head> section can you safely put the Google Tag Manager code?

Google Tag Manager isn't dependent on any plugins, and runs in raw JavaScript. In order to prevent conflict, it should be placed as high as possible in the <head> tag.

Considering it's self-contained and doesn't have any conflicts, it's perfectly safe to place it right after <head>, before any <meta> tags. Google's search algorithm will read the entire DOM in an attempt to find your <meta> tags, so they don't need to be the first things in the <head> section.

In your above example, I would recommend placing your Google Tag Manager code in between <head> and <meta charset="utf-8"> (which is where I normally place it on my own sites).

In addition to this, don't forget the noscript equivalent, which allows Google Tag Manager to run in the case of JavaScript being disabled on the page. This should be placed directly after your <body> tag:

<noscript>
  <iframe src="https://www.googletagmanager.com/ns.html?id=GTM-XXXXXX" height="0" width="0" style="display:none;visibility:hidden"></iframe>
</noscript>

Hope this helps! :)


The reason why Google recommends putting it as high up as possible is primarily to improve accuracy in tracking. The higher up in the page the snippet is, the faster it is loaded. Placing the snippet lower in your page, can potentially miss tracking users who left your page before the code was loaded. It can also cause to mistakenly report a site visitor that navigated away from your homepage before the code was loaded as a direct visitor to the page the user navigated to.

It is also important with Google’s A/B testing tool, Optimize. Having the snippet load faster ensures that Optimize will load the correct version of the page as soon as possible.

However, there are other factors you might want to consider, as discussed here: What are best practices to order elements in <head>?. For example:

...For this reason, HTML5 specifies that any meta tag which is used to specify the character set (either <meta http-equiv="Content-type" content="text/html; charset=..."> or simply <meta charset=...>) must be within the first 1024 bytes of the file in order to take effect. So, if you are going to include character encoding information within your document, you should put the tag early in the file, possibly even before the <title> element.

So although you can put your tracking code snippet immediately following the opening head tag, you might want to consider putting it after the most important meta tags. Those tags generally don't take long to load, and won't hold off your tracking code much.

But yes, it does matter where in the head you put your tracking code for the reasons mentioned above. So if you'll be loading many scripts, stylesheets, etc., then put your tag manager code higher up rather than just dropping it at the end.

<!doctype html>
<html class="no-js" lang="">
<head>
    <meta charset="utf-8">
    <meta http-equiv="x-ua-compatible" content="ie=edge">
    <title></title>
    <meta name="description" content="">
    <meta name="viewport" content="width=device-width, initial-scale=1">

    <!-- Tracking Code -->

    <link rel="stylesheet" href="css/normalize.css">
    <link rel="stylesheet" href="css/main.css">