How to add Analytics Experiment Code (A/B Testing) in Google Tag Manager
Posted: Tue Apr 22, 2025 4:05 am
In the last few days I came across a challenging situation in my work routine: I needed to create an A/B Test in Analytics and implement the code in Google Tag Manager (GTM) .
At first, it seemed like an easy task, until the time came to implement it. Given the problem that arose, I decided to share with you how I managed to solve the challenge of creating an A/B test and implementing the code in Tag Manager correctly.
My challenge began with an SEO analysis I was performing on one of my clients' websites. I discovered that there was a product with two different argentina mobile database versions of layout, content, and offer. In a way, it was an A/B test being performed incorrectly. After all, the way the user accessed one version or the other depended on whether they had clicked on the product on the home page or on the product within its category.
Yes, we needed to fix this and create a fair A/B test to see which version performed better in sales. And that's what we did.
If you already have knowledge of A/B Testing you can skip to:
How to implement Analytics experiment code via Google Tag Manager.
After all, what is A/B Testing?
A/B testing is a method used to compare two versions of a page or email, for example. The goal of creating an experiment is to analyze which version generates the highest conversion. In A/B testing, it is possible to test several variables on each page, such as content, color of a CTA (call-to-action) button, images, and different offers.
ab test
The main benefit of A/B Testing is that it allows you to make decisions based on real data that was generated by your audience.
There are currently several specialized tools on the market that perform this type of test, such as Google Analytics (GA), Google Optimizeze (still in beta), Optimizely and others.
Starting A/B Testing
As it is a free tool, we chose to create the A/B Test via Google Analytics.
With the test created, it was time to implement the experiment code on the website. However, the client was using GTM for tag management, which meant we needed to implement the A/B Test code in Google Tag Manager. And that's when the problem arose.
At first, we installed the code in Analytics in what we believed was the correct way:
GA was not identifying the experiment code. After some research, we discovered that GTM was implemented incorrectly on the site. There was an update to the GTM implementation and now the tags must be implemented in the following order: the first at the beginning of the header and the second at the beginning of the body.
We fixed the GMT tag implementation and ran the experiment. And once again, wow! GA still wasn't recognizing the experiment code and still couldn't capture the data from the original and variant pages.
How to Implement Analytics A/B Testing Code in Google Tag Manager
After much research, we finally solved the problem and discovered that the A/B Test code should be implemented as follows:
1st – Check if the GTM tags are implemented in the order: the first at the beginning of the header and the other at the beginning of the body. Access the implementation guide !
test-implementation
Ultimately, the trick was to enable support for document.write. Support for document.write is important because it allows JavaScript snippets to update content directly in the browser, allowing the experiment code to redirect the user to the original or variant page.
At first, it seemed like an easy task, until the time came to implement it. Given the problem that arose, I decided to share with you how I managed to solve the challenge of creating an A/B test and implementing the code in Tag Manager correctly.
My challenge began with an SEO analysis I was performing on one of my clients' websites. I discovered that there was a product with two different argentina mobile database versions of layout, content, and offer. In a way, it was an A/B test being performed incorrectly. After all, the way the user accessed one version or the other depended on whether they had clicked on the product on the home page or on the product within its category.
Yes, we needed to fix this and create a fair A/B test to see which version performed better in sales. And that's what we did.
If you already have knowledge of A/B Testing you can skip to:
How to implement Analytics experiment code via Google Tag Manager.
After all, what is A/B Testing?
A/B testing is a method used to compare two versions of a page or email, for example. The goal of creating an experiment is to analyze which version generates the highest conversion. In A/B testing, it is possible to test several variables on each page, such as content, color of a CTA (call-to-action) button, images, and different offers.
ab test
The main benefit of A/B Testing is that it allows you to make decisions based on real data that was generated by your audience.
There are currently several specialized tools on the market that perform this type of test, such as Google Analytics (GA), Google Optimizeze (still in beta), Optimizely and others.
Starting A/B Testing
As it is a free tool, we chose to create the A/B Test via Google Analytics.
With the test created, it was time to implement the experiment code on the website. However, the client was using GTM for tag management, which meant we needed to implement the A/B Test code in Google Tag Manager. And that's when the problem arose.
At first, we installed the code in Analytics in what we believed was the correct way:
GA was not identifying the experiment code. After some research, we discovered that GTM was implemented incorrectly on the site. There was an update to the GTM implementation and now the tags must be implemented in the following order: the first at the beginning of the header and the second at the beginning of the body.
We fixed the GMT tag implementation and ran the experiment. And once again, wow! GA still wasn't recognizing the experiment code and still couldn't capture the data from the original and variant pages.
How to Implement Analytics A/B Testing Code in Google Tag Manager
After much research, we finally solved the problem and discovered that the A/B Test code should be implemented as follows:
1st – Check if the GTM tags are implemented in the order: the first at the beginning of the header and the other at the beginning of the body. Access the implementation guide !
test-implementation
Ultimately, the trick was to enable support for document.write. Support for document.write is important because it allows JavaScript snippets to update content directly in the browser, allowing the experiment code to redirect the user to the original or variant page.