Rich-text Reply

Custom Audiences - include/exclude other experiments & specific variations

JDahlinANF 12-02-14

Custom Audiences - include/exclude other experiments & specific variations

[ Edited ]

Sharing for broader usage - can be useful for setting audiences where you want to include/exclude users from other experiments or variations of other experiments.

 

 

function checkOptimizely(id,type) {
  //id is the experiment or variation id value (from the Dianostic report)
  //type is optional
  //to check if a user is on an experiment:
  //    checkOptmizely('203142438');
  //to check if a user is in a specific experiment's variation, pass "variant" for type
  //    checkOptmizely('203155272','variant');

  var OptSegment = (function(){
		var name = "optimizelyBuckets";
		var cookie = document.cookie.match(name+"=([^;]*)") || "";
		if (cookie != null) {
			var decodedCookie = decodeURIComponent(cookie[0]);
      if (type == 'variant') {
        var value = decodedCookie.match('"([^,]*)":"'+id+'"');
      } else {
        var value = decodedCookie.match('"'+id+'":"([^,]*)"');
      }
			return value;
		} else {
			return null;
		}
	})();
	var optlyVal = false;
	if (OptSegment != null) {
		optlyVal = true;
	}
	return optlyVal;
}

 

To include users who are in a specific experiment

(function(){
  return checkOptimizely('203142438');
})();

 To include users who are in a specific variation:

(function(){
  return checkOptimizely('203155272','variant');
})();

To exclude users who are in a specific experiment:

(function(){
  return !checkOptimizely('203142438')
})();

 To exclude users who are in a specific variation:

(function(){
  return !checkOptimizely('203155272','variant');
})();

 

 

 

 

greg 12-03-14
 

Re: Custom Audiences - include/exclude other experiments & specific variations

This is really neat, thanks for sharing! Just for a complete understanding, can you explain why the function calls at in parentheses?

Also, would all this JS go into the Custom Javascript field for an Audience, or elsewhere?
Level 2
JDahlinANF 12-03-14
 

Re: Custom Audiences - include/exclude other experiments & specific variations

All of this goes into the Custom JavaScript area in the audience.

 

Here is an example of a Custom Audience "Exclude ExperimentName's Group_B variation" (the group_b variation has id "1738680394"):

 

function checkOptimizely(id,type) {
  //id is the experiment or variation id value (from the Dianostic report)
  //type is optional
  //to check if a user is on an experiment:
  //    checkOptmizely('203142438');
  //to check if a user is in a specific experiment's variation, pass "variant" for type
  //    checkOptmizely('203155272','variant');

  var OptSegment = (function(){
		var name = "optimizelyBuckets";
		var cookie = document.cookie.match(name+"=([^;]*)") || "";
		if (cookie != null) {
			var decodedCookie = decodeURIComponent(cookie[0]);
      if (type == 'variant') {
        var value = decodedCookie.match('"([^,]*)":"'+id+'"');
      } else {
        var value = decodedCookie.match('"'+id+'":"([^,]*)"');
      }
			return value;
		} else {
			return null;
		}
	})();
	var optlyVal = false;
	if (OptSegment != null) {
		optlyVal = true;
	}
	return optlyVal;
}

(function(){
  return !checkOptimizely('1738680394','variant');
})();

 

I like to use anonymous function calls so I can manipulate the results of the function calls if I need to. For this example, it may not need to be wrapped, you could probably just call the function directly, like this:

!checkOptimizely('1738680394','variant');