npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

express-ab

v1.0.1

Published

Middleware for AB testing in Express

Downloads

704

Readme

express-ab

Build Status Test Coverage

Middleware for AB/split/multi-variant testing in Express. Allows you to specify multiple variants of an endpoint as a part of an experiment. Remembers which variant the user was assigned using a cookie.

Supports outputting Google Experiments variables (experimentId and experimentVariant).

Install

$ npm install express-ab --save

Usage

Notice that express-ab requires the cookie-parser middleware to remember which variant the user was served.

var express = require('express');
var cookieParser = require('cookie-parser');
var ab = require('express-ab');

var app = express();
app.use(cookieParser());

var myPageTest = ab.test('my-fancy-test');

app.get('/', myPageTest(), function (req, res) {
    res.send('variant A');
});

app.get('/', myPageTest(), function (req, res) {
    res.send('variant B');
});

app.listen(8080);

In example above users will be presented with either 'variant A' or 'variant B'. Distribution will be 50/50 in a round-robin fashion.

You can add as many alternative endpoints to your test as you like, e.g. also 'variant C' etc.

Weighted distribution

The function returned by ab.test() (assigned to myPageTest), has the following arguments: myPageTest(variantId[, weight])

If you prefer to have a custom distribution, you can specify a weight percentage for each variant. This should be in decimal notation, and the sum should be 1.

app.get('/', myPageTest(null, 0.2), function (req, res) {
    res.send('variant A');
});

app.get('/', myPageTest(null, 0.8), function (req, res) {
    res.send('variant B');
});

In this example variant A will be selected 20% of the time, and variant B 80% of the time.

Google Experiments

If you are using Google Experiments you can add the expriment ID when running the test, and it will be available in the locals collection like this:

var myPageTest = ab.test('my-fancy-test', { id: 'YByMKfprRCStcMvK8zh1yw' });

app.get('/', myPageTest(), function (req, res) {
    // res.locals.ab.name === 'my-fancy-test'
    // res.locals.ab.id === 'YByMKfprRCStcMvK8zh1yw'
    // res.locals.ab.variantId === 0
    res.send('variant X');
});

To use it in your front end, you can expose the ab object e.g. on window. Then you have to notify Google Analytics that you are running an experiment by setting the following vars:

ga('set', 'expId', window.ab.id);
ga('set', 'expVar', window.ab.variantId);

More about setting the experiment variant in Google Analytics is explained here.

Get variant in other routes

If you need the selected variant in other routes not specifically part of the AB test, you can use the middleware function getVariant() on the returned test function (assigned to myPageTest).

app.get('/somepage', myPageTest.getVariant, function (req, res) {
    res.send('variant ' + res.locals.ab.variantId);
});

Usage as passive middleware

If you need the variant information in many routes in your application, and need cookies to be assigned for any/all of them, you can create your variants as general purpose middleware instead of attaching it to specific routes.

var variants = ['A', 'B', 'C'];
for (var i = 0; i < variants.length; i++) {
    app.use(myPageTest(variants[i]));
}

app.get('/somepage', myPageTest.getVariant, function(req, res) {
    res.send('variant ' + res.locals.ab.variantId);
});

Disable cookie

If you do not want the user to be sent to the same variant in the test on every return, you can disable cookies like this:

var myPageTest = ab.test('my-fancy-test', { cookie: false });

Or you can do it for all tests in the constructor:

var ab = require('express-ab')({ cookie: false });

Skip route if part of another test

If you are running multiple tests, you can skip routes using ab.filter(test). To create a new test only for users not in the previous test, the code could look something like this:

var abTest1 = ab.test('filter-test-1');
var abTest2 = ab.test('filter-test-2');

app.get('/', ab.filter(abTest1), abTest2(), helpers.send('2A'));
app.get('/', ab.filter(abTest1), abTest2(), helpers.send('2B'));
app.get('/', helpers.send('fallthrough2'));

In this case, if a user is already in abTest1, he will not be able to be in abTest2 as well. Just remember to include a fall through route.

Credits

This project was inspired by abn by NoumanSaleem. express-ab removes external dependencies and adds support for Google Experiments variables.