Rich-text Reply

Global var window.optimizely with asynchronous loading

sebastienaxinte 03-03-17
Accepted Solution

Global var window.optimizely with asynchronous loading

Hi, looking at the async snippet here: (I can't use the synchronous snipper for performance reasons), it look like the variable:

window.optimizely = window.optimizely|| [];

 Is declared only after 1 sec to avoid the flashing issue when users has slow connections.


Why not having this variable declared before the snippet?

Because I need to triggers some events in my app and I don't want to wait for optimizely to be loaded to trigger those events. I want to use the "queue" logic as KissMetrics or Google Tag Manager have.


window.optimizely.push(["trackEvent", "eventName"]);



I've seen that declaring the variable 

window.optimizely = window.optimizely|| [];


before the snipper works properly:

1) The tracking events are pushed into the queue

2) Optimizely loads

3) Optimizely send the queue events.


Please let me know if there's a real reason why the variable isn't declared outside the IIFFE, thanks!



JasonDahlin 03-03-17

Re: Global var window.optimizely with asynchronous loading

You can declare the variable if you want.  As you've seen, when the snippet loads, it does not overwrite any exitsing data you have pushed into the array and sends the array's contents as soon as it is able to do so (similar to how GA's gaq array behaves)

--Jason Dahlin
Analytics and Testing Guru Smiley Happy

Experimentation Hero