Managing the allocation of a variation according to a connection ID

portrait de l'auteur Julie Trenque

Written by Julie Trenque

Updated on 22/09/2020

1 min

Advanced

Was this content useful?

By default, Kameleoon allocate randomly a variation to a visitor. 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 ? registeredVariationId : "none";
}

However, this function can be overwritten to change the way a variation is selected and displayed to a user. In the case of a website that needs a secure connection, it can be interesting to always display the same variation to a connected user, whatever website or device he is using:

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;
 }
 }
 chosenVariationId = chosenVariationId ? chosenVariationId : "none";
 return chosenVariationId;
}

It is necessary to place this code in you account advanced options.

To do so, log in to your Kameleoon App.

Then, use the left menu to go to “Administrate” > “Sites”.

Click on “Configuration” on the card of your website.

Click to display the “Experiment” section of the page.

You will find a “Variation selection script” field.

Add the script above in the field and validate.