npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

react-split-testing

v1.2.3

Published

Simple A/B testing component for React.

Downloads

1,358

Readme

react-split-testing

NPM Scrutinizer Code Quality Build Status GitHub Issues Gitter License

A/B testing, also called split testing, is an experiment where two (or more) variants of a webpage are shown to users at random, and is used as a means of determining which variant leads to a better performance. Once a variant wins, it is then shown to all users.

Wrap components in <Variant /> and nest in <Experiment />. A variant is chosen randomly and saved to local storage.

<Experiment name="My Example">
  <Variant name="A">
    <div>Version A</div>
  </Variant>
  <Variant name="B">
    <div>Version B</div>
  </Variant>
</Experiment>

Example (Demo)

Table Of Contents

Installation

npm

npm install react-split-testing

yarn

yarn add react-split-testing

Usage

import { Experiment, Variant } from 'react-split-testing'

class App extends Component {
  render() {
    return (
      <Experiment
        ref="experiment"
        name="My experiment"
        onChoice={(experimentName, variantName) => console.log(experimentName, variantName)}
      >
        <Variant name="A">
          <div>Section A</div>
        </Variant>
        <Variant name="B">
          <div>Section B</div>
        </Variant>
      </Experiment>
    )
  }
}

back to top


Server Rendering

A <Experiment /> with a userIdentifier property will choose a consistent <Variant /> suitable for server side rendering.

Example

import { Experiment, Variant } from 'react-split-testing'

class App extends Component {
  render() {
    return (
      <Experiment name="My experiment" userIdentifier={this.props.userIdentifier}>
        <Variant name="A">
          <div>Section A</div>
        </Variant>
        <Variant name="B">
          <div>Section B</div>
        </Variant>
      </Experiment>
    )
  }
}

back to top


API Reference

<Experiment />

Experiment container component. Children must be of type Variant.

  • Properties:

    • name - Name of the experiment.
      • Required
      • Type: string
      • Example: "My Example"
    • userIdentifier - Distinct user identifier. Useful for server side rendering.
      • Optional
      • Type: string
      • Example: "lMMaTqA8ODe7c36hhf2N"
    • variantName - Name of the variant. When defined, this value is used to choose a variant. This property may be useful for server side rendering.
      • Optional
      • Type: string
      • Example: "A"
    • onChoice - Called when a Variant has been chosen for your Experiment.
      • Optional
      • Type: function
      • Example: (experimentName, variantName) => { console.log(experimentName, variantName) }
    • onRawChoice - Same as onChoice, but the objects passed are React component instances.
      • Optional
      • Type: function
      • Example: (experiment, variant) => { console.log(experimentName.getName(), variant.props.name) }
  • Methods:

    • getName() - Returns the Experiment name.
    • getActiveVariant() - Returns the currently displayed Variant.
    • getActiveVariantName() - Returns the currently displayed Variant name.
    • getVariant(variantName) - Returns the instance of the specified Variant name.

back to top


<Variant />

Variant container component.

  • Properties:

    • name - Name of the variant.
      • Required
      • Type: string
      • Example: "A"
    • weight - The variants will be chosen according to the ratio of the numbers, for example variants A, B, C with weights 1, 2, 2 will be chosen 20%, 40%, and 40% of the time, respectively.
      • Optional
      • Type: number
      • Default: 1
      • Example: 2
  • Methods:

    • getName() - Returns the Variant name.
    • getWeight() - Returns the Variant weight.

back to top


License

MIT