• Stars
    star
    432
  • Rank 100,650 (Top 2 %)
  • Language
    JavaScript
  • Created over 10 years ago
  • Updated about 7 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Simple declarative and universal A/B testing component for React.

react-ab

npm version Build Status Coverage Status Dependency Status Size

Simple declarative and universal A/B testing component for React.

Demo

A/B Testing Demo

Install

npm install react-ab --save

or

bower install react-ab --save

Examples

Using Mixpanel.

var Experiment = require("react-ab").Experiment
  , Variant = require("react-ab").Variant;

var App = React.createClass({
  choice: function (experiment, variant, index) {
    mixpanel.register({
      "tagline": variant
    });
  }

  , click: function (e) {
    mixpanel.track("click");
  }

  , render: function () {
    return (
      <div>
        <Experiment onChoice={this.choice} name="tagline">
          <Variant name="normal">
            <h1> A A/B testing component for React </h1>
          </Variant>
          <Variant name="enterprise">
            <h1> A vertically integrated React component </h1>
          </Variant>
          <Variant name="lies">
            <h1> One weird React component that will increase your metrics by 100%! </h1>
          </Variant>
        </Experiment>
        <a onClick={this.click} href="//github.com/olahol/react-ab">React AB component</a>
      </div>
    );
  }
});

Using Google Universal Analytics. Requires a Custom Dimension.

var Experiment = require("react-ab").Experiment
  , Variant = require("react-ab").Variant;

var randomGoogle = function () {
  // base randomness off analytics.js client id.
  // https://developers.google.com/analytics/devguides/platform/user-id#clientid-userid
  var clientId = tracker.get("clientId");
  return (parseFloat(clientId, 10) % 100) / 100;
};

var App = React.createClass({
  choice: function (experiment, variant) {
    var dimension = 1; // Index of your custom dimension.
    ga("set", "dimension" + dimension, experiment + ": " + variant);
  }

  , click: function (e) {
    ga("send", "event", "click", "link");
  }

  , render: function () {
    return (
      <div>
        <Experiment onChoice={this.choice} random={randomGoogle} name="tagline">
          <Variant name="normal">
            <h1> A A/B testing component for React </h1>
          </Variant>
          <Variant name="enterprise">
            <h1> A vertically integrated React component </h1>
          </Variant>
          <Variant name="lies">
            <h1> One weird React component that will increase your metrics by 100%! </h1>
          </Variant>
        </Experiment>
        <a onClick={this.click} href="//github.com/olahol/react-ab">React AB component</a>
      </div>
    );
  }
});

Universality is achieved by setting get, set, clear. Here is an example server side with Express.js and using ES6:

import express from "express";
import cookieParser from "cookie-parser";

import React from "react/addons";
import { Experiment, Variant } from "react-ab";

var App = React.createClass({
  choice: function (experiment, variant, index) {
    console.log(experiment, variant, index);
  }

  , render: function () {
    return (
      <div>
        <Experiment {...this.props} onChoice={this.choice} name="tagline">
          <Variant name="normal">
            <h1> A A/B testing component for React </h1>
          </Variant>
          <Variant name="enterprise">
            <h1> A vertically integrated React component </h1>
          </Variant>
          <Variant name="lies">
            <h1> One weird React component that will increase your metrics by 100%! </h1>
          </Variant>
        </Experiment>
      </div>
    );
  }
});

var app = express();

app.use(cookieParser());

app.get("/", function (req, res) {
  res.send(React.renderToString(<App
    get={(x) => req.cookies[x]}
    set={(x, y) => res.cookie(x, y)}
    clear={res.clearCookie}
  />));
});

app.listen(3000);

API

Experiment

Props

name

Name of experiment, this prop is required. Should be something that describes the category being tested like color or title.

onChoice

Callback that fires when a variant is chosen. Gets arguments experiment name, variant name, variant index and was retrieved?. was retrieved? is true if the variant was retrieved using the get prop usually from a cookie.

random

Random function, should return a number in the range [0, 1). The default uses crypto.getRandomValues() when available and falls back on Math.random.

get

A function that takes an experiment and returns a variant. By default uses browser cookies.

