Google Tag Manager (GTM) is an excellent tool to streamlime your analytics tags like Google Analytics, Facebook Pixel, Hotjar, etc… When coupled with Google Analytics, it also allows you easily setup events that you can use for tracking goals or conversions, without having to tinker with your websites code. Because of its ease of use, you can do most of these yourself without needing to involve the development team. If you have Google Analytics installed directly on your website right now and want to switch to using GTM, here’s my basic Google Tag Manager setup for Analytics.
Before we dive in, let’s just first cover some basics around what GTM is and its sections.
Google Tag Manager Basics
Google Tag Manager is not an analytics tool in and of itself. All it does is help you manage tracking snippets or other tags you want to fire on page. It allows you to manage all of that from one place, and by only installing the GTM code snippet on your website, so you don’t have to modify the code every time you want to install or change a tracking snippet.
I am going to assume you have already created a “container” for your website and know how to install GTM on your website. If you don’t, I recommend starting by reading Google’s documentation on setting up and installing a tag manager. This post only covers what to do once you have a container.
Inside your container, you will see the container is split into multiple sections in the left navigation. Briefly, they do the following:
Workspaces are a way to split work into difference streams, which comes it very handy if there are multiple people working in a container
Overview is an area that gives a status of your container. What it contains, what the recent edits were, and which version you’re on (yes, GTM has version control!).
Tags are essentially the snippets of code we want added on our pages (like Google Analytics, or other custom tags like Facebook Pixel).
Triggers decide decide when to fire specific tags. Tags only fire if the trigger condition matches. This is what we use to create filters based on page URL, path, domain, etc…
Variables hold values or even JavaScript functions which you can reference in other places around GTM. Google Tag Manager comes with some built-in variables which you can enable, or create custom ones.
Folders are simply a way to keep your container organised. I have seen folders be set by section on large sites, or by geographical region for a site controlled by multiple geographical teams.
Templates are premade selection of tags and variables which are contributed by the community. You can browse them to see if there are any of your interest. We’re not going to dive into Templates as we will not use them today.
If you want to read more about any section, a quick Google will bring you lots of good resources. Now that we have all that out of the way, let’s get started with our setup!
Basic Google Tag Manager setup
We are going to start bottom-up, i.e. from the Variables. Let’s first our first variable.
1. Google Analytics ID Variable
Create a new variable by going to Variables > New. Name it “GA ID” (or whatever you fancy), and select the variable type as “Constant”.
Now, instead of typing our ID everywhere, we can just reference this variable.
2. Variable to redact Personally Identifiable Information
Next, we will create another variable to remove Personally Identifiable Information (PII) from the hit. This ensures you never send any personal information (such as names or email addresses) to Google Analytics; which is very important for reasons like GDPR compliance.
So let’s create a new variable, and call it “customTask – Remove PII from hit”. Set the variable type to “Custom JavaScript”. We’re going to copy the code from this solution by Brian Clifton; which extends on the original solution by Simo Ahava.
This script redacts any PII recognised so that it doesn’t get passed onto Google Analytics (which is also part of Google Analytics terms and conditions). We’re going to use both these variables in our next variable.
3. Google Analytics Settings Variable
The last variable we will create is the main settings variable for Google Analytics. GTM has a specific type available for this, which includes custom options for Google Analytics. You can find this by setting the variable type to “Google Analytics Settings”, and I’m going to name this “GA Settings”.
Under “Tracking ID”, you can call in the variable we created for our ID, rather than typing it again. You can do this by enclosing the variable name in double curley braces or by clicking on the +
icon next to the field and selecting the variable you want; so type or select {{customTask - Remove PII from hit}}
.
We will also expand More Settings > Fields to Set and add a field named “customTask” and set the value to the PII removing variable we created. This means that custom JavaScript will be fired whenever this Google Analytics settings variable is called.
If you are familiar with content groups, custom dimensions, custom metrics or the other advanced analytics options in Google Analytics, you will notice those options available here too. But we’re not going to dive into those here.
Now we should have three variables created, and that’s all we need from the variables section. We can now setup our main tracking tag.
4. Google Analytics Tag
This step is very easy, as GTM gives us a specific tag type to do the job. To set this up, we need to navigate to Tags > New. We can name it something like “GA Main Tag” and set the tag type to “Google Analytics: Universal Analytics”.
We set the Track Type to “Page View” and select our {{GA Settings}}
variable as the Google Analytics Settings. As you may have guessed, you can select different tracking types based on what you are trying to achieve (like tracking events which we will cover further on). Under Triggering, you can select the default “All Pages – Pageview” trigger, as that will apply to all pages which is what we need. We will look at custom triggers too soon.
If you’ve got this far, then congratulations! You can now save and publish (submit) this container, and you have Google Analytics now running through a Google Tag Manager setup. You can now use Google Tag Manager for more tracking, like finding broken backlinks.
Make sure you actually submit the container otherwise your changes are not published, and also make sure that you have installed the GTM snippet on your website (and removed the Google Analyics snippet). You can also click on the Preview tool and then visit your website, to preview the tags. You will see a view window pop up and it will give details of which tags were fired (and it only happens for you).