![PyPI Now Supports iOS and Android Wheels for Mobile Python Development](https://cdn.sanity.io/images/cgdhsj6q/production/96416c872705517a6a65ad9646ce3e7caef623a0-1024x1024.webp?w=400&fit=max&auto=format)
Security News
PyPI Now Supports iOS and Android Wheels for Mobile Python Development
PyPI now supports iOS and Android wheels, making it easier for Python developers to distribute mobile packages.
express-ab
Advanced tools
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
).
$ npm install express-ab --save
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.
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.
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.
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);
});
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);
});
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 });
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.
This project was inspired by abn by NoumanSaleem. express-ab removes external dependencies and adds support for Google Experiments variables.
FAQs
Middleware for AB testing in Express
The npm package express-ab receives a total of 33 weekly downloads. As such, express-ab popularity was classified as not popular.
We found that express-ab demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
PyPI now supports iOS and Android wheels, making it easier for Python developers to distribute mobile packages.
Security News
Create React App is officially deprecated due to React 19 issues and lack of maintenance—developers should switch to Vite or other modern alternatives.
Security News
Oracle seeks to dismiss fraud claims in the JavaScript trademark dispute, delaying the case and avoiding questions about its right to the name.