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-chansey

v1.5.0

Published

A collection of utilities for ab testing in react front end applications

Downloads

10

Readme

Overview

react-chansey allows your application to conditionally render components based on the experience data provided by koa-chansey. It also takes care of firing the correct analytics events for your experiment so that we have consistent eventing regardless of which application is being tested.


Requirements

react-chansey depends on:

  • window.uSwitchEventTracking - this is used to push events onto the GTM data layer
  • window.nerdalytics - this is used to simplify the "Experiment Loaded" and "Experiment Viewed" events by using data-nerd-* attributes

react-chansey requires an experiments object as shown below to function. The suggested way to get access to this data is using koa-chansey.

{
  "experiment-home-movers-hub-homepage-banner-ltv28": {
    "cohort": 0,
    "value": "control"
  },
  "killswitch-mobile-app-broadband-cheapest-deals": {
    "cohort": 1,
    "value": true
  },
  "killswitch-mobile-app-fullstory-recording": {
    "cohort": 0,
    "value": false
  }
}

react-chansey/provider usage

Provides the experiences context to your react application.

Usage

react-chansey/provider provides <ExperienceProvider value={X} />, which makes the experiences data available to the rest of your app. In this example we are getting the experiences as a prop passed to our App component (for SSR) or from window.__experiences__ in the client.

// App.js

import React, { Component } from 'react'
import { ExperienceProvider } from 'react-chansey'

const App = ({ experiences, isServer }) => {
  const experienceContext = props.isServer
    ? experiences
    : window.__experiences__

  return (
    <ExperienceProvider value={experienceContext}>
      <RestOfYourApplication />
    </ExperienceProvider>
  )
}

react-chansey/experiment usage

Experiment exports multiple components to set up your conditional rendering.

Usage

react-chansey/experiment provides <Experiment id="example-name" />, which allows you to define children for A/B/n test splitting, In this example we are splitting a test with 2 variants and a control, with an optional fallback component if the experiment does not exist in the experiences data.

If you would like your Fallback and Control to be the same component (a common pattern), you can do this by adding a Fallback and omit the Control component. The fallback will be used when the user is in the control bucket and when the user is not applicable for the experiment / if the experiences data isn't populated due to technical difficulties.

import React, { Component } from 'react'
import { Experiment, Variant, Control, Fallback } from 'react-chansey'

const HomeComponent = ({ isServer }) => {
  return (
    <div>
      <h1>Welcome</h1>
      <Experiment id='experiment-home-page-design'>
        <Fallback>
          <p>
            This will be rendered if the experiment does not exist in launch
            darkly / the user is not applicable for the experiment. It can also
            be used to explicitly render content when custom content rendering
            logic is required (the experience entity selector in eevee is an
            example of this).
          </p>
        </Fallback>

        <Control>
          <p>This user is in control - Eventing has been fired!</p>
        </Control>

        <Variant value='variant-a'>
          <p>This user is in variant A! - Eventing has been fired! </p>
        </Variant>

        <Variant value='variant-b'>
          <p>This user is in variant B! - Eventing has been fired! </p>
        </Variant>
      </Experiment>
    </div>
  )
}

react-chansey/personalisation usage

react-chansey/personalisation provides <PersonalisationElement ... />, which allows you to create a personalised block with the data read from a rvu-personalisation cookie.

If any of the dependencies listed in dependencies are missing from the rvu-personalisation cookie, the PersonalisationElement will automaticaly render the fallback. This will also happen if the target browser doesn't support the Custom Elements v1 API.

PersonalisationElement props

  • dependencies: Array<string> required
  • fallbackElement: ReactNode required
  • personalisedElement: ReactNode required

PersonalisationData props

  • type: string required
import React from 'react'
import { PersonalisationData, PersonalisationElement } from 'react-chansey'

const HomeComponent = () => {
  return (
    <div>
      <h1>Welcome</h1>
      <PersonalisationElement
        dependencies={['firstName', 'lastName']}
        fallbackElement={<div>My name is Bond, James Bond</div>}
        personalisedElement={
          <div>
            My name is <PersonalisationData type='lastName' />,{' '}
            <PersonalisationData type='firstName' />{' '}
            <PersonalisationData type='lastName' />
          </div>
        }
      />
    </div>
  )
}

Examples

All examples use the code above. You can also try yourself with the demo app.

Result with cookie set to: 'rvu-personalisation={"firstName":"Testy","lastName":"McTestface","unusedDependency":"I wont affect the code"}'

Personalisation1

Result with cookie set to: 'rvu-personalisation={"firstName":"Testy"}' Or as: 'rvu-personalisation={"firstName":"Testy","lastName":""}'

Personalisation2