Skip to content

santosh1994/nextjs-ab-test-middleware

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

13 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

AB test Middleware

NPM Version

AB test Middleware for Nextjs

Installation

This is a Node.js module available through the npm registry. Installation is done using the npm install command:

$ npm install nextjs-ab-test-middleware

API

var activateAB = require("nextjs-ab-test-middleware");

activateAB(req, res, next, experiments)

Sample usage

var express = require("express");
const next = require("next");
var activateAB = require("nextjs-ab-test-middleware");

const experiment = [
  {
    name: "experiment-1",
    variants: [
      {
        id: "a",
        weight: 50,
      },
      {
        id: "b",
        weight: 50,
      },
    ],
    customFilter(req, res) {
      return req.originalUrl.indexOf("US") !== -1;
    },
  },
  {
    name: "experiment-2",
    variants(req, res) {
      if (req.originalUrl.indexOf("US") !== -1) {
        return [
          {
            id: "a",
            weight: 50,
          },
          {
            id: "b",
            weight: 50,
          },
        ];
      }
      return [
        {
          id: "a",
          weight: 100,
        },
        {
          id: "b",
          weight: 0,
        },
      ];
    },
  },
];
const app = next({ dev });

app
  .prepare()
  .then(() => {
    const server = express();

    server.use((req, res, next) => activateAB(req, res, next, experiment));

    server.use((req, res, next) => {
      next();
    });

    server.listen(3000, (err) => {
      if (err) throw err;
    });
  })
  .catch((ex) => {
    console.error(ex.stack); // eslint-disable-line no-console
    process.exit(1);
  });

License

MIT