Sign Up for Free

RunKit +

Try any Node.js package right in your browser

This is a playground to test code. It runs a full Node.js environment and already has all of npm’s 400,000 packages pre-installed, including @appnest/ab-test with all npm packages installed. Try it out:

var abTest = require("@appnest/ab-test")

This service is provided by RunKit and is not affiliated with npm, Inc or the package authors.

@appnest/ab-test v0.0.7

A/B testing made incredible simple


A/B testing made incredible simple
Never be happy with your conversion rate. Just remember that every page can be better. This library makes A/B testing incredible simple and works very well with web components. Go here to see a demo

Downloads per month NPM Version Dependencies Contributors


➤ Table of Contents


➤ Installation

npm i @appnest/ab-test


➤ Usage

Use the experiment() function to add an experiment where you test one of multiple variations. The function takes an id of the experiment and an array of potential variations. The function optionally takes an instance of a Test class where the selected values will be added. If none is specified the function will use a global instance.

import { experiment } from "@appnest/ab-test";

const headline = experiment("header.title.text", ["A/B testing", "A/B testing made simple", "Everyone should A/B test"]);

We want the headline to be the same next time the user reloads the page. To load the selected variations from local storage we use the load() function on the test object.

import { test } from "@appnest/ab-test";


To save the values to local storage we use the save() function.

import { test } from "@appnest/ab-test";;

Ultimately we want to save the selected variations each time new experiments are added to the test. To do that we listen for the update event on the test object. It's also here we want to send information about the selected values to our analytics tools.

import { Experiments, test, ExperimentEvent } from "@appnest/ab-test";

test.addEventListener(ExperimentEvent.UPDATE, (e: CustomEvent<Experiments>) => {;
  console.log("TODO: Update analytics tools", e.detail);



➤ A/B test web components using lit-html

If you want to test different web components using lit-html you can use the experimentElement() function. This function is a lit directive that takes an id of the test and a map, mapping each tag name to its corresponding import function. Optionally it takes a map of properties that should be set on the element. If the element doesn't need an import simple pass the value null.

import { customElement, html, LitElement } from "lit-element";
import { experiment, experimentElement } from "@appnest/ab-test";

export default class HomeElement extends LitElement {
  render () {
    return html`
      ${experimentElement("element", {
        "element-one": () => import("../elements/element-one"),
        "element-two": () => import("../elements/element-two")
      }, {
        headline: experiment("element.headline", ["A headline", "Another headline"])


➤ How to measure the data using Google Analytics?

You can use many tools to measure the AB test. Google Analytics is a nice free choice. Start by going to and create an account. Then add the tracking code to your index.html file.

<script async src=""></script>
  window.dataLayer = window.dataLayer || [];
  function gtag(){dataLayer.push(arguments);}
  gtag("js", new Date());
  gtag("config", "UA-XXXXXXXX-X");

Now, let's say you want to AB test a title for the header. It can either be "Buy today" or "Get instant access". First you should define the title using the experiment() function, giving it an ID and an array with the two variations.

import { experiment } from "@appnest/ab-test";

const title = experiment("header.title", ["Buy today", "Get instant access"]);

Add the title to the correct place in the header. Next you want send the data to Google Analytics. You can do that by using the global gtag() function and setting a dimension. Read more about the approach here. We are going to create the dimension in Google Analytics in next step, but lets assume for now its dimension1. Since dimensions only take primitive values we stringify the object specifying the selected variations.

import { Experiments, test, ExperimentEvent } from "@appnest/ab-test";

test.addEventListener(ExperimentEvent.UPDATE, (e: CustomEvent<Experiments>) => {;
  gtag("config", "UA-XXXXXXXX-X", {
    "dimension1": JSON.stringify(e.detail)


We now go to Google Analytics → Admin → Property → Custom Definitions → Custom Dimensions and create a custom dimension called AB Test. Set the scope to session. Remember that you cannot delete custom dimensions after they have been created and that you can have a maximum of 20 in each view. Therefore you can consider creating a new view for this purpose.


Next we go to Google Analytics → Admin → View → Segments and create two segments. We call the first segment for AB Header Title 1 and the other segment for AB Header Title 2. In the first segment we go to the tab Conditions and add a filter that includes the AB Test dimension we created before if the dimension contains "header.title.text":"Buy today". We do the same for the other segment but only includes the dimension if it contains "header.title.text":"Get instant access".


Then we go to Google Analytics → Admin → View → Goals and create some goals that can capture conversions. That could for example be sign ups to the newsletter. Make sure to track this from your app since we need the data to figure out which variation is doing best.


Now we need to set up a view so we can figure out what variations are doing the best. Go to Google Analytics → Customization → Custom Reports and create a new report. Select the conversion rate of the goals you are measuring as your Metric Groups. The Dimension Drilldowns should be the AB Test dimension you created.


Go to the report you just created and select the two segments AB Header Title 1 and AB Header Title 2. Click save and you are done! You will now be able to track which headline is doing best. Wait a couple of weeks and select the one that are doing best.


You should always try to only have one AB test going on at a given time.


➤ Contributors

Andreas MehlsenYou?
Andreas MehlsenYou?


➤ License

Licensed under MIT.

RunKit is a free, in-browser JavaScript dev environment for prototyping Node.js code, with every npm package installed. Sign up to share your code.
Sign Up for Free