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

  1. In GTM, navigate to Variables > User-Defined Variables > New.
  2. Click on Variable Configuration.
  3. Select Data Layer Variable.
  4. Set Variable Name to dlv_test_name.
  5. Set Data Layer Variable Name to test_name.
  6. 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.

  1. In GTM, go to Triggers > New.
  2. Click on Trigger Configuration.
  3. Select Custom Event.
  4. Set Trigger Name to ab_split_test_trigger.
  5. In Event Name, enter ab_split_test.
  6. Under This trigger fires on, select All Custom Events.
  7. 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.

  1. In GTM, navigate to Tags > New.
  2. Click on Tag Configuration.
  3. Select Google Analytics: GA4 Event.
  4. Choose your existing GA4 Configuration Tag from the dropdown.
  5. Set Event Name to ab_split_test (or a name of your choice).
  6. 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: {{dlv_test_id}} (if using advanced data mode)
  7. Click on Triggering and select ab_split_test_trigger.
  8. Name your tag GA4 - ab_split_test.
  9. Click Save.

Step 4: Preview and Debug in GTM

Before publishing, it’s important to test your setup.

  1. In GTM, click Preview to enter Debug Mode.
  2. Navigate to your website where the AB Split Test plugin is active.
  3. Perform actions that would trigger the ab_split_test event (e.g., visit a split test page or complete a conversion).
  4. In the GTM Debug Console, verify that:
    • The ab_split_test event appears under Events.
    • The variables dlv_test_name, dlv_test_variation, and dlv_test_event are capturing the correct values.
    • The GA4 – ab_split_test tag fires when the event occurs.

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

  1. Go to TagsNew.
  2. Name it AB Split Test HTTP POST Conversion.
  3. 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>

  1. Under Triggering, select AB Split Test Conversion Trigger.
  2. Click Save.

Step 6: Publish Your GTM Container

Once you’ve confirmed everything is working:

  1. In GTM, click Submit.
  2. Add a descriptive Version Name and Version Description (e.g., “Added AB Split Test tracking”).
  3. 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

  1. In GA4, navigate to Configure > Custom definitions.
  2. Click on Create custom dimensions.

6.2 Create Custom Dimensions

For each parameter (test_name, test_variation, test_event):

  1. Set Dimension name to match the parameter (e.g., test_name).
  2. Optionally add a Description.
  3. Set Scope to Event.
  4. In Event parameter, enter the parameter name exactly (e.g., test_name).
  5. 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

  1. In GA4, go to Configure > DebugView.
  2. Perform the split test actions on your website again.
  3. In DebugView, verify that the ab_split_test event appears with the associated parameters.

7.2 Check Realtime Reports

  1. Navigate to Reports > Realtime.
  2. 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.

Leave a Comment

You must be logged in to post a comment.