The Javascript API provides a function, KM.ab, to set up the majority of the A/B test. Specifically, it does three things:
Randomly assigns the current visitor to one of the variations passed in. (KM.ab returns the variation that was assigned, so you can save it as a JS variable.)
Ensures that subsequent calls to KM.ab returns the same variation for the visitor.
Sets a Kissmetrics property with the name of your experiment, and the value is the selected variant. In our example below, the A/B test will set the property “Signup Button Color” to either “red” or “green”. You will be able to segment any report using this property.
Full Example
Below shows an example of a complete A/B test, using KM.ab:
Explaining KM.ab
Let’s take a closer look at what KM.ab() does:
For each person seeing this A/B test, choose randomly between the available options “red” and “green”.
Sets a property “Signup Button Color” with the value “red” or “green”, depending on what was randomly chosen.
This is equivalent to:
KM.ab() returns which variation is picked, to reuse as a JavaScript variable (color). To remember which variation was used, KM sets a cookie. Please look at our page on Developing Locally if you are testing KM.ab() locally.
Notes
Wrap the call to KM.ab() in a function that is pushed to _kmq, or you may encounter JS errors if our library has not completely loaded before this executes.
Our JavaScript library depends on cookies to work properly. Browsers do not preseve cookies from page to page in localhost, so please refer to our guide on developing locally.
Weighted Variants
When you use KM.ab() to set up a test, we’ll split the variations evenly by default. However, you can give the function an extra argument to indicate the distribution of the test. These are all valid options: