Rich-text Reply

Callback for optimizely initialization?

tylerball 08-13-14

Callback for optimizely initialization?

Is there any way to reliably execute javascript after optimizely scripts have been loaded and the experiments have been populated in the "optimizely" global variable?

Shaunak 08-13-14
 

Re: Callback for optimizely initialization?

Hey Tylerball,

I am trying to understand your use case for the callback, can you elaborate about what you are trying to achieve. Optimizely does not have a callback function, but if I understand your use case better I might be able to come up with a solution.

Best,
Shaunak Mali
Solutions Architect | Optimizely



Optimizely
adzeds 08-14-14
 

Re: Callback for optimizely initialization?

Are you looking to fire something back to Optimizely or to your own service?
David Shaw
Level 11
tylerball 08-14-14
 

Re: Callback for optimizely initialization?

I'm looking to execute javascript in my application based on whether a variation is active, as a part of an a/b test. I can look in window.optimizely.activeExperiments but since that is populated asynchronously it's not availble on dom ready when my application code executes.

adzeds 08-14-14
 

Re: Callback for optimizely initialization?

Ok. Can you set your script to run on a delay? - In my head I am sure this is possible, I just can't seem to pull out of my mind the best way to implement this.
David Shaw
Level 11
tylerball 08-14-14
 

Re: Callback for optimizely initialization?

[ Edited ]

Delaying my code by an arbitrary amount isn't a good solution. It will decrease the performance of my application for all users and I have no way of knowing what the right amount of time would be.

The closest I've got so far is using jQuery's .on() to listen for an event on the <body> and trigger it inside of optimizely's variation code. e.g:

application code:

$('body').on('optimizely', function () { doSomething(); });



optimizely variation code:

$('body').trigger('optimizely');

Re: Callback for optimizely initialization?

Hey Tylerball,

The Optimziely Snippet will look at each line of your variation code every 50ms and determine if the element is in the DOM before performing the action. So it is already checking in a specified timeframe on when to execute the code and impact on performance is negligible. A full article explaining the snippet timing is located here: https://help.optimizely.com/hc/en-us/articles/200040335-How-Optimizely-Works-Snippet-order-of-operat...

You mention in a previous comment that activeExperiements is not available at DOM ready. Can you explain that a little more. The Javascript page variable is available to you as soon as the snippet runs and will not go away after DOM ready.

If you set a variable at the end of your variation code you could then check every 50ms if that variable is set. Once it is set you see that variable is set, you know that all variation code is done running. Do you see any issues with doing this?
Optimizely
tylerball 08-14-14
 

Re: Callback for optimizely initialization?

There's no data in window.optimizely until the optimizely javascript loads in from cdn.optimizely.com. All I need is some sort of way to know when that script has loaded so I can look in optimizely's data and do stuff with it.

Re: Callback for optimizely initialization?

Haveing a dedicated callback is a great idea and something we can have the engineering team look into. However for now you can use the following code snippet to accomplish the same thing.

 

window.myInterval = setInterval(function() {
  if (window.optimizely != 'undefined' && window.optimizely.activeExperiments.length > 0) {
    // Run custom code
    clearInterval(window.myInterval);
  }
}, 50);

 

 

Also, the higher you have the Optimizely snippet in your <head> tag the earlier the Optimizely snippet is avaialbe and any JavaScript you load in the page after the call of the Optimizely snippet will have access to the window.optimizely page variable.

 

Optimizely
tylerball 08-18-14
 

Re: Callback for optimizely initialization?

The API already uses .push() to execute things, so I could see doing something like:

 

window.optimizely.push(['callback', function () { someCode() }]);

 

EA_Rob 05-09-16
 

Re: Callback for optimizely initialization?

DId this issue ever gain any traction?

I also have some timing issues with figuring out when optimizely has loaded.

An event publish or callback method would be perfect.

 

Robert

Level 1