Experiment Engine

Send test versions to GA4 and other tools

Every assigned shopper is pushed to the data layer, so you can build a GA4 audience or segment for each version without connecting an account.

When a shopper is put in a test, the storefront script pushes one event to window.dataLayer, once per test and page, after the shopper accepts analytics cookies. It never runs before consent. Google Tag Manager, GA4 and heatmap tools read it without any account connection.

The event fields
FieldValue
eventml_experiment_assigned
experiment_idThe id of the test
version_keyThe version the shopper got
is_controltrue for the Original, false for the others

Build a GA4 audience

  1. In Google Tag Manager, add a data layer variable for each field, named after it: experiment_id, version_key and is_control.
  2. Add a trigger of type custom event with the event name ml_experiment_assigned.
  3. Add a GA4 event tag on that trigger. Send the event name ml_experiment_assigned and add experiment_id, version_key and is_control as event parameters.
  4. In GA4 Admin, open Custom definitions and create custom dimensions, event scope, for those three parameters. They can take a day to appear.
  5. In GA4 Admin, open Audiences and create an audience, or in Explore create a segment. Include users with the event ml_experiment_assigned where experiment_id matches your test and version_key matches the version.

Read it from your own code

The script also exposes a small read-only object on the page. MLTesting.assignments() returns the tests this shopper is in as an object of test id to version key. MLTesting.onAssign(callback) calls your function for every assignment, including ones made before you subscribed, and returns a function that unsubscribes.

Still stuck? We reply within one business day.