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-split

v0.1.2

Published

Express middleware for split and AB testing

Downloads

14

Readme

express-split

Node.js Express middleware for split and AB testing. Allows to run multiple experiments and track conversions to choose the winning variant.

Usage

Add express-split to your project:

npm install express-split

Import express-split:

const split = require('express-split');

Add your experiments:

app.use(split({
  experiments: {
   'button-text': {options: ['sign-up', 'start', 'early-access']}
}}));

Start an experiment for a user (a random option will be assigned):

app.get('/', (req, res) => {
  req.split.set_id(1234); // Sets an indentifier for this user - can be avoided if using cookies
  req.split.start('button-text', () => {
    res.send('Home page');
  });
});

Do different things for each variant:

app.get('/product', (req, res) => {
  req.split.get('button-text', (option) => {
    if (option === 'start') {
      res.send('Start now!');
    } else if (option === 'start') {
      res.send('Get early access');
    } else {
      res.send('Sign up');
    }
  });
});

Mark convertions:

app.post('/subscribe', (req, res) => {
  req.split.finish('button-text', () => {
    res.send('Thanks!');
  });
});

Get the results for each experiment:

app.get('/admin/experiments', (req, res) => {
  req.split.results((results) => {
    res.send(JSON.stringify(results, null, 4));
  });
});

Get the results in a web GUI:

app.get('/admin/experiments', (req, res) => {
  req.split.gui(req, res);
});

Cookies

Note: If you don't use cookies you have to manually specify an integer identification for the user.

Call a cookie middleware like cookie-parser before using express-split:

const cookieParser = require('cookie-parser');
const split        = require('express-split');

app.use(cookieParser());
app.use(split({use_cookies: true}));

Options

| Option | Description | Default | | ----------------------|----------------|------------------------------------------------------------------------------------| | experiments | The available experiments and their options (see format below) | {} | | storage | Where to store the experiments results (Options: in-memory, mysql) | 'in-memory' | | db_pool | Connection pool object. Required if chosen a database storage | false | | db_table_experiments | Where to store the experiments in the database | 'split_experiments' | | db_table_users | Where to store the users in the database | 'split_users' | | use_cookies | Whether or not to use cookies. If false, use req.split.set_id() to manually set an identifier for each user | false | | cookie_name | The cookie name to use | '_splituid' | | cookie_max_age | The max-age to set for the cookie | 15552000000 (180 days) |

experiments

The experiments object holds the experiment name in each key, with the value of a new object options that holds an array of the variants for this experiment. As a convention, the first option should be the default option (if anything fails, the first option will be returned).

{
  'button-text': {
    options: ['sign-up', 'start', 'early-access']
  },
  'price': {
    options: ['300', '200', '400']
  }
}

Storage

in-memory (default)

Note: Using this option, your data will be deleted once the node process is stopped.

Stores the experiments and users data in the node process memory. Useful for setting up, not recommended in production.

mysql

Persists the data to a MySQL database. Creates 2 tables: split_experiments and split_users (configurable). A required db_pool object must be provided.

const app   = express();
const split = require('express-split');
const mysql = require('mysql');
const pool  = mysql.createPool({
  connectionLimit : 30,
  host     : 'localhost',
  user     : 'db_username',
  password : 'secret_password',
  database : 'db_name'
});

app.use(split({
  storage: 'mysql',
  db_pool: pool,
  experiments: {
    'price': {
      options: ['300', '200', '400']
    }
  }
}));

API

Constructor

set_id(user_id)

start(experiment_id, [callback])

get(experiment_id, callback)

finish(experiment_id, [callback])

results(callback)