Get visitor's Optimizely A/B test and variation

后端 未结 2 894
北海茫月
北海茫月 2021-02-04 08:02

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 Javascr

2条回答
  •  盖世英雄少女心
    2021-02-04 08:59

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

提交回复
热议问题