Where Should The Google Tag Manager Snippet Be Placed?



Perhaps this sounds familiar: Your team has just decided to start transitioning to Google Tag Manager. However, you’re stuck on where you need to place the container code.

Traditionally, you’ve placed the Google Analytics immediately before the closing head tag, or perhaps it’s even still in the footer. (gasp!)

With Google Tag Manager the placement is now a little different. Instead of placing it in the head section, Google recommends putting the container code immediately after the opening body tag.

Where Can I Find My Snippet?

Google provides you with the exact code and instructions for where to add it to your website.

For Google Tag Manager Version 1, you can find the snippet code and their instructions on the left navigation menu under Admin -> Install GTM.

Screen Shot 2014-12-01 at 3.07.00 PM

In Version 2, the installation code is found along the top navigation, under the Admin option. Then, under the Container Settings, there’s an option for Install Google Tag Manager.

You may notice that Version 2 has a very similar layout to Google Analytics.

Screen Shot 2014-12-12 at 9.42.06 AM

What Does This Code Do?

Let’s examine the container code and see what it’s actually doing.

The first thing we see is a no script tag. The no script tag is tells the browser, if the user does not have JavaScript enabled, then render an iframe version of the GTM Container to the page.

The next thing we see is a script tag. The script inside of here is a JavaScript function that loads your specific container on your page. It does by creating a new script tag and setting the source to the url to your Tag Manager container.

It also tells your page to load your GTM container asynchronously, which means that the rest of the page can continue loading while Google is doing its magic.

Why In the Body?

Google recommends putting it immediately inside of the body and not inside of any other tag. One of the reasons may be so that older versions of Internet Explorer do not run into issues with appendChild(). Older versions of Internet Explorer are known to have issues with JavaScript. Google is recommending this code placement to avoid any issues.

Along with Google’s recommendation here are a few other reasons and tips for your implementation:

Load your important information first.

Putting the GTM container code immediately after the opening body tag is important for a few reasons. The pageview tag relies on the title tag of the page, which is found in the head section of your site. Content experiments are setup in the head of the page as well. Having the GTM snippet in the body guarantees that these are taken care of.

Place your dataLayer above the container code.

We need to put the dataLayer somewhere now. You may be tempted to add your dataLayer after your container code. This is problematic and will cause issues when you try to push data into the dataLayer. If you place the dataLayer after the container code it will end up overwriting the dataLayer that the Google Tag Manager code creates for you, and you may miss out on having important information available when your first Tags try to fire.

Do not place the container code inside of other HTML tags.

The reason we don’t place the container code inside of other html tags is because of older browsers and their JavaScript engines. Older versions of Internet Explorer have an issues appending HTML tags that are dynamically generated. The script tag that the container code generates will be appended outside of the head section which is not what we want.

Worst Case Scenarios

We hear it all the time. Maybe your website is using a template or you don’t have the appropriate permissions to modify the body code.

Can I put the GTM snippet in the head section of my page?

While it is not recommended, it is possible to have the Google Tag Manager script in the head section. However, this requires that you do not place the noscript tag in the head. The following screenshot demonstrates this method.

Screen Shot 2014-12-12 at 11.09.49 AM

As you can see the container loader script is placed at the very bottom of the head section. We then place the noscript tag and its contents immediately after the opening body tag. By doing this, we are allowing browsers with JavaScript enabled process the GTM script as normal without having to parse the noscript tag while loading the page.

This method works, however it’s not officially supported by Google.

Can I put the GTM snippet in the footer section of my page?

This is highly discouraged. For the same reason that we recommend putting Google Analytics in the head section, you want your GTM container to load as soon as possible. This enables you to begin tracking users right away, and ensures the maximum amount of tags are fired.

That’s it, it’s pretty simple. Our recommendation is to place the GTM container code in right after the opening body tag. That way there’s no custom work to implement the GTM container. Since GTM was designed for non developers it only makes sense to keep the custom development to a minimum.

