Mobile app version of vmapp.org
Login or Join
Caterina187

: Google Tag Manager Position in html 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

@Caterina187

Posted in: #Google #GoogleSearchConsole #Seo

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 ?

10.01% popularity Vote Up Vote Down


Login to follow query

More posts by @Caterina187

1 Comments

Sorted by latest first Latest Oldest Best

 

@Megan663

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. www.simoahava.com/analytics/container-snippet-gtm-secrets-revealed/#gref in the comments)

10% popularity Vote Up Vote Down


Back to top | Use Dark Theme