Get visitor's Optimizely A/B test and variation

家住魔仙堡 提交于 2019-12-20 10:34:29

问题


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.


回答1:


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];



回答2:


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;
}


来源:https://stackoverflow.com/questions/17326157/get-visitors-optimizely-a-b-test-and-variation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!