Rich-text Reply

Load different (not additional) javascript file in a test version (addthis)

km 04-19-16

Load different (not additional) javascript file in a test version (addthis)

Hi there-

 

I'm attempting to A/B test different treatment of social sharing widgets. The test requires me to load a different .js file for each variant, ex:

Original:

<script type="text/javascript" src="//s7.addthis.com/js/300/addthis_widget.js#pubid=ra-1234"></script>

 

vs. Test Version:

 

<script type="text/javascript" src="//s7.addthis.com/js/300/addthis_widget.js#pubid=ra-5678></script>

 

The script tag for the 'original' variant is already in the HTML of our pages. I can't figure out how to get Optimizely to load a different version INSTEAD of the original on the test version. I've read (and tried) the documentation on loading external js at http://developers.optimizely.com/javascript/code-samples/index.html#helpers-external-js) but that only loads an additional .js file, it doesn't alter the fact that the browser is still going to load the original addthis tag that's in our HTML already.


Any way to get this to work?

 

km
Level 1

robertchan 04-20-16
 

Re: Load different (not additional) javascript file in a test version (addthis)

Hi km,

 

Your best bet is going to be to remove the current (original) script, and build a script for each variant like so (just copy and paste the code into each variant):

 

//VARIANT 1
    var fileRef = document.createElement('script');
    fileRef.setAttribute('type','text/javascript');
    fileRef.setAttribute('src', 'http://s7.addthis.com/js/300/addthis_widget.js#pubid=ra-1234');
    document.body.appendChild(fileRef);
//END OF CODE FOR VARIANT 1

//VARIANT 2
    var fileRef = document.createElement('script');
    fileRef.setAttribute('type','text/javascript');
    fileRef.setAttribute('src', 'http://s7.addthis.com/js/300/addthis_widget.js#pubid=ra-5678');
    document.body.appendChild(fileRef);
//END OF CODE FOR VARIANT 2
Robert Chan

Experimentation Hero

Re: Load different (not additional) javascript file in a test version (addthis)

[ Edited ]

Hi there,

Thanks for your question km! It's great to see that you've already found the external-js loading code snippet.

Unfortunately it's not possible to have Optimizely stop a script or anything from loading. Optimizely works client-side and can only add to whatever is going on on a webpage. It can't break into the original page or a script on your page to prevent that script from loading another script.

Does it work for you to load the other script and replace the original widget with the second one? Otherwise, Robert has a good answer for you as well (thanks Robert!).

Best wishes,
Nils