A/B testing React components and debug tools. Isomorphic with a simple, universal interface. Well documented and lightweight. Tested in popular browsers and Nod
A/B testing React components and debug tools. Isomorphic with a simple, universal interface. Well documented and lightweight. Tested in popular browsers and Nod
Wrap components in ](#variant-) and nest in [. A variant is chosen randomly and saved to local storage.
Report to your analytics provider using the emitter. Helpers are available for Mixpanel and Segment.com.
emitter.addPlayListener(function(experimentName, variantName){
mixpanel.track("Start Experiment", {name: experimentName, variant: variantName});
});Please ★ on GitHub!
Table of Contents
emitteremitter.emitWin(experimentName)emitter.addActiveVariantListener([experimentName, ] callback)emitter.addPlayListener([experimentName, ] callback)emitter.addWinListener([experimentName, ] callback)emitter.defineVariants(experimentName, variantNames [, variantWeights])emitter.setActiveVariant(experimentName, variantName)emitter.getActiveVariant(experimentName)emitter.getSortedVariants(experimentName)Subscription
experimentDebugger
mixpanelHelper
segmentHelper
react-ab-test is compatible with React 0.14.x and 0.15.x.
npm install react-ab-testTry it on JSFiddle
…Try it on JSFiddle
…Try it on JSFiddle
Use emitter.defineVariants() to optionally define the ratios by which variants are chosen.
var Experiment = require("react-ab-test/lib/Experiment");
var Variant = require("react-ab-test/lib/Variant");
var emitter = require("react-ab-test/lib/emitter");
// Define variants and weights in advance.
emitter.defineVariants("My Example", ["A", "B", "C"], [10, 40, 40]);
var App = React.createClass({
render: function(){
return
;
}
});
The debugger attaches a fixed-position panel to the bottom of the `` element that displays mounted experiments and enables the user to change active variants in real-time.
The debugger is wrapped in a conditional if(process.env.NODE_ENV === "production") {...} and will not display on production builds using envify.
Try it on JSFiddle
var Experiment = require("react-ab-test/lib/Experiment");
var Variant = require("react-ab-test/lib/Variant");
var experimentDebugger = require("react-ab-test/lib/debugger");
experimentDebugger.enable();
var App = React.createClass({
render: function(){
return
;
}
});
A ](#experiment-) with a `userIdentifier` property will choose a consistent [ suitable for server side rendering.
See ./examples/isomorphic for a working example.
The component in Component.jsx:
var React = require("react");
var Experiment = require("react-ab-test/lib/Experiment");
var Variant = require("react-ab-test/lib/Variant");
module.exports = React.createClass({
propTypes: {
userIdentifier: React.PropTypes.string.isRequired
},
render: function(){
return
;
}
});
We use a session ID for the userIdentifier property in this example, although a long-lived user ID would be preferable. See server.js:
…Remember to call abEmitter.rewind() to prevent memory leaks.
An EJS template in template.ejs:
Isomorphic Rendering Example
On the client in app.jsx:
var React = require('react');
var ReactDOM = require('react-dom');
var Component = require("../Component.jsx");
var container = document.getElementById("react-mount");
ReactDOM.render(, container);Code from ./src is written in JSX and transpiled into ./lib using Babel. If your project uses Babel you may want to include files from ./src directly.
Please let us know about alternate libraries not included here.
Please let us know about React A/B testing resources not included here.
Experiment container component. Children must be of type Variant.
name - Name of the experiment.string"My Example"userIdentifier - Distinct user identifier. When defined, this value is hashed to choose a variant if defaultVariantName or a stored value is not present. Useful for server side rendering.string"7cf61a4521f24507936a8977e1eee2d4"defaultVariantName - Name of the default variant. When defined, this value is used to choose a variant if a stored value is not present. This property may be useful for server side rendering but is otherwise not recommended.string"A"Variant container component.
name - Name of the variant.string"A"emitterEvent emitter responsible for coordinating and reporting usage. Extended from facebook/emitter.
emitter.emitWin(experimentName)Emit a win event.
experimentName - Name of an experiment.string"My Example"emitter.addActiveVariantListener([experimentName, ] callback)Listen for the active variant specified by an experiment.
SubscriptionexperimentName - Name of an experiment. If provided, the callback will only be called for the specified experiment.string"My Example"callback - Function to be called when a variant is chosen.functionexperimentName - Name of the experiment.stringvariantName - Name of the variant.stringemitter.addPlayListener([experimentName, ] callback)Listen for an experiment being displayed to the user. Trigged by the React componentWillMount lifecycle method.
SubscriptionexperimentName - Name of an experiment. If provided, the callback will only be called for the specified experiment.string"My Example"callback - Function to be called when an experiment is displayed to the user.functionexperimentName - Name of the experiment.stringvariantName - Name of the variant.stringemitter.addWinListener([experimentName, ] callback)Listen for a successful outcome from the experiment. Trigged by the emitter.emitWin(experimentName) method.
SubscriptionexperimentName - Name of an experiment. If provided, the callback will only be called for the specified experiment.string"My Example"callback - Function to be called when a win is emitted.functionexperimentName - Name of the experiment.stringNo open issues yet, or sync has not completed.