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

ab-react

v2.0.3

Published

A/B testing component for React

Downloads

267

Readme

ab-react

Jest coverage

If this component helped you or your team, please give it a GitHub 🌟!

Examples


Installation

yarn add ab-react

or

npm install -S ab-react

Usage

This is mainly for AB testing, it will work with multiple children but try to stick with two.

Normal use case

In the following example, three things can happen:

1 - The ABReact component will randomize one of the children and store the value on a cookie, after that the user will always see the same version.

2 - If a version is set on the cookie with the defined cookie name, that version will be rendered.

3 - If a version is set on the cookie with the defined cookie name but the child doesn't exist null will be returned.

No matter what scenario happens, the trackVersion event will be called each time the component is rendered.

import * as React from 'react';
import { useCallback } from 'react';
import ABReact from 'ab-react';

export default function MyComponent() {
  const trackVersion = useCallback((version: number) => {
    gtag('event', 'screen_view', {
      'screen_name': 'MyComponent',
      'version': version,
    });
  });

  return (
    <ABReact cookieName='cookie-name-to-store-version' event={trackVersion}>
      <div key="version-1">Version 1</div>
      <div key="version-2">Version 2</div>
    </ABReact>
  ) 
}

If you are already using react-cookies and CookiesProvider

If you already has a top-level CookiesProvider mounted you can set a property so the ABReact won't mount the CookiesProvider internally:

import * as React from 'react';
import { useCallback } from 'react';
import ABReact from 'ab-react';

export default function MyComponent() {
  return (
    <ABReact cookieName='cookie-name' mountCookieProvider={false}>
      <div key="version-1">Version 1</div>
      <div key="version-2">Version 2</div>
    </ABReact>
  ) 
}

Forcing a version

If you want to force a version to be rendered (to disable one or to maintain SSR consistency), you can set the property forceVersion:

import * as React from 'react';
import { useCallback } from 'react';
import ABReact from 'ab-react';

export default function MyComponent() {
  return (
    <ABReact cookieName='cookie-name' forceVersion={1}>
      <div key="version-1">Version 1</div>
      <div key="version-2">Version 2</div> {/* This version will always be rendered */}
    </ABReact>
  ) 
}

Examples

Vite.js

Using only CSR (Client Side Rendering)

import { useState, useCallback } from 'react';
import ABReact from 'ab-react';

export default function ABTesting() {
  const [selectedState, setSelectedState] = useState<string>('');

  const eventExample = useCallback((version: number) => {
    setSelectedState(version.toString());

    /**
     * Do any tracking here such as sending an event to Google Analytics
     */
  }, [setSelectedState]);

  return (
    <div>
      <h1>Normal scenario for a pure client side rendering (CSR)</h1>
      <ABReact cookieName='cookie-1' event={eventExample}>
        <div>1</div>
        <div>2</div>
      </ABReact>
      <p>Selected version for cookie-1 (returned by the callback): { selectedState }</p>
    </div>
  );
}

Next.js

Using SSR (Server Side Rendering), CSR (Client Side Rendering) and AppRouter

page.tsx:

/* file: page.tsx */

import { cookies } from 'next/headers';

import Components from './components';

export default function ABTestings() {
  const v1 = Number(cookies().get('cookie-1')?.value);
  const v2 = Number(cookies().get('cookie-2')?.value);

  console.log(cookies().get('cookie-1'));

  return (
    <div>
      <Components v1={Number.isNaN(v1) ? undefined : v1} v2={Number.isNaN(v2) ? undefined : v2} />
    </div>
  );
}

components.tsx:

/* file: components.tsx */
'use client';
import { useState, useCallback } from 'react';
import ABReact from 'ab-react';

export default function Components({ v1, v2 }: { v1?: number, v2?: number }) {
  const [selectedState, setSelectedState] = useState<string>('');

  const eventExample = useCallback((version: number) => {
    setSelectedState(version.toString());

    /**
     * Do any tracking here such as sending an event to Google Analytics
     */
  }, [setSelectedState]);

  return (
    <div>
      <h1>Normal scenario, after SSR forces version from v1</h1>
      <ABReact cookieName='cookie-1' forceVersion={v1}>
        <div>1</div>
        <div>2</div>
      </ABReact>

      <h1>Another scenario, after SSR forces version from v2 and runs an callback when the version is rendered</h1>
      <ABReact cookieName='cookie-2' event={eventExample} forceVersion={v2}>
        <div>1</div>
        <div>2</div>
      </ABReact>
      <p>Selected version for cookie-2 (returned by the callback): { selectedState }</p>

      <h1>Forced version scenario, no internal choosing. Version hard coded.</h1>
      <ABReact cookieName='cookie-3' forceVersion={1}>
        <div>1</div>
        <div>2 forced</div>
      </ABReact>
    </div>
  );
}