Managing the allocation of a variation according to specific criteria

By default, Kameleoon allocates randomly a variation to a visitor. Here is the function used.

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 in order to change how the variation is selected and displayed to a visitor. For instance, it can be interesting to choose a variation according to a JavaScript variable. Here is an example:

function(experiment)
{
    if(experiment.id == "ID TEST"){ //le code ci-dessous n'est exécuté que pour un test donné.
 
        if(typeof versionTest != "undefined") { //Test de la présence de la variable
            if(versionTest == 1)
                return 81103; //Identifiant de la version 1 à afficher
            else if(versionTest == 2)
                return 81104; //Identifiant de la version 2 à afficher
            else if(versionTest == 3)
                return 81105; //Identifiant de la version 3 à afficher
            else if(versionTest == 4)
                return 81106; //Identifiant de la version 4 à afficher
        }
     }
     else{ //Sinon le comportement par défaut est exécuté pour tous les autres tests en cours
          var deviationRandom = Math.random();
          var chosenVariationId;
          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