set

A function that takes an experiment and variant and stores it. By default uses browser cookies.

clear

A function that clears/unsets an experiment. By default uses browser cookies.

Context

get, set, clear, random can also be set from context. If these props exists they overwrite context.

randomExperiment

random function taken from context.

getExperiment

get function taken from context.

setExperiment

set function taken from context.

clearExperiment

clear function taken from context.

Methods

getVariant()

Returns the name of the current variant.

chooseVariant()

Choose a new variant.

clearCookie()

Clear the experiment cookie.

Variant

Props

name

Name of variant, this props is required. Should be something descriptive of the attribute the variant represent like red or large.


MIT Licensed

More Repositories

1

melody

🎶 Minimalist websocket framework for Go
Go
3,685
star
2

react-tagsinput

Highly customizable React component for inputing tags.
JavaScript
1,345
star
3

reactpack

📦 build your react apps with one command and one `npm i`.
JavaScript
982
star
4

melody-jsnes

📺 Multiplayer NES through the magic of WebSockets and Go.
HTML
767
star
5

react-social

Simple React components for social buttons (Facebook, Twitter and Pinterest) and social counts.
HTML
177
star
6

go-imageupload

🔳 Gracefully handle image uploading and thumbnail creation.
Go
139
star
7

iso-3166-2.js

Lookup information about ISO-3166-2 subdivisions
JavaScript
108
star
8

express-chrome-logger

Debug your express app using the Chrome console.
JavaScript
83
star
9

pocketplace

🎆 Draw pixels on a canvas with friends.
Go
79
star
10

scrollparent.js

A function to get the scrolling parent of a html element.
HTML
58
star
11

node-csp

Communicating sequential processes for node.
JavaScript
55
star
12

iso-3166-2.json

JSON file of ISO 3166-2 subdivisions.
JavaScript
54
star
13

greasyphone

📱 🎮 Play NES using smartphones as joypads
JavaScript
45
star
14

viewdiff

📒 view your diffs in a separate window.
JavaScript
41
star
15

bf2c

🐛 Not the smallest compiler ever
JavaScript
20
star
16

socialcount.js

Tiny javascript library for getting social share counts. Pinterest, Facebook, Twitter and Google Plus supported.
JavaScript
17
star
17

cpp-csp

Minimalistic header-only library for channels and CSP (Communicating sequential process) in C++11.
C++
15
star
18

ethereum-web1-guestbook

📓 A Web 1.0 guestbook on Web 3.0
HTML
15
star
19

mainthread

Run functions in Go's main thread.
Go
14
star
20

eselement

DOM-like wrapper around the Javascript AST
JavaScript
6
star
21

tsreflect

Flexible reflection based TypeScript type generator for Go types that can be marshalled with `encoding/json`.
Go
6
star
22

capreq

Capture HTTP Requests using gopacket and libpcap
Go
6
star
23

hnplain

output Hacker News in plain text
Python
6
star
24

react-bus

Event emitter designed for communication between react components.
JavaScript
5
star
25

bf2asmjs

A simple brainfuck compiler targeting asm.js
Python
5
star
26

spawn.js

Tiny javascript library for spinning up one off web workers.
JavaScript
3
star
27

node-crcaptcha

Node package for using the Civil Rights Captcha.
JavaScript
2
star
28

tiny-xss-scanner

Lighweight xss scanner
Python
2
star
29

olapass

Stateless password manager.
JavaScript
1
star
30

request-animation-loop

As setInterval is to setTimeout, requestAnimationLoop is to requestAnimationFrame.
JavaScript
1
star
31

cpp-project-template

A small template for C++11 projects using tup and clang.
C++
1
star
32

generator-react-umd

A Yeoman generator that generates a minimalistic scaffold for a ES6 React component wrapped with the UMD.
JavaScript
1
star
33

gin-csrf

CSRF protection middleware for Go web framework Gin
Go
1
star
34

deepmodules

Analyze depth of Go packages
Go
1
star
35

node-mysql-slowlog

Application level slow log for mysql connections. Good for debugging slow queries when you haven't turned on or have access to the database slow log.
JavaScript
1
star