Where Should The Google Tag Manager Snippet Be Placed?/
December 12, 2014
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.
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.
What Does This Code Do?
Let’s examine the container code and see what it’s actually doing.
<!-- Google Tag Manager -->
height="0" width="0" style="display:none;visibility:hidden"></iframe></noscript>
new Date().getTime(),event:'gtm.js'});var f=d.getElementsByTagName(s),
<!-- End Google Tag Manager -->
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
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.
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.
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.