A/B testing is a cornerstone of optimizing digital experiences. Whether you’re testing landing page designs, call-to-action buttons, or content layouts, a well-structured A/B test can significantly impact conversion rates. In this blog post, we’ll walk you through how to set up an A/B testing script using Google Tag Manager (GTM) and send the results to Google Analytics 4 (GA4) for analysis.
By the end of this guide, you’ll have a ready-to-use script that:
Google Tag Manager simplifies deploying scripts on your website without needing direct access to the codebase. With GA4’s advanced analytics capabilities, you can measure the performance of your A/B test and gain actionable insights, such as how users interact with each version.
In our case is a very important tool to allow the QA team adjust the experiment probability and change the percentage of users going to each version.
Here’s the complete JavaScript code you’ll use in GTM:
(function() {
// Define the percentage of users entering the experiment (0 to 100)
var experimentPercentage = 50; // Example: 50% of users enter the experiment
// Define the probability of assigning users to version B (0 to 1)
var bVersionProbability = 0.5; // Example: 50% of experiment users go to version B
// Define the URLs for the A and B versions
var urlA = "https://example.com/version-a";
var urlB = "https://example.com/version-b";
// Helper function to determine if a user enters the experiment
function isInExperiment() {
return Math.random() < experimentPercentage / 100;
}
// Retrieve the stored variant from localStorage to maintain consistency
var variant = localStorage.getItem('abTestVariant');
// If no variant is stored and the user enters the experiment, assign one
if (!variant) {
if (isInExperiment()) {
variant = Math.random() < bVersionProbability ? 'B' : 'A';
localStorage.setItem('abTestVariant', variant);
// Send an event to GA4 for entering the experiment
sendGA4Event("experiment_entered", { experiment_group: variant });
} else {
variant = 'Control'; // Not in the experiment
localStorage.setItem('abTestVariant', variant);
}
}
// Redirect users based on the assigned variant
if (variant === 'A' && window.location.href !== urlA) {
window.location.href = urlA;
} else if (variant === 'B' && window.location.href !== urlB) {
window.location.href = urlB;
}
// Send an event to Google Analytics 4 for the user's variant assignment
sendGA4Event("ab_test_assignment", { ab_test_variant: variant });
// Function to push an event to the GA4 Data Layer
function sendGA4Event(eventName, params) {
window.dataLayer = window.dataLayer || [];
window.dataLayer.push({
event: eventName,
...params
});
}
})();
Follow these steps to deploy the script:
The script sends two events to GA4:
Once the data starts flowing into GA4, use Exploration Reports to analyze your test:
Setting up A/B testing with GTM and GA4 provides a powerful, flexible way to experiment and optimize your website. This script ensures you can measure participation and results accurately, giving you the insights needed to make data-driven decisions.
Need help implementing this script or analyzing your results? Contact us at Deeploy for expert assistance in A/B testing, analytics, and digital marketing optimization.