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.
| Field | Value |
|---|---|
| event | ml_experiment_assigned |
| experiment_id | The id of the test |
| version_key | The version the shopper got |
| is_control | true for the Original, false for the others |
Build a GA4 audience
- In Google Tag Manager, add a data layer variable for each field, named after it: experiment_id, version_key and is_control.
- Add a trigger of type custom event with the event name ml_experiment_assigned.
- 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.
- In GA4 Admin, open Custom definitions and create custom dimensions, event scope, for those three parameters. They can take a day to appear.
- 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.