Subscribing to Testa browser events

The Testa integration script fires two events as it runs an experiment. You can subscribe to them to send experiment data to your own tools, such as Google Tag Manager or Mixpanel.

Experimentation events

  • variation_assigned - fires just before the variation is applied, that is, before a redirect or any HTML change.
  • variation_applied - fires after the variation has been applied.

Both pass your listener the same data object:

js
data = {
  project_id: 123,             // your project ID
  experiment: 456,             // experiment ID
  variation: 1,                // variation ID (0 = control)
  uuid: "abc-123-def-456",     // the visitor's unique ID
  title: "Homepage Hero Test", // experiment name
  url: "https://example.com/", // current page URL
};

Subscribing

Add your listeners to window.Analytica.listeners before the Testa script loads. Each listener is a [eventName, handler] pair:

js
window.Analytica = window.Analytica || {};
window.Analytica.listeners = window.Analytica.listeners || [];

window.Analytica.listeners.push([
  "variation_applied",
  function (data) {
    console.log("Variation applied:", data);
  },
]);

Forwarding to your analytics

Google Tag Manager, by pushing to the dataLayer:

js
window.Analytica.listeners.push([
  "variation_applied",
  function (data) {
    window.dataLayer = window.dataLayer || [];
    window.dataLayer.push({
      event: "testa_variation_applied",
      experiment_id: data.experiment,
      experiment_name: data.title,
      variation_id: data.variation,
    });
  },
]);

Mixpanel, by tracking an event:

js
window.Analytica.listeners.push([
  "variation_applied",
  function (data) {
    mixpanel.track("Experiment Viewed", {
      experiment_id: data.experiment,
      experiment_name: data.title,
      variation_id: data.variation,
    });
  },
]);

Use variation_assigned when you need to record the assignment before a redirect sends the visitor to another page, since variation_applied may not fire before the browser navigates away.