Skip to main content

Managing a variation's allocation according to a user ID

By default, Kameleoon randomly allocates a variation to a visitor. The assignment algorithm logic is explained in our statistical paper in the "Kameleoon's assignation algorithm" section. Here is the function used by Kameleoon:

function (experiment)
{
var registeredVariationId;
var deviationRandom = experiment.obtainVariationAssignmentRandomNumber();
var total = 0.0;
for (var i = 0, l = experiment.variations.length; i < l; ++i)
{
total += experiment.variations[i].deviation;
if (deviationRandom <= total)
{
registeredVariationId = experiment.variations[i].id;
break;
}
}
return registeredVariationId != null ? registeredVariationId : "none";
}

However, this function can be overwritten to change the way a variation is selected and displayed to a user. Websites that require a secure connection could benefit from showing the same variation to connected users across websites and devices:

function(experiment)
{
var memberId = window.member_id.toString(); //CODE to update with your variable that contains the unique identifier. It has to be available before calling the Kameleoon snippet
memberId = memberId + experiment.id.toString();
var hash = 0;
for (i = 0; i < memberId.length; ++i)
{
char = memberId.charCodeAt(i);
hash = ((hash << 5) - hash) + char;
hash = hash & hash;
}
hash = (Math.abs(hash) * 9301 + 49297) % 233280;
var deviationRandom = hash / 233280;
var total = 0.0;
for (var i = 0, l = experiment.variations.length; i < l; ++i)
{
total += experiment.variations[i].deviation;
if (deviationRandom <= total)
{
chosenVariationId = experiment.variations[i].id;
break;
}
}
return registeredVariationId != null ? registeredVariationId : "none";
}

It is necessary to place this code in you account advanced options. To do so:

  1. Log in to your Kameleoon account.
  2. Click Admin > Projects.
  3. Click Configuration on your website's card.

  1. Add the script above in the Variation selection script field.