Rich-text Reply

New Optimizely Data Object - Finding Active Variation

cubelodyte 10-05-16

New Optimizely Data Object - Finding Active Variation

[ Edited ]

As we attempt to migrate to Optimizely X there are a number of issues we need to resolve. 

In today's post, I'll focus on the the new Optimizely object: optimizely2.

 

Currently, on every page load, we attempt to identify the running (active) experiment/variation combination by name in order to pass that information along to our data tagging system. 

 

To do this, we use the following code to pull this information from the 'optimizely' data object:

 

var oData = window.optimizely.data;  
if (oData.state.activeExperiments && oData.state.activeExperiments.length) {
    var activeExperiments = oData.state.activeExperiments;
    
    var mCnt = activeExperiments.length;
    var mTest = '';
    var mVars = '';
    var mExp = '';
    var tCount = 0;

    for (var i=0;i<(mCnt);i++) {
      mExp = activeExperiments[i];
      var curTest = oData.experiments[mExp].name;
      var curVar = oData.state.variationNamesMap[mExp];

	    if (tCount>0) {
	      mTest = mTest + " : ";
	      mVars = mVars + " : ";
	    }
	    mTest = mTest + curTest;
	    mVars = mVars + curVar;
	    tCount += 1;
    }
  }

The new Optimizely data object, 'optimizely2' is structured completely different.

 

Has anyone found a way to access the Experiment and Variation names of the Optimizely X variations that are running on a page?

Scott Ehly
Manager of Site Optimization
sehly@rentpath.com

'The single biggest problem with communication is the illusion that it has taken place.' - George Bernard Shaw
robertchan 10-11-16
 

Re: New Optimizely Data Object - Finding Active Variation

I don't have direct access to Optimizely X, but looked at the developer documentation for you. It seems like you can grab what you need by first retrieving the object:

 

// Retrieve the data object
var oData = window.optimizely.get('data');

Followed by calling what you want. As an example, to get a campaign name, your entire code might look like the following:

 

// Retrieve the data object
var oData = window.optimizely.get('data');

// Get campaign names
oData.campaigns.name

You can probably see how this could get integrated with your current code with a few changes. I found the information from here: https://developers.optimizely.com/x/solutions/javascript/reference/index.html

 

The section I went to was under Optimizely X > Web >  Reference > Reading Optimizely data and state > Data

Robert Chan

cubelodyte 10-11-16
 

Re: New Optimizely Data Object - Finding Active Variation

Robert (@robertchan),
Thanks for a push in the right direction.

The code you proposed will get me information about all running campaigns, but not so much about the one active for a current visit/visitor.

However, scrolling down a little further on the page

https://developers.optimizely.com/x/solutions/javascript/reference/index.html#state

revealed exactly what I need. Similar to the get.('data') is the .get('state') object.

From this, I can find out what I need about the current session.

Thanks again.
Scott Ehly
Manager of Site Optimization
sehly@rentpath.com

'The single biggest problem with communication is the illusion that it has taken place.' - George Bernard Shaw
robertchan 10-11-16
 

Re: New Optimizely Data Object - Finding Active Variation

Glad to be of help and that you found the state reference.
Robert Chan