Add JavaScript based on test variation

AB Split test fires an event after we’ve done our thing, so that you can do whatever you like.

The event

ab-test-setup-complete

Examples

Add a script depending on the current test variation.

<script>
    jQuery(document).ready(function(){
        jQuery('body').bind('ab-test-setup-complete', function() { 
           //do what you want!
           
            // get experiment information for current user
            var testId = 6833; // your test  ID you want to 
            var testDetails = JSON.parse(getCookie('btab_' + testId));     // the data for the current user
            // testDetails = {eid: '68833', variation: 'easy', conversion: 1}

            // add script to page if variation is 'easy'
            if(testDetails .variation == 'easy') 
            {
               var my_awesome_script = document.createElement('script');
               my_awesome_script.setAttribute('src','http://example.com/site.js');
               document.head.appendChild(my_awesome_script);
            }                
        });
    });
</script>

 

Leave a Comment