6 Steps To A/B Test Using Google Tag Manager


Written by Joe Shrimpton

4 mins

18th December 2019

In part one of this series, we introduced you to the Importance of a Testing Culture within your marketing. In case you missed the article, you can check it out here.

So why bother with a testing culture? Ultimately, introducing an AB (or other) testing culture will:

  • provide you with a learning purpose to every task, even the most repetitive
  • drive a deeper understanding of what’s working and what’s not, helping you to identify new opportunities faster, reducing the time and money wasted on underperforming efforts
  • help your company to instil a new approach to being curious and push the boundaries to continually improve

In this follow up, we explore split testing in action using Google Tag Manager. It’s simple to do and the great thing is you probably already have this free technology. If you don’t, then talk to one of our friendly experts today, and we’ll get you set up.


Before we get into the how, let’s just define what it is, and why it’s important.

A/B tests are simply a way of testing two options against each other to identify which one works better. It’s an experiment where two versions of a webpage or ad are shown to users at random, and statistical analysis is used to determine which variation performed better against a given conversion goal.

This is important as it removes subjectivity from your web/ad design choices, enabling data-driven decisions to be made based on what you know, not what you think you know. It allows companies to continually optimise their user experience, learning along the way about the impact changes to their digital channels have on user behaviour.

Testing one change at a time via A/B testing enables companies to pinpoint exactly which changes had an effect on visitors’ behaviour, and which did not. Over time, they can demonstrate the effect of multiple ‘winning’ changes, for example, improving a single goal like conversion rate over time.

It all starts with:

  • Which conversion goal are you testing?
  • Conversion goals can be anything from clicking on a button or link to product purchases and email sign-ups

Next, you need to:

  • Think of A/B testing ideas and hypotheses – why will they be better than the current version?
  • Prioritize your list in terms of expected impact and ease of implementation – are there any quick wins you can focus on first?

And now to the nitty-gritty, setting up your experiment on Google Tag Manager.

The aim of this particular experiment is to split test conversion rates from two pages which users are randomly sent to via a submit button redirect.


We’ll start by building a custom Javascript variable which outputs either path1 or path2 at random. We achieve this by using the random number variable in GTM which can go up to 2147483647 and refreshes with each page view.

We’ve written the below function which simply asks, is the value less than exactly half of 2147483647? If yes, we return path1 and if no, we return path2. This provides a completely random assignment of either path.

In the GTM debug mode you can test that this works:


Next you build your trigger. As you’ll be editing the DOM (Document Object Model), you need to wait until the tag’s finished loading, otherwise you risk not being able to identify the element you’re trying to edit.

  1. 5. THE TAG

You now need to build a custom HTML tag and insert a Javascript function – this function includes the element we’re editing (in this instance, a submit button). You then need to insert the value of the ‘href’ redirect link using the ‘Test Path Select’ variable that we built earlier.


Don’t forget to tick document.write (to safely amend the DOM) and remember to place the function inside <script>….</script> tags as the tag is HTML and needs to know that it’s deploying Javascript.


Finish by connecting the tag to the trigger. If everything works correctly, your button should redirect at random to either of your two test redirect pages.

This same approach would work for changing the text within the button – and even the appearance of the button – if you were to dynamically switch CSS classes. It’s worth noting that you can place the code from the step 1 Javascript variable into the HTML tag, but we’ve found that separating them out keeps typed code to a minimum and easier to understand by the non-developers amongst us.

If youd like more information on where to start with A/B testing via Google Tag Manager, or any other marketing tests,  get in touch with one of our experts or give us a call on 01273 041111.