Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get visitor's Optimizely A/B test and variation

When I run an experiment on my website, I want to be able to find out which test and variation the current visitor sees. I can't find how to do this from the Optimizely Javascript API.

like image 838
Pascal Klein Avatar asked Jun 26 '13 16:06

Pascal Klein


People also ask

What is variation in AB testing?

A variation is another version of your current version with changes that you want to test. You can test multiple variations against the control to see which one works best.

How do you do ab test in unbounce?

How to Start an A/B Test in Unbounce. Select a page you would like to test to reach the Page Overview screen. Scrolling down the Page Overview screen, you will see options to change the Page Traffic Mode. Standard Mode is used when you only have a single variant running - no kind of testing is currently underway.


2 Answers

To expand on Kevin Borders' answer. It's possible that you are running more than one experiment on a page. I've included a code snippet below, demonstrating how to return an array of active variation IDs:

// Return a list of active Optimizely variation IDs
function activeVariations(){

  // Multiple variations may currently be active
  var activeVariations = [];

  // Get state settings from optimizely object
  var state = window['optimizely'].data.state;

  // For each of the active experiments:
  for (var i = state.activeExperiments.length - 1; i >= 0; i--) {

    // Current experiment ID
    var experimentID = state.activeExperiments[i];

    // Current corresponding variation ID
    var variationID = state.variationIdsMap[experimentID];

    // If we have an active variation, add it to our array
    if (variationID) { activeVariations.push(variationID[0]); }
  }

  // List of active variations
  return activeVariations;
}
like image 155
Alex Johnson Avatar answered Oct 05 '22 10:10

Alex Johnson


You can get the ID of the first running experiment (assuming you have one), and then the variation index (e.g., 0, 1, 2), name, and ID:

var experimentID = window['optimizely'].data.state.activeExperiments[0];
var variationIndex = window['optimizely'].data.state.variationMap[experimentID];
var variationName = window['optimizely'].data.state.variationNamesMap[experimentID];
var variationID = window['optimizely'].data.state.variationIdsMap[experimentID];
like image 31
Kevin Borders Avatar answered Oct 05 '22 10:10

Kevin Borders