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.
Asked
Active
Viewed 3,766 times
13
-
1Have you checked the section of the API documentation that mentions referencing the data object? You can see the variations you are currently cookied to with optimizely.variationIdsMap and optimizely.variationNamesMap – jamis0n Sep 10 '13 at 00:21
2 Answers
20
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];

Kevin Borders
- 2,933
- 27
- 32
-
Is there any possible way to get the **actual** experiment ID? **Actual** meaning the experiment that contains the the code that is trying to access it. – Silver Ringvee Mar 11 '16 at 11:04
6
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;
}

Alex Johnson
- 1,504
- 13
- 20
-
Is there any possible way to get the **actual** experiment ID? **Actual** meaning the experiment that contains the the code that is trying to access it. – Silver Ringvee Mar 11 '16 at 11:04
-
@SilverRingvee Can you expand on your question? Perhaps provide an example. I'd like to help, but I'm not exactly sure what you're trying to get? – Alex Johnson Mar 11 '16 at 18:39
-
For example if I try to access experiment ID in experiment Javascript. So the same function would return different ID for each each experiment that contains that function. Same should work in Variation JS as well.. I hope I explained myself better now and you can help me. – Silver Ringvee Mar 13 '16 at 11:21
-
The optimizely object contains an "active experiments" key: ``optimizely.activeExperiments`` - this will return a list of all experiments currently running on the page for that visitor. Is this what you're looking for? Source: https://help.optimizely.com/hc/en-us/articles/205670207-The-console-data-object-and-Optimizely-log – Alex Johnson Mar 15 '16 at 17:05
-
No this is not what I am looking for. I need the exact (ONLY ONE) experiment ID. Something that I can write either in Experiment JS or Variation JS and would return the value for THAT experiment ID. – Silver Ringvee Mar 16 '16 at 09:34