Don't install Google Tag Manager code 'as high in the <head> of the page as possible'

PUBLISHED 23 Aug 2018 - UPDATED 18 Aug 2023

I love Google Tag Manager, I really do. But when you create a new GTM container for Web, don't follow the installation instructions that pop up:

Google Tag Manager instructions

These instructions say: "Copy the code below and paste it on to every page of your website. Paste this code as high in the <head> of the page as possible."

That's not the right approach - instead, the installation code should be pasted below your data layer code. As Google advises elsewhere (in its Tag Manager installation documentation):

For Tag Manager web page installations, you must create a data layer. The highlighted code below shows where the data layer is established, before Tag Manager is loaded.

And then gives this example (I've highlighted the data layer code with a yellow background):

That's the minimum you need to establish the data layer.

dataLayer.push()

But what if you want to push information into your data layer to use it with Google Tag Manager's tags and triggers? For example, you might push a user's role (subscriber, admin and so on) into the data layer, then use it as part of a trigger - setting the analytics code not to fire if the user is an admin being one common use case.

Well, if you want that data to be available on container load, it will need to be placed above the GTM installation snippet as well.

As Google itself explains in its documentation for developers:

If you want... data layer variables to be available to Tag Manager when the container is loaded, add a dataLayer.push() call above the Tag Manager container code.

And here's Google's example for that (again (I've highlighted the data layer code with a yellow background):

Google also used to provide a Quick Start Guide to Google Tag Manager which had an even clearer warning:

If you put the Google Tag Manager container snippet above the dataLayer array, Google Tag Manager may malfunction, and will not be able to read the variables defined in the array.

Sadly the Quick Start Guide is no longer available, but the advice still stands. Don't put the GTM snippet as high in the <head> as possible. Put it below your data layer code.

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
James Clark
Hi! I'm James Clark and I'm a freelance web analyst from the UK. I'm here to help with your analytics, ad operations, and SEO issues.
0
What do you think? Leave a commentx
linkedin facebook pinterest youtube rss twitter instagram facebook-blank rss-blank linkedin-blank pinterest youtube twitter instagram