Justin is a former LunaMetrician and contributor to our blog.

  • Alex Vida

    Thank you, this article was very helpful! 🙂

  • mhopps

    Thank you!

  • Mark H

    Thank you.
    However, what about scripts that ask to be loaded in the head, or the body, etc that I plan to put in GTM container? Does it then not matter where the GTM container is placed?

  • Brando

    If you install the container in the does that mean you have to manually do it to every page or do it with a PHP file? That is why I liked installing the analytics in the because it is unversal to every page of the site so you only had to copy and paste once. Correct me if I’m wrong that you would have to copy an past to every URL of the site in the Body tag or .php method for this to work. I’m a newb.

    • Alex Dunks

      Often times your header.php file will include the opening tag, so you can place it right after. As long as your opening tag is within a PHP template (not editable at an individual page level), you will be fine – just need to find where that is within your PHP templates – usually header as I mentioned.

      What drives me crazy is Google tells us to put GTM in the , and then Google Tag Assistant registers an “error” because the GA code is not in the head. I’m just following your instructions and using your products Google!!

  • Davide B

    Hello, is it possible to install GMT container without datalayer? I did it in the body, but Google Tag Assistant is not showing anything yet (no gtm tag fired). Is it because there are no tags in GTM containers yet?

  • https://www.ultimez.com/ Tabassum M

    Hi. Can anybody tell me, whether tag manager script is needed in every page? if yes, whether it is different from one page to another or same for all the pages of website?

    • http://www.lunametrics.com Jon Meck

      Hi Tabassum,

      Yes, Google Tag Manager code is placed on every single page, exactly the same, usually in some sort of template file or easy to maintain include file. The basic GTM code is exactly the same, but if you ever progress to using a Data Layer, that might be different on each page, but should be dynamically inserted.

      Hope this helps!

      • https://www.ultimez.com/ Tabassum M

        Thank you very much john. It was very useful for me. another doubt I have,

        I didn’t get, “but if you ever progress to using a Data Layer, that might be different on each page, but should be dynamically inserted”

        can you please explain me???

        • http://www.lunametrics.com Jon Meck

          Sure – here’s a great blog post for you to check out. http://www.lunametrics.com/blog/2013/10/15/unlock-data-layer-google-tag-manager/ The Data Layer is a place for you to put information about a particular page or user which can then be used inside of Google Tag Manager. For instance, on a blog, you might store the author of the current post in the data layer, so that it can then be sent to Google Analytics.

          This is a little more complicated and usually involves a developer, and is not necessary for the just setting up basic Tags, Triggers, and Variables.

          • https://www.ultimez.com/ Tabassum M

            yup… Thanks John.

  • Michael C.

    What happens when GTM is being abused and loads an excessive amounts of TAGs, scripts custom HTML container (containing large amounts of javascript) and several A/B testing? Having the code snippet in my opening body tag causes the page to flicker every time the user changes pages. Also it spikes the browsers CUP usage to 100% for a short time.

    However if I place it at the bottom with the rest of my Javascript things seem to speed up drastically and the browser no longer flickers on page load.

    • http://www.lunametrics.com Jon Meck

      Hi Michael,

      The recommendation is ideal for most Tags, as we want them to load as soon as possible. Different firing triggers can be used to dictate when Tags fire, “as soon as possible” = Pageview, or “after everything else” = Page Load. We’d prefer GTM to be at the top so we can make sure that Google Analytics and our conversion tags absolutely fire.

      If the snippet is placed at the bottom, then you run the risk of a user landing on the page and leaving, either to go another internal page or bouncing entirely, before Google Analytics has a chance to send off a hit. This could result in less than accurate Pageviews, Users, and Session, as well as a decrease in attributed sessions (if the first hit is missed in a session.)

      If you have access to GTM, I’d recommend strategically firing Tags at the correct time, placing high value tags first and less important Tags after the rest of the page content has loaded.

      Hope this helps!

  • TheDoc

    Hi, one thing I don´t get: I thought that once I installed the google tag manager, I can easily install analytics through / with the help of google tag manager. But if the tag manager pixel is placed in the body and the analytics code is supposed to be placed in the header… you see what i mean?

    What would you recommend?

    • http://www.lunametrics.com Jon Meck

      Yes, these two instructions seem slightly at odds with each other. Ideally, the GA tracking code fires as soon as possible. Because GTM is happening farther down on the page, it may add a tiny delay to GA code – however this risk has obviously been deemed acceptable by Google’s standards.

      GA is often placed at the bottom of the head, and GTM at the VERY TOP of the body. So in that case, the delay would very small.


      • TheDoc

        Hey Jon, thanks a lot!

  • http://macinfosoft.com Macinfosoft

    I am trying to put the Google Tag Manager in PHP pages. It is not wordpress. Normal PHP script. Could you please let me know where to put that code?

  • http://faisalpervaiz.com Faisal Pervaiz

    I am little confuse : Do we need to place GTM code on every page or we can select “ALL Pages” from GTM dashboard?
    Lets say I have installed Google analytics through Google Tag manger but I wanna track all the pages of my site. How will it work?

  • CarlG17

    What can be done if the architecture of the site utterly prevents loading important information before the head tag? The site design I am using is old and was not made with that requirement in mind, the tags, in order to be filled with the relevant data has to be loaded along further down on the page.

    Putting the tag manager snippet code at the bottom is the only solution I can think of.

  • Hector Olguin

    Hello. Thanks for the info. I was just checking on my GTM code in GTM and I noticed that install instructions say to place two code snippets, on in the header and one in the body. When did this happen? Do I need to change all my implementations now? Here is the quick start page that I read: https://developers.google.com/tag-manager/quickstart

  • chrilles

    Now it looks like there are 2 script we need to insert. One in the head section and one just after Body-start.

    When did that change and what difference will it make if I skip out the second part after body-start? Do I need to update all my sites?


  • http://www.digital1.se Carl Zetterberg

    What if I need to put a code snippet to via Google Tag Manager. Is that possible and if, how? 🙂

Contact Us.


24 S. 18th Street, Suite 100,
Pittsburgh, PA 15203

Follow Us




We'll get back to you
in ONE business day.