Learning: AB Testing with GTM

Hey guys,

Since the Google Optimize sunset announcement, I’ve been searching for other solutions.

Furthermore, it seems there won’t be an AB testing tool on GA4:


Since free AB Testing tools have a lot of limitations, I’ve been doing some AB Tests directly on GTM.

1) User-Defined Variable (Custom Javascript):

This AB Test is based on cookies, so we need to use some javascript for this.

function(){
  var variation = 2;     // number of variations
  var test_variation;
  
  if(localStorage.getItem("test_variation")){
    test_variation = localStorage.getItem("test_variation");
  }else{
    var random = Math.floor(Math.random() * variation);
    test_variation = 'variation' + (random + 1);
    localStorage.setItem('test_variation', test_variation);
  }
  return test_variation;
    
}

2) Configuring GA4:
create a property name with the value of the variable that was set.

3) Trigger: Experimental Group
This is the trigger for the experimental group.

4) Custom Script - Variation:
Here we use a custom javascript for the variation we want to test.
In this example, I just changed the color of the button.

To do a redirect test:

window.location.replace("http://www.redirect.com");

It's just a quick solution. If you want more options, I found this script: https://github.com/thenextweb/cro.

I hope it can be useful!

Last edited by @hesh_fekry 2023-03-21T13:01:42Z

2 Likes

Thanks for sharing this @waboratto