|
| 1 | +# `react-optimizely` [](https://www.npmjs.org/package/react-optimizely) [](https://travis-ci.org/onbjerg/react-optimizely) [](https://greenkeeper.io/) |
| 2 | + |
| 3 | +React helpers for A/B testing with [Optimizely](https://optimizely.com). |
| 4 | + |
| 5 | +**Quick Start Example** |
| 6 | + |
| 7 | +```json |
| 8 | +import React from 'react' |
| 9 | +import optimizely, { variate } from 'react-optimizely' |
| 10 | + |
| 11 | +class MyComponent extends React.Component { |
| 12 | + render () { |
| 13 | + return variate({ |
| 14 | + 'Variant A': () => <div>Variant A</div>, |
| 15 | + 'Variant B': () => <div>Variant B for {this.props.optimizely.experiment.name}</div>, |
| 16 | + default: () => <div>No variant</div> |
| 17 | + }, this.props.optimizely.variant) |
| 18 | + } |
| 19 | +} |
| 20 | + |
| 21 | +export default optimizely('Experiment A')(MyComponent) |
| 22 | +``` |
| 23 | + |
| 24 | +--- |
| 25 | + |
| 26 | +## Installation |
| 27 | + |
| 28 | +```sh |
| 29 | +npm install --save react-optimizely |
| 30 | +``` |
| 31 | + |
| 32 | +Or even better |
| 33 | + |
| 34 | +```sh |
| 35 | +yarn add react-optimizely |
| 36 | +``` |
| 37 | + |
| 38 | +## API |
| 39 | + |
| 40 | +#### optimizely |
| 41 | + |
| 42 | +Wraps a React component and injects props with information about an experiment and it's current variant. |
| 43 | + |
| 44 | +The props injected are ``experiment``, ``variant`` and ``isActivated``. |
| 45 | + |
| 46 | +If the experiment does not exist, then ``experiment`` and ``variant`` is ``null`` and ``isActivated`` is ``false``. Otherwise ``experiment`` is an object. |
| 47 | + |
| 48 | +If the experiment is not enabled or not activated, then ``variant`` is ``null`` and ``isActivated`` it false. |
| 49 | + |
| 50 | +**Parameters** |
| 51 | + |
| 52 | +- `experimentName` **[String](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String)** The name of the experiment, as it is in Optimizely. |
| 53 | + |
| 54 | +**Examples** |
| 55 | + |
| 56 | +```js |
| 57 | +import React from 'react' |
| 58 | +import optimizely from 'react-optimizely' |
| 59 | + |
| 60 | +class MyComponent extends React.Component { |
| 61 | + render () { |
| 62 | + const { |
| 63 | + experiment, |
| 64 | + variant, |
| 65 | + isActivated |
| 66 | + } = this.props.optimizely |
| 67 | + |
| 68 | + const result = [] |
| 69 | + if (experiment) { |
| 70 | + result.push(<div>Experiment: {experiment.name} ({isActivated ? 'Activated' : 'Not activated' })</div>) |
| 71 | + } |
| 72 | + if (variant) { |
| 73 | + result.push(<div>Variant: {variant.name}, {variant.code}</div>) |
| 74 | + } |
| 75 | + |
| 76 | + return result |
| 77 | + } |
| 78 | +} |
| 79 | + |
| 80 | +export default optimizely('Experiment A')(MyComponent) |
| 81 | +``` |
| 82 | + |
| 83 | +Returns a **[Function](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/function)** that takes a React component to wrap, that returns the wrapped component. |
| 84 | + |
| 85 | +#### variate |
| 86 | + |
| 87 | +Helper function to render different results based on the current variant. |
| 88 | + |
| 89 | +Takes an object whose keys are variant names and whose values are the result for the given variant. The value can be of any type. If the value is a function, then ``variate`` returns the result of the given function. |
| 90 | + |
| 91 | +The value for the special key ``default`` is returned if the current variant does not exist in the map, or if the variant was not specified (i.e. the experiment is not activated or not found). |
| 92 | + |
| 93 | +**Parameters** |
| 94 | + |
| 95 | +- `variantToResultMap` **[Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object)** An object whose keys are variant names and whose values are the result for the given variant. |
| 96 | +- `variant` **[Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object)** The current variant for the experiment |
| 97 | + |
| 98 | +**Examples** |
| 99 | + |
| 100 | +```js |
| 101 | +import React from 'react' |
| 102 | +import optimizely, { variate } from 'react-optimizely' |
| 103 | + |
| 104 | +class MyComponent extends React.Component { |
| 105 | + render () { |
| 106 | + return variate({ |
| 107 | + 'Variant A': () => <div>Variant A</div>, |
| 108 | + 'Variant B': () => <div>Variant B for {this.props.optimizely.experiment.name}</div>, |
| 109 | + default: () => <div>No variant</div> |
| 110 | + }, this.props.optimizely.variant) |
| 111 | + } |
| 112 | +} |
| 113 | + |
| 114 | +export default optimizely('Experiment A')(MyComponent) |
| 115 | +``` |
| 116 | + |
| 117 | +```js |
| 118 | +import React from 'react' |
| 119 | +import optimizely, { variate } from 'react-optimizely' |
| 120 | + |
| 121 | +class MyComponent extends React.Component { |
| 122 | + render () { |
| 123 | + return <div>{variate({ |
| 124 | + 'Variant A': 'Hello, world', |
| 125 | + 'Variant B': 'Hello, internet', |
| 126 | + default: 'Hello' |
| 127 | + }, this.props.optimizely.variant)}</div> |
| 128 | + } |
| 129 | +} |
| 130 | + |
| 131 | +export default optimizely('Experiment A')(MyComponent) |
| 132 | +``` |
| 133 | + |
| 134 | +#### activate |
| 135 | + |
| 136 | +Activates an experiment for the given visitor. |
| 137 | + |
| 138 | +Note that the experiment is **not** activated if: the experiment doesn't exist, the experiment is not enabled or if the experiment name is not unique. |
| 139 | + |
| 140 | +**Parameters** |
| 141 | + |
| 142 | +- `experimentName` **[String](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String)** The experiments name |
| 143 | + |
| 144 | +**Examples** |
| 145 | + |
| 146 | +```js |
| 147 | +import React from 'react' |
| 148 | +import { activate } from 'react-optimizely' |
| 149 | + |
| 150 | +class MyComponent extends React.Component { |
| 151 | + activateExperiment () { |
| 152 | + activate('Experiment B') |
| 153 | + } |
| 154 | + render () { |
| 155 | + return <button onClick={this.activateExperiment>Activate experiment</button> |
| 156 | + } |
| 157 | +} |
| 158 | + |
| 159 | +export default MyComponent |
| 160 | +``` |
| 161 | +
|
| 162 | +Returns true if the experiment was activated and false otherwise. |
| 163 | +
|
| 164 | +#### getVariant |
| 165 | +
|
| 166 | +Gets the current variant for an experiment. |
| 167 | +
|
| 168 | +Note that the function returns null if the experiment is not activated, enabled or if it does not exist. |
| 169 | +
|
| 170 | +**Parameters** |
| 171 | +
|
| 172 | +- `experimentName` **[String](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String)** The experiments name |
| 173 | +
|
| 174 | +**Examples** |
| 175 | +
|
| 176 | +```js |
| 177 | +import React from 'react' |
| 178 | +import { getVariant } from 'react-optimizely' |
| 179 | + |
| 180 | +class MyComponent extends React.Component { |
| 181 | + currentVariantNameForExperiment (experimentName) { |
| 182 | + let variant = getVariant(experimentName) |
| 183 | + if (!variant) return null |
| 184 | + return variant.name |
| 185 | + } |
| 186 | + render () { |
| 187 | + return [ |
| 188 | + <div>Activated variants</div>, |
| 189 | + <div>Experiment A: {this.currentVariantNameForExperiment('Experiment A')}</div>, |
| 190 | + <div>Experiment B: {this.currentVariantNameForExperiment('Experiment B')}</div>, |
| 191 | + <div>Experiment C: {this.currentVariantNameForExperiment('Experiment C')}</div> |
| 192 | + ] |
| 193 | + } |
| 194 | +} |
| 195 | + |
| 196 | +export default MyComponent |
| 197 | +``` |
| 198 | +
|
| 199 | +Returns the variant (as described on the Optimizely JS API reference) or null. |
| 200 | +
|
| 201 | +#### tag |
| 202 | +
|
| 203 | +Add custom tags to the current session. |
| 204 | +
|
| 205 | +**Parameters** |
| 206 | +
|
| 207 | +- `rawTags` **[Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String)**... A variadic number of objects whose keys are tag names and values are tag values |
| 208 | +
|
| 209 | +**Examples** |
| 210 | +
|
| 211 | +```js |
| 212 | +import React from 'react' |
| 213 | +import { tag } from 'react-optimizely' |
| 214 | + |
| 215 | +class MyComponent extends React.Component { |
| 216 | + onPurchase () { |
| 217 | + tag({ purchased: true, purchasedAt: Date.now }, { foo: 'bar' }) |
| 218 | + } |
| 219 | + render () { |
| 220 | + return <button onClick={this.onPurchase}>Purchase</button> |
| 221 | + } |
| 222 | +} |
| 223 | + |
| 224 | +export default MyComponent |
| 225 | +``` |
| 226 | +
|
| 227 | +Returns undefined. |
| 228 | +
|
| 229 | +#### track |
| 230 | +
|
| 231 | +Track an event. |
| 232 | +
|
| 233 | +**Parameters** |
| 234 | +
|
| 235 | +- `event` **[String](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String)** The event name |
| 236 | +- ``revenue`` **[Number](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number)** **Optional**: The amount of revenue generated from this event (in cents) |
| 237 | +
|
| 238 | +**Examples** |
| 239 | +
|
| 240 | +```js |
| 241 | +import React from 'react' |
| 242 | +import { track } from 'react-optimizely' |
| 243 | + |
| 244 | +class MyComponent extends React.Component { |
| 245 | + onPurchase () { |
| 246 | + track('purchase', 495) // $4.95 in revenue |
| 247 | + } |
| 248 | + render () { |
| 249 | + return <button onClick={this.onPurchase}>Purchase</button> |
| 250 | + } |
| 251 | +} |
| 252 | + |
| 253 | +export default MyComponent |
| 254 | +``` |
| 255 | +
|
| 256 | +Returns undefined. |
0 commit comments