Analytics Blog
Top Google Tag Manager Optimization Tips
Welcome to the first part in a two-part series on Google Tag Manager (GTM) optimizations. Whether you’re an experienced analytics developer or just venturing into Google Tag Manager, we hope you’ll pick up some useful GTM tips and tricks designed to help you do more with less code.
Let Me Paint You a (GTM) Picture
You’re knee-deep in a website replatform project. Data layer specs you spent your evenings writing have been handed to developers, and now endless Jira tickets have been raised. Things don’t feel very organized, but let’s face it, this is Jira.
However, you want your GTM container to be organized and optimized. That’s your domain, it’s what you control, and it will be neat and tidy! So what better time to conduct some optimizations than during a website replatform?
If we’re being honest, often these are the only chances we get to do some housekeeping. So let’s go through some of our top Google Tag Manager tips and tricks that we look for when working on our client’s GTM containers. Most of these are really straightforward quick wins — nothing complex.
Tip 1: Replace if / else and Switch Statements with Lookup Tables
As you trawl through the pieces of custom code in your container, you make a discovery. You see a Custom JavaScript (JS) variable that looks like this:
This is a relic. A variable from your early days as an analyst when you first learned JavaScript (JS) and before you rose to the rank of Tag Master (I want that job title). The JS is crude and the formatting haphazard. You’re much more meticulous these days. It’s been so long that you aren’t sure if you wrote this.
Now, we’ve all been there, especially those of us who are a dab hand with JavaScript or first learning to code. We gravitate to writing code for every possible problem because we either find it fun or it’s weirdly easier.
You inspect the code. Years of experience have taught you that less code is better, not more. This seems like a perfect candidate for some container optimization! And you know the perfect variable to do it.
The trusty lookup table. It’s so often overlooked, as well as its nerdy brother, the regular expression (regex table).
You don’t need to do pros and cons for this, but you run through them in your head anyway should you need to justify your actions to management.
Using a lookup table, here offers a few advantages.
- It’s codeless which means it’ll take up less space in the container
- It’s less likely to run into syntax errors (again because it’s codeless)
- It’s easier to update in the future (because… codeless)
- And it’s a nice catch-all rule using the default value
And voila! An ugly piece of JavaScript from the good old days is replaced with a safe and easier-to-understand built-in lookup table. You saved container space, avoided errors, and made future modifications easier to make. Take the week off!
But in all seriousness, if you’re looking for some quick wins, take to heart this first GTM tip and go through your custom JS variables, look for if/else or switch statements, and see if you can convert them to lookup tables.
Tip 2: Extract Cookie Values with Regex Tables
This second Google Tag Manager trick is cool and builds on the codeless theme, while also giving the lookup table’s nerdy brother a chance to shine.
This time you come across a funky piece of code that is fetching a cookie value. That’s weird, you think. GTM has in-built support for first-party cookies, so why would I have done this with code?
After some deciphering of the JavaScript, it dawns on you. You need to extract a value from within the cookie value itself. The cookie contains lots of other information separated by a delimiter, and you just need one bit of it.
It looks like the interaction count from the OneTrust cookie. OneTrust is a consent management solution and the OptanonCookie contains a nifty interactionCount value that can be useful to collect.
Normally, this kind of thing could (or should) be put in the data layer, the bastion of a clean implementation, but life is often a little trickier when dealing with third-party tools.
You’re using methods like split to get an array of the values in the cookie and then loop through them and look for the interactionCount value.
If the value was standalone in the cookie, your primary course of action would be to just create a GTM First-Party Cookie variable and read that, but this requires you to slice up the value with the split method.
Ugh…flashbacks…an error burned into your brain “Uncaught exception: .split is not a function.” You now have an aversion to split that’s hard to shake.
So you’re not immediately sure what the optimization is, but you know this needs to be optimized. Maybe you can’t quite justify it right now, but the nature of this code scratches at your brain, annoying you.
You notice that you’re using a regex test in the code…light bulb…could you use a regex table instead?
Well, yes, you can. Here’s how to do it:
The input variable should be the first-party cookie variable for the OptanonConsent cookie. Then use a regex expression with capture groups to get the value you want.
Wow, that’s two pieces of old code ditched in one day. You’re on a roll!
Again, being serious here, go through JS variables, look for things you’re doing with cookies, and see if you can accomplish the same thing without code.
Tip 3: Dynamic Cookie Names
Extracting cookie values using regex is great, but what if you wanted to get the cookie name because it’s dynamic? Crazy thought, right? Well, it can happen. And this is where the third Google Tag Manager tip comes into play.
So it’s nearly the end of your day, you’ve had some great wins, and lookup tables are your new thing. You feel super smart for writing less code, which is weird in itself.
You’re looking for more variables where you’re doing things with cookies and JavaScript, because that’s your low-hanging fruit for optimization. Then you come across a frightening snippet:
Thankfully, you don’t have to decipher it. You’re merely reminded of it because it came up recently when doing a cookie review for the implementation of your consent management solution. It’s a memory you wanted to forget.
Okay, but don’t give up now. You’ve explored strange new optimizations already today, sought out new solutions, and boldly went where no analytics developer went before. Why stop now? You only have a wife and kids at home. I’m sure they’ll understand.
Just break it down: the part of this cookie that is troublesome and requires code is that it contains the customer ID from the IBM Websphere Commerce platform. So you can’t use an in-built first-party cookie variable in Google Tag Manager to get it, since the customer ID is different every time.
In the code, you’re looking through all cookies for any names containing “WC_USERACTIVITY_” and selecting the one you find. Then you’re splitting the name of the cookie up to get the customer ID.
This is going to require a 4 p.m. cup of coffee — preferably intravenous.
Now that you’re, caffeinated, it’s time to get back to it. Let’s start by grabbing all of the browser cookies, then we can easily search and split where we need to. In this case, you decide to create a Javascript Variable which captures ‘document.cookie.’
Once you’ve got that, how do you search through it? Lookup table’s nerdy brother again? Whip out the regex table? Why not, give it a try.
Then you just need to write the regular expression.
The first capture group is your universal catch-all. This will handle any cookies in the string set before the one you want to use.
The second group is your initiator. This pulls the first part of the cookie name, the part that always exists before the dynamic part of the name is set.
The third group is the value you want. This is the dynamic part of the cookie name and the data we’re after. We’re also limiting what could be collected here to make sure we don’t get any bad data. (We could use the same numeric filter in the above tip, but we like to show different solutions where we can.)
In this case, we’re actually getting data from the cookie name itself, but you could modify the regex to get the data from the value too.
Post-GTM Tips Retrospective
What a stroke of genius, and it’s only just turned 5 p.m. You feel like a hero and really want to tell someone about all the cool optimizations you’ve done today. But while you pack your bag, you realize that few people understand the intricacies of being an analytics developer.
Why not connect with the implementation team here at Blast in the comments and tell us about all the cool Google Tag Manager tips and tricks that you’ve done that no one else at your company understands. We’re here for you!
Stay tuned for part two…