0

I recently added to my website the google tag manager container. Reading the google documentation i found this sentence : "Paste this snippet into your website template page so that it appears immediately after the opening tag.".

This makes me wonder why it has to be at the top of the <body> tag. All the answers i found on the web justified the presence of the container in the <body> tag but i didn't found any reason why it should be at the beginning.

Does anyone have some informations about this ?

1 Answer 1

1

Here's what I found about having the GTM container in the <body>:

First, if you add the <noscript/> tag as well (as you should), this should always be in the body of the document. The tag (which is shown for browsers without JavaScript enabled) contains an iFrame which loads the GTM library. If you add the <noscript/> tag into the head of your document, it can perform pretty wildly with some browsers. You could experiment with leaving the <noscript/> in the body, and placing the JavaScript in the <head/>, but I haven’t tested it and certainly don’t recommend it.

The second reason is simple: to maximize data collection, the snippet should be the first thing loaded when the body of the page is rendered. Because the library is loaded asynchronously, there’s no reason to delay it at all. So have it load as the first thing when the body is rendered, so that you don’t risk losing any data due to sluggish DOM elements delaying the page load.

and potential problems with having it in the <head>:

You can place the container snippet in the HEAD of the document. Usually this should work fine. The only problem I can think of is that GTM loads before the browser reaches BODY, and thus might fail because many GTM features require the document node, which is created when BODY is rendered.

(cf. http://www.simoahava.com/analytics/container-snippet-gtm-secrets-revealed/#gref in the comments)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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