Where Should The Google Tag Manager Snippet Be Placed?

/

blog-gtm-snippet

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.

Google once recommended placing the container code immediately before the opening body tag, but the placement of Google Tag Manager is now a little different. Instead of placing it in the body section, Google Tag Manager has split the container, with one going in the head and the other in the body.

Where Can I Find My Snippet?

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

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”.

gtm-admin-screen
The following screen with your snippet should look like this.

gtm-snippet

What Does This Code Do?

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

In the first part, we see a script tag. The script inside of here is a JavaScript function that loads your specific container on your page. It does this by creating a new script tag and setting the source to the url to your Tag Manager container. This is the important part; it goes in the head.

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.

In the second part, we see a no script tag. This works as a backup, allowing you to track users without Javascript (which, will not be much.) The no script tag tells the browser, if the user does not have JavaScript enabled, then render an iframe version of the GTM Container to the page. Certain Tags and pixels can be configured to still load properly, even for those without JavaScript. This part of the snippet goes in the body, and is really only important if you care about tracking non-JS users.

Why In the Head?

Google recommends putting it immediately inside of the head and not inside of any other html element. Why? It’s better for tracking all around. The higher up in the page the snippet is, the faster it is loaded. In the older version of Google Tag Manager, it was recommended to place the snippet in the body tag, but this would potentially miss tracking users who left your page before the body tag loaded.

This has become even more important with the release of Google’s new A/B testing tool, Optimize. Whether you’re running a redirect test or and A/B experiment, having the snippet load faster ensures that Optimize will load the correct version of the page that your user is supposed to see, as soon as possible.

Place Your dataLayer Above the Container Code.

We need to put the dataLayer somewhere now. If you’re moving your code, make sure you also move your dataLayer. 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.

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 head code.

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

While it is not recommended, it is possible to put the Google Tag Manager script and no script tags in the body section, as shown below. If you are planning on, or already using GTM to run A/B testing, then you should place the top snippet in the head to make sure your experiments run smoothly.

So do I need to change at all?

Well, not right away. Maybe you’ve already installed Google Tag Manager and don’t want to dedicate precious resources to making this change. While the older method still works, it may not be as accurate in tracking your users and may affect any current or future testing.

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, so your Analytics and marketing tags can load as soon as possible. This helps to ensure that you capture as many users as possible.


That’s it, it’s pretty simple. Our recommendation is to follow Google Tag Manager’s instructions and place the GTM container code as close to the top of the head of your page as possible.

Athena Ermidis is our marketing intern at LunaMetrics. Originally from Ohio, she is now enjoying her time in Pittsburgh learning about data analytics, email campaigning, and all the nerdiness Luna has to offer. She loves traveling, musical theater, drinking tea and exploring the city. Coincidentally, her all-time favorite color is yellow.

  • 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!
      -Jon

      • 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!
      -Jon

  • 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.

      -Jon

      • 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?

    https://uploads.disquscdn.com/images/ba43d4bc5487e906fbb50b23abb5c50ef5ab03bacfa2c11643e8f49aefc63b47.png

    • Gonzalo

      Wondering same here! @jonmeck:disqus I would appreciate any input because Tag assistant for Google also tells me to put the script part into the body. Many thanks

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

        Hi there – the new GTM instructions are definitely the way to go. You don’t NEED to update, but I’d recommend it at some point. The change wasn’t too long ago. This change will help for loading tags sooner, like GA, as well as any A/B experiments. The sooner they load, the better, to help collect as much information as possible as well as minimizing flashing and site delays (if you’re running experiments.)

        The second part is meant to help handle users that don’t allow JavaScript. There are certain Tags that you can fire for people that do not have JS. But, if you’re not using those Tags or don’t care, then I personally wouldn’t worry about that part.

        Tag Assistant is just a helpful tool, but it can be wrong sometimes and is mostly for double-checking. Follow the instructions in GTM and you’ll be fine!

        • Gonzalo

          Many thanks for your prompt reply!

  • 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.

LunaMetrics

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

Follow Us

1.877.220.LUNA

1.412.381.5500

getinfo@lunametrics.com

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