Managing the allocation of a variation according to a connection ID

By default, Kameleoon allocate randomly a variation to a visitor. Here is the function used by Kameleoon:

function (experiment)
{
  var variationId;
  var deviationRandom = Math.random();
  var total = 0.0;

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

However, this function can be overloaded 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 à personnaliser selon votre cas d'usage. L'idéal etant de stocker dans une variable JavaScript "member_id" l'identifiant de connexion du visiteur. ATTENTION, cette variable doit se trouver dans la page avant le script Kameleoon.

  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 personal space.

Then, use the left menu to go to the “Configured sites” page, with all the websites you have set up with your account.

Click on the “Advanced configuration” button for the website of your choice.

You will arrive on the Advanced configuration options page, as you can see on the tabs on the left.

Now, you can add the script above in the “Variation selection script" field.

Have more questions? Submit a request
Powered by Zendesk