Rich-text Reply

How to dynamically add visitor to audience and reload all experiments for that audience

kevzettler 12-14-15

How to dynamically add visitor to audience and reload all experiments for that audience

Using the JS api. I want to dynamically add a visotor/user to an audience and reload/refresh/retrigger all experiments that audience is assigned to. 

My use case is the user is anonymous. They take an action and 'logs in' We do not refresh the page on login which would cause the optimiezly snippet to reload. So I'd need a manual way to retrigger. Is this possible?

JDahlinANF 12-14-15
 

Re: How to dynamically add visitor to audience and reload all experiments for that audience

kevzettler 12-14-15
 

Re: How to dynamically add visitor to audience and reload all experiments for that audience

Then I have to switch my experiment to 'Manual Activation Mode' and instrument the activation for it everywhere. When I only need to reactivate it for this particular case.
JDahlinANF 12-15-15
 

Re: How to dynamically add visitor to audience and reload all experiments for that audience

[ Edited ]

@kevzettler- Perhaps I misunderstood your question.  Are you saying that you have:

1- several experiments that act only when a user is "logged in" (or not logged in)

2- your site uses an AJAX type approach to your login process (in at least one page affected by at least one of the experiments)

 

and that your goal here is to:

1- notice when a user has an "audience changing" behavior (logging in or out)

2- re-evaluate all experiments that use such audiences

3- apply all experiments that the user now qualifies for

4- undo all experiments that the user previously qualified for but is no longer a member of the audience?

 

On the surface, #4 is not feasible.  You cannot "undo" the changes already applied by an experiment unless you are able to write the code that puts everything back in place.  Make sense?  Like, if you change the color from black to green, your "remove" code would need to know to change it from green to black.  Where this gets complicated is if you removed any dynamic content from the page, you would need to be able to be able to put it back in place, which means you would probably need to store those elements somewhere so you can put them back.  Depending on your approach to removing those elements, you may break event bindings that are tied to those elements.

 

Items #1-3 can all be accomplished by switching each affected experiment to Manual Activation and writing the appropriate code that activates each of them.

 

An alternative, is that you do not use the logged in status as an audience criteria and instead use a global audience and place the same code you have in your audience criteria into your variation's code to determine whether or not the run the experiment on page load.  The changes can be wrapped inside a function which can then also be triggered by the AJAX style login event.

 

With this approach, both logged in and non-logged in users will be in the experiment, so you will need to be able to create a segment that identifies which users in the experiment were actually logged in or not.  You should be able to do this in your enterprise web analytics platform or you can add some code to "doStuff" (below) which places the user into a custom segment and then filter your test results for only this segment to see the behavior of users who were actually affected by the experiment.

 

For example:

/* Everything you want to do the page when the user is logged in, goes here */
function doStuff() {
	//this variation's code
}

/* This Indicates if a user is logged in or not - this is probably you currently have in your "Logged In Audience" criteria */
function isLoggedIn(){
	//returns true if currently logged in
}

/* Set up a listener for the event triggered by a user logging in */
$(window).on('userLoggedInTriggerFiredByTheAJAXEvent',function(){
    doStuff();
});

/* If the user is logged in when the page loads, "doStuff" */
if (isLoggedIn) {
	doStuff();
}