Rich-text Reply

Iteration of data objects through JS API

davidsen86 10-17-14

Iteration of data objects through JS API

Hi.

 

I'm new to optimizely, so this might be a rookie question.

 

When I'm looking into

$(window['optimizely'].data.visitor.audiences)

It returns this:

[Object

2119053717: true

 

Which I know means, that this visitor is in the audience with id: 2119053717

I know that this audience is called "business" because I looked in data.audiences. I would like to check if this visitor is part of the audience "business". - But since audiences isn't an array but an object, I can't iterate it. So I can't compare data.audiences with data.visitor.audiences.

Makes sense?

Re: Iteration of data objects through JS API

[ Edited ]

Hi @davidsen86,

 

You should be able to iterate through an object as an array as long as you provide the right key.  Here is a stackoverflow post that provides general guidance.

 

Also, here is some sample code another Optimizely client and their analytics agency are using to successfully parse through another aspect of our data object.  You may also use this as a guide for your own code.  I've bolded and highlighted the lines where the key comes into play.

 

function() {
  var returnVal = "";
  if (optimizely.activeExperiments.length > 0) {
    var experimentId;
    var experimentName;
    var variationName;
    var delimiter = "_";
    var namesCombo;
    for (var i = 0; i < optimizely.activeExperiments.length; i++) {
	  experimentId = window['optimizely'].data.state.activeExperiments[i];
	  experimentName = window['optimizely'].data.experiments[experimentId].name;
	  variationName = window['optimizely'].data.state.variationNamesMap[experimentId];
 	  namesCombo = "op:" + experimentName + delimiter + variationName;          
	  returnVal += (returnVal) ? "|" + namesCombo : namesCombo;
    }
  }
  return (returnVal);
}

 

Harrison Krat
Solutions Architect | Optimizely, Inc.
harrison@optimizely.com