Skip to content
santosh edited this page Sep 3, 2022 · 1 revision

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

Sample app that will use custom token formats. This adds an ID to all requests and displays it using the :id token.

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

Clone this wiki locally