Matt K
Matt K

Reputation: 6708

Placement of GTM container contradicts placement of GA container

Google Support suggests placing its Google Analytics tracking code "before the closing </head> tag on every web page on your site you wish to track". It also suggests using Google Tag Manager to more efficiently implement your Analytics tracking code for dynamic sites. However, it says to place the Google Tag Manager code "immediately after the opening <body> tag on every page on your site", which seems to contradict itself.

So if I'm using Google Tag Manager to install Google Analytics (as it suggests), what's the proper placement of the Google Tag Manager container- before the closing </head> tag or after the opening <body> tag?

Upvotes: 0

Views: 351

Answers (2)

Philip Walton
Philip Walton

Reputation: 30461

It doesn't matter where you place your GA tracking snippet. The reason official GA docs suggest placing it in the <head> is because you can place it in the <head> and it will work. And including the snippet as early in the document as possible is advantageous for two reasons:

  1. So the analytics.js script gets downloaded as soon as possible, so tracking can happen as soon as possible. (Note: the longer it takes to download analytics.js, the greater the chances that a person who comes to your site and then immediately leaves won't be tracked. Maybe you care about this, maybe you don't -- I don't).
  2. Because the snippet defines the ga() command queue function, and the earlier you define it, the earlier you can use it. If you only use it in the snippet itself, then it doesn't matter where you define it.

On the other hand, the GTM snippet cannot be placed in the <head>. I mean, it can, but it might break things.

If you look at the GTM snippet, you'll notice it includes an <iframe> inside a <noscript> tag. If you put an <iframe> in the <head> of an HTML document, the HTML parser will assume you forgot to add the closing </head> tag and close the tag for you. This means that if you have any stuff after the GTM tag in the head (e.g. <link> tags, <meta> tags, etc.), that stuff will now be in <body>, which could cause errors.

All that being said, if you add the GTM tag as the very last tag inside <head>, having the HTML parser close </head> and start <body> prematurely will make absolutely no difference.

In short, it probably doesn't matter for most use cases. Also, I'd strongly encourage you to actually read the contents of the snippets you're adding to your site to understand what they do. They're not that scary.


To answer your question in a comment:

Yes but if GTM loads in the body it will place the GA tracking code in the body, which contradicts where it says to place the GA code

This is not really true, nor does it really matter. If you look at what the tracking script actually does, you'll see that it mainly just downloads analytics.js, and it absolutely does not matter if the <script> tag downloading analytics.js is in the <head> or <body>.

Upvotes: 1

nyuen
nyuen

Reputation: 8907

GA and GTM are two different implementation "methods", and therefore aren't really contradictory. As GTM documentation specifies, you should place the GTM bootstrap immediately after the opening <body> tag, outside of any containing tag (ie. <div>, <p>, <section>, etc.).

Here's a good reference: http://www.simoahava.com/analytics/container-snippet-gtm-secrets-revealed/

When GTM loads, it will invoke the analytics.js library, and create your tracking object (which mimics the first few bits of the GA snippet). Then you can create your pageview tag and start basic tracking.

Upvotes: 0

Related Questions