Previously, I’ve already written about where to place Google Tag Manager code on your site. As a matter of fact, that post dedicated to GTM code placement dilemma was the very first publication of my blog and it still contains up-to-date information.
But since Google has updated official recommendations on where to add Google Tag Manager container snippet, an additional inquiry yielding new insights into the subject matter certainly would not go amiss.
What has changed
Henceforth, GTM container snippet loads in a protocol-independent manner. What this means is that regardless of your site’s protocol new snippets will always use HTTPS protocol by default, thus providing more protection to your private data and improving overall performance.
Additionally, now instead of putting the container snippet right after the opening
<body> tag Google recommends you to place it in the
head section, exactly where a loader of an asynchronous JS library belongs to.
Please do mind that you still have to place the <noscript> part of the snippet right after the opening
<body> tag and here’s why:
Thus, an official recommendation on how and where to place Google Tag Manager code looks like this:
// jQuery and other loaders
// your dataLayer declarations
window.dataLayer = window.dataLayer || ;
'key1' : 'val1',
'key2' : 'val2'
<!-- Google Tag Manager -->
new Date().getTime(),event:'gtm.js'});var f=d.getElementsByTagName(s),
<!-- End Google Tag Manager -->
<!-- Google Tag Manager (noscript) -->
height="0" width="0" style="display:none;visibility:hidden"></iframe></noscript>
<!-- End Google Tag Manager (noscript) -->
Will my current implementation still work?
YES, in case you have Google Tag Manager container snippet placed right after the opening
<body> tag, this implementation will work just fine. As a matter of fact, you can inject the code however you like. But still…
Why should you update your GTM implementations right away
I guess reasons for changing the snippet location are quite obvious. But in order to get a full insight into the matter, let’s take a closer look at the benefits of putting Google Tag Manager container snippet in the
- The sooner the better
As you place your GTM container snippet as high in the
head section as possible, you obviously narrow down the possibility of losing data because the earlier you fire your tags the earlier tracking process starts.
- Preparing for the sync snippet rollout
An option for a synchronous GTM loading has been promised a more then a year ago (see this video). Though at the moment it’s still in beta, the snippet’s transferal helps future-proof enabling the option. Especially amid the upcoming rollout of Google Optimize.
- Easier to implement/migrate
Oftentimes, it’s much easier to paste a given snippet of code in the
head section as it’s more accessible. Plus, in this case ga.js migration gets smoother.
- Higher level of security
As mentioned above, GTM now uses HTTPS protocol, which protects your data from malicious parties.
Obviously, the announcement of these changes in the conjunction with the upcoming Google Optimize presentation presages a release of a long-awaited feature – synchronously loading tags.