Rich-text Reply

Loading Optimizely JavaScript only when needed

Inez 05-28-14

Loading Optimizely JavaScript only when needed

Does anyone have experience with loading Optmizely JavaScript on demand? Are there any downsides to it?

 

In my case feature that I want to A/B test is activated on the page only 1% of the time. So I don't want to load Optmizely JS always (for performance reason) and load it only when that particular feature is activated - does it sound like a right approach given mentioned circumstances?

 

Thanks,

Inez

Level 1

Ricky 05-29-14
 

Re: Loading Optimizely JavaScript only when needed

Hey Inez,

 

In your particular case it would make sense to conditionally load the Optimizely JS file only when the content that you want to A/B test shows up on the page. 

 

The key will be to make sure that you always load the file when that content is on the page, and additionally that you have a way to know which visitors saw the experiment so that  you can load the Optimizely file on subsequent pages that you may want to use to track conversions. This should be relatively straightforward to do by just checking for the cookies that get set.

Optimizely
Inez 05-29-14
 

Re: Loading Optimizely JavaScript only when needed

Thanks Ricky, this is helpful! However I have another related question.

The product that I'm going to test is a rich text editor. I want to see if there is higher conversion rate (of achieving certain goal) for people which were displayed certain welcome message (dialog). I want to display dialog once, but track how it affect performance in the longer term. I was planning on implementing logic of displaying dialog only once by myself (using totally custom, optimizely not realted cookie), but from what you wrote it seems that there might be a way to use optmizely for it, is that correct? (If so, could you provide some specifics?)

 

Thanks,

Inez

Level 1
Ricky 05-29-14
 

Re: Loading Optimizely JavaScript only when needed

Hey Inez,

 

For loading the JavaScript file you could easily check that either the page has the elements that you want to modify or the user has an optimizelyBuckets cookie that contains the experiment id. However, this is not going to be a valid approach for showing the dialog only once, because when the variation code is executing the user will have already been bucketed into the experiment.

 

The way we normally work around this problem is by setting an additional cookie right in the variation code. For example if you write:

 

document.cookie='saw_dialog=true;expires[SOME_DATE];domain=[YOUR_DOMAIN];path=/'

 Then you can easily check for the presence of the saw_dialog cookie before showing it again.

 

In your case the code would look something like this:

 

1. Check for presence of saw_dialog cookie

2. If present do not show dialog, otherwise show dialog

3. If cookie is not already set, set the cookie

 

This way your code will only run once and set you up within Optimizely to make sure you don't run the visual changes multiple times for any user.

 

Let me know if that helps.

Optimizely