Google Tag Manager + GA4 set up guide
Introduction
This guide will walk you through integrating AB Split Test with Google Analytics 4 (GA4) using Google Tag Manager (GTM). By following these steps, you’ll be able to track split test visits and conversions in GA4, leveraging the dataLayer
events pushed by the plugin.
The AB Split Test plugin pushes the following dataLayer
object on split test visits and conversions:
window.dataLayer.push({
'event': 'ab_split_test',
'test_name': Your test name,
'test_variation': Page ID or variation name,
'test_event': 'visit' or 'conversion'
'abuuid': UUID of user
});
Prerequisites
- Access to your website’s Google Tag Manager (GTM) account.
- A Google Analytics 4 (GA4) property set up.
- The AB Split Test plugin installed and configured on your website.
- If you want to trigger conversions with tag manager – enable advanced mode in Settings > AB Split Test > Advanced Data Mode
Step 1: Set Up Data Layer Variables in GTM
First, we’ll create variables in GTM to capture the custom parameters (test_name
, test_variation
, test_event
, {abuuid}) from the dataLayer
.
1.1 Create Data Layer Variable for test_name
- In GTM, navigate to Variables > User-Defined Variables > New.
- Click on Variable Configuration.
- Select Data Layer Variable.
- Set Variable Name to
dlv_test_name
. - Set Data Layer Variable Name to
test_name
. - Click Save.
1.2 Create Data Layer Variable for test_variation
Repeat the steps above with the following details:
- Variable Name:
dlv_test_variation
- Data Layer Variable Name:
test_variation
1.3 Create Data Layer Variable for test_event
Repeat the steps again with:
- Variable Name:
dlv_test_event
- Data Layer Variable Name:
test_event
1.4 Create Data Layer Variable for abuuid
(optional)
If using advanced data mode (Settings > AB Split Test > Advanced Data Mode), you can also accept the UUID generated for the user, to later trigger a test conversion via an Event in Tag Manager
Repeat the steps again with:
- Variable Name:
dlv_abuuid
- Data Layer Variable Name:
abuuid
1.5 Create Data Layer Variable for abuuid
(optional)
If using advanced data mode, you’ll probably also want to collect the test ID so you can pass that back with the UUID.
Repeat the steps again with:
- Variable Name:
dlv_test_id
- Data Layer Variable Name:
test_id
Step 2: Create a Custom Event Trigger in GTM
Next, we’ll set up a trigger that fires when the ab_split_test
event is pushed to the dataLayer
.
- In GTM, go to Triggers > New.
- Click on Trigger Configuration.
- Select Custom Event.
- Set Trigger Name to
ab_split_test_trigger
. - In Event Name, enter
ab_split_test
. - Under This trigger fires on, select All Custom Events.
- Click Save.
Step 3: Create a GA4 Event Tag in GTM
We’ll now create a tag that sends the event data to GA4 whenever the trigger fires.
- In GTM, navigate to Tags > New.
- Click on Tag Configuration.
- Select Google Analytics: GA4 Event.
- Choose your existing GA4 Configuration Tag from the dropdown.
- Set Event Name to
ab_split_test
(or a name of your choice). - Under Event Parameters, click Add Row to add parameters:
- Parameter Name:
test_name
; Value:{{dlv_test_name}}
- Parameter Name:
test_variation
; Value:{{dlv_test_variation}}
- Parameter Name:
test_event
; Value:{{dlv_test_event}}
- Parameter Name:
abuuid
; Value:{{dlv_abuuid}}
(if using advanced data mode) - Parameter Name:
test_id
; Value:{{
(if using advanced data mode)dlv_test_id
}}
- Parameter Name:
- Click on Triggering and select
ab_split_test_trigger
. - Name your tag
GA4 - ab_split_test
. - Click Save.
Step 4: Preview and Debug in GTM
Before publishing, it’s important to test your setup.
- In GTM, click Preview to enter Debug Mode.
- Navigate to your website where the AB Split Test plugin is active.
- Perform actions that would trigger the
ab_split_test
event (e.g., visit a split test page or complete a conversion). - In the GTM Debug Console, verify that:
- The
ab_split_test
event appears under Events. - The variables
dlv_test_name
,dlv_test_variation
, anddlv_test_event
are capturing the correct values. - The GA4 – ab_split_test tag fires when the event occurs.
- The
OPTIONAL Step 5: Send a conversion event to AB Split Test
Now, we will create a GTM tag that fires an HTTP POST request to log conversions. you would add this to your own external trigger, like call tracking software etc. that is connected to Tag Manager.
Create a Custom HTML Tag
- Go to Tags → New.
- Name it AB Split Test HTTP POST Conversion.
- Choose Tag Type: Custom HTML.
<script>
(function() {
var advancedId = {{AB Split Test Advanced ID}};
var testId = {{dlv_test_id}}; // Replace with your test ID
var eventType = 'conversion'; // visit | conversion | goal
var conversionValue = 1; // Can replace with the value of the conversion
var location = 'gtm';
var yourWebsiteDomain = 'https://mydomain.com'; // update with your domain that's hosting AB split test
var data = {
eid: testId,
type: eventType,
location: location,
orderValue: conversionValue,
ab_advanced_id: {{dlv_abuuid}}
};
var xhr = new XMLHttpRequest();
xhr.open("POST", yourWebsiteDomain +"/wp-admin/admin-ajax.php?action=abst_event", true);
xhr.setRequestHeader("Content-Type", "application/json");
xhr.send(JSON.stringify(data));
console.log("AB Split Test Conversion Sent:", data);
})();
</script>
- Under Triggering, select AB Split Test Conversion Trigger.
- Click Save.
Step 6: Publish Your GTM Container
Once you’ve confirmed everything is working:
- In GTM, click Submit.
- Add a descriptive Version Name and Version Description (e.g., “Added AB Split Test tracking”).
- Click Publish.
OPTIONAL Step 7: Configure GA4 to Receive Custom Parameters
To view the custom parameters in GA4 reports, you’ll need to register them as custom dimensions.
6.1 Access Custom Definitions
- In GA4, navigate to Configure > Custom definitions.
- Click on Create custom dimensions.
6.2 Create Custom Dimensions
For each parameter (test_name
, test_variation
, test_event
):
- Set Dimension name to match the parameter (e.g.,
test_name
). - Optionally add a Description.
- Set Scope to Event.
- In Event parameter, enter the parameter name exactly (e.g.,
test_name
). - Click Save.
Note: It may take up to 24 hours for GA4 to start populating these custom dimensions in your reports.
Verify Events in GA4
7.1 Use DebugView
- In GA4, go to Configure > DebugView.
- Perform the split test actions on your website again.
- In DebugView, verify that the
ab_split_test
event appears with the associated parameters.
7.2 Check Realtime Reports
- Navigate to Reports > Realtime.
- Look for the
ab_split_test
event and verify the parameters.
Additional Tips
- Custom Reports: Use GA4’s Exploration reports to analyze the custom dimensions in depth.
- Data Retention: Configure data retention settings in GA4 and Settings > AB Split Test > UUID Validity to keep your event data as long as needed.