Git Product home page Git Product logo

react-abtest's Introduction

react-abtest

A simple React and React Native AB test component.

Install

yarn add react-abtest

or

npm install react-abtest

Usage

You can either automatically render a component based on the group placement or you can get the group the user is placed in and choose how to handle it yourself.

Render a component

ExperimentRandom

Randomly renders a variant.

import { ExperimentRandom } from 'react-abtest';

const A = <div>A variant</div>;
const B = <div>B variant</div>;
const C = <div>C variant</div>;

// Optional, but useful for logging test data.
const logger = (variant) => console.log(`User placed in group ${variant}.`);

const ExampleTest = () => {
  return (
    <ExperimentRandom
      variants={[A, B, C]}
      logger={logger}
    />
  );
}

export default ExampleTest;

ExperimentRandomWeighed

Randomly renders a variant based on weight.

import { ExperimentRandomWeighed } from 'react-abtest';

const A = <div>A variant</div>;
const B = <div>B variant</div>;
const C = <div>C variant</div>;

// Optional, but useful for logging test data.
const logger = (variant) => console.log(`User placed in group ${variant}.`);

const ExampleTest = () => {
  return (
    <ExperimentRandomWeighed
      weights={[0.1, 0.1, 0.8]}
      variants={[A, B, C]}
      logger={logger}
    />
  );
}

export default ExampleTest;

ExperimentUniqueId

Renders the same variant based on a unique identifier and experiment name.

import { ExperimentUniqueId } from 'react-abtest';

const A = <div>A variant</div>;
const B = <div>B variant</div>;
const C = <div>C variant</div>;

// Optional, but useful for logging test data.
const logger = (variant) => console.log(`User placed in group ${variant}.`);

const ExampleTest = ({ uid }) => {
  return (
    <ExperimentUniqueId
      experimentName={'sample-experiment'}
      uid={uid}
      variants={[A, B, C]}
      logger={logger}
    />
  );
}

export default ExampleTest;

ExperimentUniqueIdWeighed

Renders the same variant based on weight, a unique identifier* and experiment name.

  • Should be of some length, even though the library support one char id's. Short id's may result in uneven distribution.
import { ExperimentUniqueIdWeighed } from 'react-abtest';

const A = <div>A variant</div>;
const B = <div>B variant</div>;
const C = <div>C variant</div>;

// Optional, but useful for logging test data.
const logger = (variant) => console.log(`User placed in group ${variant}.`);

const ExampleTest = ({ uid }) => {
  return (
    <ExperimentUniqueIdWeighed
      experimentName={'sample-experiment'}
      uid={uid}
      weights={[0.1, 0.1, 0.8]}
      variants={[A, B, C]}
      logger={logger}
    />
  );
}

export default ExampleTest;

ExperimentValueGroup

When you already have assigned the users to a group (number), for example in a cookie.

import Cookie from 'cookies';
import { ExperimentValueGroup } from 'react-abtest';

const A = <div>A variant</div>;
const B = <div>B variant</div>;
const C = <div>C variant</div>;

const ExampleTest = () => {
  const userGroup = Cookies.get('abTestCookie');

  const variants = [
    {
      group: 1, // Single group
      component: A
    },
    {
      group: '2-50', // Range group
      component: B,
    },
    {
      group: '51-100',
      component: C
    }
  ];

  // Optional, but useful for logging test data.
  const logger = (variant) => console.log(`User placed in group ${variant}.`);

  // userGroup = 1, would render A
  // userGroup = 33 would render B
  // userGroup = 51 would render C
  return (
    <ExperimentValueGroup
      userGroup={userGroup}
      variants={variants}
      logger={logger}
    />
  );
}

export default ExampleTest;

Place user in a group

experimentRandomGroup

Randomly returns a group.

import { experimentRandomGroup } from 'react-abtest';
const exampleTest = () => {
  // Optional, but useful for logging test data.
  const logger = (group) => console.log(`User placed in group ${group}.`);
 
  const options = {
    groups: 5, // Number of groups to place users in
    logger,
  };

  return experimentRandomGroup(options);
}

export default exampleTest;

experimentRandomWeighedGroup

Randomly returns a group based on weight.

import { experimentRandomWeighedGroup } from 'react-abtest';

const exampleTest = () => {
  // Optional, but useful for logging test data.
  const logger = (group) => console.log(`User placed in group ${group}.`);

  const options = {
    weights: [0.2, 0.8],
    logger,
  };

  return experimentRandomWeighedGroup(options);
}

export default exampleTest;

experimentUniqueIdGroup

Returns the same group based on a unique identifier and experiment name.

import { experimentUniqueIdGroup } from 'react-abtest';

const exampleTest = ({ uid }) => {
  // Optional, but useful for logging test data.
  const logger = (group) => console.log(`User placed in group ${group}.`);

  const options = {
    experimentName: 'experimentName',
    uid,
    logger,
  };

  return experimentUniqueIdGroup(options);
}

export default exampleTest;

experimentUniqueIdWeighedGroup

Returns the same group number based on weight, a unique identifier* and experiment name.

  • Should be of some length, even though the library support one char id's. Short id's may result in uneven distribution.
import { experimentUniqueIdWeighedGroup } from 'react-abtest';
const exampleTest = ({ uid }) => {
  // Optional, but useful for logging test data.
  const logger = (group) => console.log(`User placed in group ${group}.`);

  const options = {
    experimentName: 'experimentName',
    uid,
    weights: [0.2, 0.8],
    logger,
  };

  return experimentUniqueIdWeighedGroup(options);

}

export default exampleTest;

react-abtest's People

Contributors

dependabot-preview[bot] avatar engvik avatar risto1913 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.