Blast Analytics and Marketing

Analytics Blog

Supporting Leaders to EVOLVE
Category: Digital Analytics

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:

custom JavaScript variable code

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.

gandoff google tag manager meme

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

Lookup table variable configuration code

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!

Adding a variable to a data layer

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.

Custom javascript variable configuration code

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:

Regex table code configuration

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.

fortune cookie with regex table fortune

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:

example of custom javascript for cookies

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.’

Javascript variable that captures browser cookies

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.

meme showing the benefits of using a regex table

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.)

regext table configuration in GTM

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…

Joshua Barratt
About the Author

Joshua Barratt is a Senior Analytics Implementation Consultant at Blast Analytics. He has a unique blend of skills in both web development and analytics ensuring best-in-class strategy and implementation; from solution design through development, QA and deployment. He specializes in digital analytics, particularly Adobe Analytics, Google Analytics and a variety of tag management solutions.

Connect with Joshua on LinkedIn. Joshua Barratt has written on the Blast Digital Customer Experience and Analytics Blog.

We’re here to help with tips and insights on the following topics:

Data Management Digital Analytics Digital Experience Digital Transformation Marketing Activation User Privacy
HIPAA and Analytics White Paper CTA

Featured White Paper

Healthcare Analytics and HIPAA: Ways to Minimize Risk and Ensure Compliance

The rise in digital data and analytics adds complexity and risk for healthcare organizations. Those that don’t comply with data privacy requirements, including Health Insurance Portability and Accountability Act (HIPAA), could face heavy fines, civil action lawsuits, and even criminal charges. Not to mention loss of patient trust.

Download the White Paper

Ready To Do More With Your Data?

If you have questions or you’re ready to discuss how Blast can help you EVOLVE your organization, talk to an Analytics Consultant today.

Call 1 (888) 252-7866 or contact us below.