Rich-text Reply

Pause an Experiment via JS API?

parththakkar 02-01-16
Accepted Solution

Pause an Experiment via JS API?

[ Edited ]

Hello!

So I have a bit of code that conditionally activates an experiment based on an input, but I also want to be able to pause an experiment based on an input as well.

I can't find any API calls to pause an experiment, just to disable optimizely all together, any advice? - Thanks!


Here's ma code:

document.getElementById("btn1").addEventListener(
    "click",
    function(){
console.log("optimizely helper : btn1");
//        window.optimizely.push(["disable", Experiment_ID_2]);
        window.optimizely.push(["activate", Experiment_ID_1]);
    }
);
document.getElementById("btn2").addEventListener(
    "click",
    function(){
console.log("optimizely helper : btn2");
//        window.optimizely.push(["disable", Experiment_ID_1]);
        window.optimizely.push(["activate", Experiment_ID_2]);
    }
);

 

Thanks!!

Analyst
Joel_Balmer 02-01-16
 

Re: Pause an Experiment via JS API?

Hi @parththakkar,

 

So the concept of activating an experiment, disabling an experiment, and pausing / running an experiment are distinct and separate from each other:

 

  • Conditional activation - the snippet code has already fully run, and is just waiting for a condition. Once met, it checks targeting conditions and if met applies the changes and bucketing accordingly.
  • Disabling Optimizely - this needs to be called before the Optimizely snippet, and will force the snippet code not to run.
  • Pausing / Running - this has the same effect as going to your account dashboard and starting or pausing the experiment, therefore should not be considered based on the user level input (you would be starting and stopping the experiment with each visitor). But if this is something you want to look into, this can be done using our REST API, here.

 

So, you can only disable the Optimizely snippet as a whole (not per experiment), and this needs to be performed before the snippet - Optimizely doesn't have a function to undo the code already applied.

 

One possible solution to what you describe might be setting a cookie upon your 'disable' action, and then refreshing the page. Assuming your targeting conditions were setup to deny users with this cookie from entering the experiment, this would essentially act as disabling the experiment for this user (similarly, you may wish to check for, and remove, this cookie before calling activate), and any actions the user took when they were in the experiment would be shown in the results.

 

I hope that gives you enough information to implement what you need!

 

Let us know if your situation is more complicated or nuanced than this, and hopefully one of us on the community will be able to offer their thoughts Smiley Happy

Re: Pause an Experiment via JS API?

I wanted to follow up with this, INSTEAD of trying to disable and try to implement user-level control of activation and deactivation (which is wrong), I went with the cookie approach.

I used JS to listen for a button click and assigned a cookie that would be used for audience targeting in the next step (As suggested by @Joel_Balmer (TY) ). 

Here's the 'helper code' that I used for reference to anyone who might need this going forward:
Note: I set my cookie age to 2 hours here and I created a function that fires and clears the cookie, and reloads the page if a user comes back and changes their first selection (after having seen later parts of the funnel)

//Declare Counter Var
var firstTime = 0;

//Add the event listener to our product buttons
document.getElementById("refi-button").addEventListener("click", disFunc);
document.getElementById("mortgage-button").addEventListener("click", disFunc);

document.getElementById("continue_button").addEventListener(
    "click",
    function(){



        if (firstTime == 0) {

            var productInputvalue = document.getElementById("PRODUCT").getAttribute("value");

            if (productInputvalue == "PP_NEWHOME") {

                console.log("Cookie : Newhome");

                document.cookie = "product = NEWHOME; max-age =" + 60*60*2;


            }

            else if (productInputvalue == "PP_REFI") {

                console.log("Cookie : Refi");

                document.cookie = "product = REFI; max-age =" + 60*60*2;


            }

            firstTime++;



        }


    }
);

function disFunc() {

    if (firstTime > 0) {
        document.cookie = "product=; max-age = 0";
        console.log("Edge Case Reached - Cookie Reset");
        location.reload();


    }
}




Analyst