June 2, 2017
How to Install Google Tag Manager on your Website Properly
Even though this topic (How to install Google Tag Manager) might sound way too basic for some of you, remember that there are nuances you should keep in mind. In this blog post, I’ll describe several ways how to install Google Tag Manager on a website, highlighting pros and cons of each option.
Where’s the problem?
Google once recommended placing the container code immediately after the opening <body> tag, but in late 2016 the recommended Google Tag Manager code placement became 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.
But first, let’s find our GTM snippet.
Where can I find Google Tag Manager code?
Google Tag Manager provides you with the exact code and instructions where to add it to your website. There are two places where you can find it:
- By clicking container ID link (located next to Publish and Preview buttons)
- Or by going to Admin > Account > Container > Install Google Tag Manager
Any of these options will open instructions how to install Google Tag Manager code on your website.
Follow the instruction and place the first code (which is surrounded by <script> tags) somewhere between <head> tags of your website (in case you don’t know, website’s code consists of head and body). This is the key part. It 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.
Important: While you can place both these codes in <body> only, you must not add them both in <head>. As it was mentioned before, <noscript> part renders an iframe, which is not allowed in <head>, otherwise you’re website’s HTML will be invalid.
Why should I place Google Tag Manager code in <head>?
While this is not 100% required, it is highly recommended that you place <script> part of GTM code into <head> of your website.
Why? It’s better for tracking. 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 users who left your page before the body tag loaded (due to slow page speed).
If your website is pretty heavy, it might take more than few seconds for GTM to start loading in <body>, but in <head> it would be initiated much faster, thus you could catch more visitors and see how they behave.
This could empower you to identify the percentage of traffic which bounces before even the page completely loads.
Remember, the lower your tracking codes are placed in website’s code, the later they will be launched.
Publish the Container
When the code is placed on your website, head over to Google Tag Manager account and publish the container. Especially if it’s empty.
Why? Because unpublished GTM container will cause an error (visible in browser’s console).
404 not found for GET: GET http://www.googletagmanager.com/gtm.js?id=GTM-XXXXX
Your browser is requesting data from Google’s servers, although that container is still not published, thus no data will be returned.
Don’t forget the Data Layer
When Google Tag Manager code is placed on your website, it automatically creates a Data Layer.
It’s like a virtual layer of your website which contains various data points (Get it? That’s why it’s call data layer.). With its help GTM is able to read, push and transfer data between your website and other tracking tools.
If you want to push some data to dataLayer when the page loads, you need to place that data above Google Tag Manager code. Like this:
window.dataLayer = window.dataLayer || ;
'registrationCountry': 'United States'
<!-- Google Tag Manager -->
<!-- End Google Tag Manager -->
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 creates for you, and you may miss out on having important information available when your first Tags (e.g. Google Analytics Pageview Tag) try to fire.
Frequently Asked Questions
So I’ve described what the perfect and recommended scenario looks like:
- <script> part of Google Tag Manager code goes to <head> (usually before the closing </head> tag).
- <noscript> part goes after the opening <body> tag.
But real-life situations are far from perfect and sometimes we have to deal with very limited circumstances. In this chapter, we’ll discuss other not-so-perfect options and what the possible outcome might be.
#1. Can I place <noscript> part somewhere else rather than right after <body> tag?
Yes, you can.
This usually happens while working with limited content management systems (CMS), which prohibit you from accessing the website’s source directly and only allows to paste tracking codes into a dedicated field. All content of that field is then placed at the very end of website’s HTML.
While it is not recommended, it is possible to put the Google Tag Manager <script> and <noscript> tags in the body section, as shown below. If you are planning on, or already using GTM to run A/B testing, then you definitely should place the top snippet in the head to make sure your experiments run smoothly.
But if you’re planning just to utilize some basic Google Analytics tracking (or something like it), then you should be fine.
But keep in mind, the lower your tracking codes are in website’s code, the later your tracking tags will fire.
#2. Can I remove <noscript> part and use only <script>
You should either put noscript part in the <body> or don’t use it at all.
#3. I Installed GTM few years ago; Both <script> and <noscript> parts are in body. Is it bad?
As it was mentioned before, prior to late 2016, Google’s recommendation was to place the entire Google Tag Manager code to <body>. This is still working till this day.
But remember that you might be losing some tracking data, because GTM (placed in <body>) starts loading a bit later compared to GTM code placed in <head>.
So if you have time and resources, I definitely recommend moving <script> part to <head>.
How To Install Google Tag Manager: Conclusion
The advice here is simple: just follow Google’s instructions.
The most recommended way how to install Google Tag Manager code is to place <script> part to <head> of your website, and <noscript> part should be place somewhere in <body> (preferably right after <body> tag).
This ensures that your marketing tags in GTM will be fired as soon as possible without much of data loss.
The lower Google Tag Manager code placement is, the later your tags will fire. As a result, you might lose a portion of your data.