# labyrinth-guards

> [![Coverage Status](https://coveralls.io/repos/github/morten-olsen/abtesting/badge.svg?branch=test)](https://coveralls.io/github/morten-olsen/abtesting?branch=test) [![Build Status](https://travis-ci.org/morten-olsen/abtesting.svg?branch=master)](https://

Latest version **1.0.0-beta.1** (published 2017-06-17) · MIT license · 0 weekly downloads

## Install

```sh
npm install labyrinth-guards
pnpm add labyrinth-guards
yarn add labyrinth-guards
bun add labyrinth-guards
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.0-beta.1 |
| Published | 2017-06-17 |
| First published | 2017-06-17 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Maintainers | mortenolsen |

## Links

- npm: https://www.npmjs.com/package/labyrinth-guards
- npm.io page: https://npm.io/package/labyrinth-guards

## Recent versions

- 1.0.0-beta.1 (latest) — 2017-06-17

## README

# A/B Testing

[![Coverage Status](https://coveralls.io/repos/github/morten-olsen/abtesting/badge.svg?branch=test)](https://coveralls.io/github/morten-olsen/abtesting?branch=test) [![Build Status](https://travis-ci.org/morten-olsen/abtesting.svg?branch=master)](https://travis-ci.org/morten-olsen/abtesting)

## TO-DO before releasable

* Implement automatic impression tracking
* Finalize tooling
* **Find a better name**

## Getting started

For a minimal setup six elements are needed: `ABRoot`, `withABTest` and `VariantA`/`VariantB` along with an `ABManger` with an `ABTestSuite` and an `ABUser`

**A.jsx**
```javascript
import React, { Component } from 'react';
import { withABTest } from from 'abstesting';

class A extends Component {
  handleClick() {
    this.props.trackConversion();
  }

  render() {
    return (
      <div onClick={this.handleClick.bind(this)}>Variant A</div>
    );
  }
}

export default withABTest(A);
```

**B.jsx**
```javascript
import React, { Component } from 'react';
import { withABTest } from from 'abstesting';

class B extends Component {
  handleClick() {
    this.props.trackConversion();
  }

  render() {
    return (
      <div onClick={this.handleClick.bind(this)}>Variant B</div>
    );
  }
}

export default withABTest(B);
```

**App.jsx**
```javascript
import React from 'react';
import { VariantA, VariantB } from 'abtesting';
import A from './A';
import B from './B';

export default () => (
  <div>
    <VariantA name="test1" autoTrackImpression>
      <A />
    </VariantA>
    <VariantB name="test2" autoTrackImpression>
      <B />
    </VariantB>
  </div>
);
```

**test1.js**
```javascript
import { FunctionTest } from 'abtesting';

export default new FunctionTest(
  (userInfo) => userInfo.age > 15,
  (userInfo) => userInfo.id % 2,
);
```

**index.jsx**
```javascript
import React from 'react';
import ReactDOM from 'react-dom';
import { ABRoot, ABUser, ABTestSuite, ABManager } from 'abtesting';
import test1 from './tests';
import App from './App.js';

const manager = new ABManager({
  user: new ABUser(),
  testSuite = new TestSuite(),
});
manager.setUserInfo({
  id: 1234,
  age: 23,
});
manager.setTests({
  test1,
});

ReactDOM.render(
  <ABRoot manager={manager}>
    <App />
  </ABRoot>,
  document.body,
)
```
## Using different components

Another usefull feature is to be able to serve different components depending on which test bucket the user is in. This can be done using the `createABTest` higher order function

**A.jsx**
```javascript
export default ({value}) => <div>VariantA {value}</div>
```

**B.jsx**
```javascript
export default ({value}) => <div>VariantB {value}</div>
```

**Component.jsx**
```javascript
import { createABSplit } from 'abtesting';
import A from './A';
import B from './B';

export default createABSplit({
  name: 'test1',
  VariantA: A,
  VariantB: B,
})
```

Now when rendering `<Component value="hello" />`, the user which are presented the original version will see "VariantA hello" and user presented with the alternative version will see "VariantB hello".

## User information

In order to figure out if users is in a given group, a set of user trades need to be provided. The test split is stateless, so therefore it can not store any information about previous a/b buckets a user has fallen into, and therefor this needs to be something which can be figured out based on values about the current user.

These are set by calling `setUserInfo` on the manager object, which was passed to `ABRoot`. When these values change, all A/B splits, currently presented to the user will be re-evaluated, and if a user changes bucket, these changes are reflected.

```javascript
import { ABManager, ABUser, ABRoot } from 'abtesting';

const manager = new ABManager({
  ...
  user: new ABUser();
});
manager.setInfo({
  age: 23
});

const root = (
  <ABRoot manager={manager}>
  </ABRoot>
)

```

## Tests

### Test Suites

### Creating a custom test
```javascript
import { ABTest } from 'abstesting';
class CustomTest extends ABTest {
  isIncludedInTest(userInfo) {
    return userInfo.isLoggedIn;
  }

  isInTestGroup(userInfo) {
    return userInfo.id % 2;
  }
}

manager.setTest({
  someTest: new CustomTest(),
});
```

## Services

The component comes with a few different service types preloaded and provides the tools for creating custom service

### Creating a service

```javascript
import { ABService, ABManager } from 'abstesting';

class CustomService extends ABService {
  constructor(endpoint) {
    super();
    this.endpoint = endpoint;
  }

  async updateTests() {
    const response = await fetch(endpoint).then(res => res.json());
    return response;
  }

  async sendData(data) {
    const data = new FormData();
    data.append( "json", JSON.stringify(data) );
    return fetch(endpoint, {
      method: "POST",
      body: data
    });
  }

  async trackImpression(name, type) {
    await this.sendData({
      type: 'impression',
      name,
      type,
    });
  }

  async trackConversion(name, type, params) {
    await this.sendData({
      type: 'impression',
      name,
      type,
    });
  }
}

const manager = new ABManager {
  ...
  service: new CustomService(),
}

const root = (
  <ABRoot manager={manager}>
  </ABRoot>
)
```

---
_Source: https://npm.io/package/labyrinth-guards · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
