项目作者: ironSource

项目描述 :
An A/B testing engine
高级语言: JavaScript
项目地址: git://github.com/ironSource/pickpick.git
创建时间: 2018-07-14T02:20:33Z
项目社区:https://github.com/ironSource/pickpick

开源协议:MIT License

下载


ANNOUCEMENT: repository changing ownership

On the 16th of March 2024 this repository will change ownership and move to the personal account of one of it’s maintainers, Yaniv Kessler

Ownership change will also occur on the npm registry to Yaniv Kessler

pickpick

An A/B testing engine

CircleCI
Coverage Status
npm latest version

To use this engine, create one or more experiments and stick them in a container. Each experiment is composed of a bunch of variations which are randomly picked/served to you. Once you obtain a variation, use it to render some content or make some sort of a decision.

The container’s job is to select the correct experiments for each visitor based on each experiment’s targeting expression. Thusly, to get a variation one would call pick() twice, once on the container and once on the experiment.

Further information about the targeting experssion syntax can be found here: pickpick-targeting-compiler

Also, please take a look at our examples

example

npm i -S pickpick

Let’s say we have a website with two pages buy and index and we want to run 3 experiments:

  • on the buy page test color button
  • on the buy page test price
  • on the index page test text
  1. const { Experiment, ExperimentContainer } = require('pickpick')
  2. // first create the experiments:
  3. let e1 = Experiment.create({
  4. name: 'buy page button color experiment',
  5. id: '953d6fe0',
  6. variations: [
  7. { object: '#ff0000', weight: 4 },
  8. { object: '#ff0000', weight: 1 },
  9. { object: '#00ff00', weight: 1 }
  10. ],
  11. targeting: '_.path in ["buy", "index"]'
  12. })
  13. let e2 = Experiment.create({
  14. name: 'buy page price experiment',
  15. id: 'a40f09ac',
  16. variations: [
  17. { object: 25 },
  18. { object: 35 },
  19. { object: 45 }
  20. ],
  21. targeting: '_.path !== "home" && page !== "foo"'
  22. })
  23. let e3 = Experiment.create({
  24. name: 'index text experiment',
  25. id: 'ac49ef42',
  26. variations: [
  27. { object: 'hi' },
  28. { object: 'hello' },
  29. { object: 'welcome' }
  30. ],
  31. targeting: '_.path === "index"'
  32. })
  33. // now create a container:
  34. let experiments = [e1, e2, e3]
  35. let container = ExperimentContainer.create({ experiments })
  36. // simulate a visitor that needs a determination about which variation of which experiment he gets:
  37. let visitor = { page: 'index' }
  38. for (let i = 0; i < 10; i++) {
  39. let experiment = container.pick(visitor)
  40. if (!experiment) {
  41. // no experiment that targets this user
  42. // handle this with defaults
  43. console.log('default goes here')
  44. } else {
  45. console.log(`selected experiment '${experiment.name}' for '${JSON.stringify(visitor)}'`)
  46. let variation = experiment.pick()
  47. console.log(`selected variation is ${variation}`)
  48. }
  49. }

API

Table of Contents

Experiment

git@github.com/:ReasonSoftware/pickpick/blob/a6938cd996f3f68216a96b210bf576e6e3ff6ece/lib/Experiment.js#L34-L171" title="Source code on GitHub">lib/Experiment.js:34-171

An A/B test experiment contains one or more @Variation">variations and a definition of @Targeting">targeting.

Experiments are serializable and can be created using classes from this engine or object literals. For example:

  1. const { Experiment } = require('pickpick')
  2. const e1 = Experiment.create({
  3. name: 'my experiment',
  4. id: 'foo',
  5. variations: [
  6. { object: 1, weight: 1 },
  7. { object: 2, weight: 1 },
  8. { object: 3, weight: 1 }
  9. ],
  10. targeting: '_.geo === "US"'
  11. })

Parameters

  • $0 Object
    • $0.name
    • $0.id
    • $0.variations (optional, default [])
    • $0.targeting (optional, default Targeting.default())
    • $0.userData

pick

git@github.com/:ReasonSoftware/pickpick/blob/a6938cd996f3f68216a96b210bf576e6e3ff6ece/lib/Experiment.js#L66-L68" title="Source code on GitHub">lib/Experiment.js:66-68

randomly select one variation from the Variations set

Returns Variant the value contained within the selected variation

match

git@github.com/:ReasonSoftware/pickpick/blob/a6938cd996f3f68216a96b210bf576e6e3ff6ece/lib/Experiment.js#L76-L78" title="Source code on GitHub">lib/Experiment.js:76-78

check if this experiment matches the input targeting

Parameters

Returns Boolean

add

git@github.com/:ReasonSoftware/pickpick/blob/a6938cd996f3f68216a96b210bf576e6e3ff6ece/lib/Experiment.js#L84-L95" title="Source code on GitHub">lib/Experiment.js:84-95

add another variation to this experiment

Parameters

iterator

git@github.com/:ReasonSoftware/pickpick/blob/a6938cd996f3f68216a96b210bf576e6e3ff6ece/lib/Experiment.js#L116-L118" title="Source code on GitHub">lib/Experiment.js:116-118

iterate over the variations contained in this experiment

Targeting

git@github.com/:ReasonSoftware/pickpick/blob/a6938cd996f3f68216a96b210bf576e6e3ff6ece/lib/Targeting.js#L11-L80" title="Source code on GitHub">lib/Targeting.js:11-80

Targeting

Parameters

match

git@github.com/:ReasonSoftware/pickpick/blob/a6938cd996f3f68216a96b210bf576e6e3ff6ece/lib/Targeting.js#L31-L39" title="Source code on GitHub">lib/Targeting.js:31-39

check if the input data is matched by this targeting instance

Parameters
  • inputTargeting Object is normally a simple js object

Returns Boolean

expression

git@github.com/:ReasonSoftware/pickpick/blob/a6938cd996f3f68216a96b210bf576e6e3ff6ece/lib/Targeting.js#L45-L47" title="Source code on GitHub">lib/Targeting.js:45-47

access this Targeting’s expression

Returns String

iterator

git@github.com/:ReasonSoftware/pickpick/blob/a6938cd996f3f68216a96b210bf576e6e3ff6ece/lib/Targeting.js#L52-L54" title="Source code on GitHub">lib/Targeting.js:52-54

iterate over the features that participate in the targeting

has

git@github.com/:ReasonSoftware/pickpick/blob/a6938cd996f3f68216a96b210bf576e6e3ff6ece/lib/Targeting.js#L61-L63" title="Source code on GitHub">lib/Targeting.js:61-63

check if a feature is part of this targeting instance

Parameters
  • feature String a name of a feature, e.g geo

Returns Boolean

Variation

git@github.com/:ReasonSoftware/pickpick/blob/a6938cd996f3f68216a96b210bf576e6e3ff6ece/lib/Variation.js#L9-L99" title="Source code on GitHub">lib/Variation.js:9-99

A variation attaches weight to a piece of data. Variations are used in Experiments and ExperimentContainers

Parameters

  • $0 Object
    • $0.object
    • $0.weight (optional, default 1)

ExperimentContainer

git@github.com/:ReasonSoftware/pickpick/blob/a6938cd996f3f68216a96b210bf576e6e3ff6ece/lib/ExperimentContainer.js#L38-L239" title="Source code on GitHub">lib/ExperimentContainer.js:38-239

Contains one or more experiments and routes traffic evenly to each of them based on their
targeting. The following is an example of using a container to host several experiments, pick
on thats appropriate for a single visitor’s targeting and then access a variation from the
selected experiment:

  1. const { ExperimentContainer, Experiment } = require('pickpick')
  2. const experiments = [
  3. Experiment.create(...),
  4. Experiment.create(...),
  5. Experiment.create(...)
  6. ]
  7. const container = ExperimentContainer.create({ experiments })
  8. let experiment = container.pick({ geo: 'US', page: 'index.html '})
  9. if (experiment) {
  10. let variation = experiment.pick()
  11. // do something with the variation data
  12. } else {
  13. console.log('no experiments that match this targeting were found')
  14. }

Parameters

  • __seed number just for testing / predictable engine results

add

git@github.com/:ReasonSoftware/pickpick/blob/a6938cd996f3f68216a96b210bf576e6e3ff6ece/lib/ExperimentContainer.js#L77-L102" title="Source code on GitHub">lib/ExperimentContainer.js:77-102

Add an experiment to this container. Inside a container experiments must
have unique ids. This method can accept different kinds of experiment expressions:

  • an instance of Experiment:
  1. container.add(Experiment.create(...))
  • An instance of Variation where it’s object is an Experiment:
    1. ```js
    2. container.add(Variation.create(Experiment.create(...)))
    3. ````
  • An instance of Variation where it’s object is an Expriment defined as an object literal:
  1. container.add(Variation.create({... experiment data ...}))
  • A variation object literal wrapping an experiment object literal, this is useful in deserialization scenarios:
  1. container.add({ object: {... experiment data }, weight: 5 })
Parameters
  • experiments …any

pick

git@github.com/:ReasonSoftware/pickpick/blob/a6938cd996f3f68216a96b210bf576e6e3ff6ece/lib/ExperimentContainer.js#L131-L151" title="Source code on GitHub">lib/ExperimentContainer.js:131-151

The pick method accepts a targeting object and randomly selects an experiment
from a set of experiments that match the targeting specification.

By default, selection is random and even, however, bias can be applied by specifying a weight
when adding an experiment to the container (see ExperimentContainer.add())

Weights are considered at the moment of selection from the current set of
matching experiments, therefor, careful planning of targeting is required to achieve
accurate traffic distribution betwee experiments.

For example, consider two experiments, E1, that targets { geo: 'US', page: '*' } and E2 that targets
{ geo: 'US', page: 'index.html' }. If both had the weight 1, given the following stream
of visitors:

  1. { geo: 'US', page: 'sale.html' }
  2. { geo: 'US', page: 'index.html' }
  3. { geo: 'US', page: 'sale.html' }
  4. { geo: 'US', page: 'index.html' }

Then it is more likely that E1 will receive more traffic than E2 since E1 competes with E2 evenly on index.html
page but not on sale.html

Parameters

Returns Experiment an experiment that matches this targeting or null if none is found.

targetingFeatures

git@github.com/:ReasonSoftware/pickpick/blob/a6938cd996f3f68216a96b210bf576e6e3ff6ece/lib/ExperimentContainer.js#L159-L161" title="Source code on GitHub">lib/ExperimentContainer.js:159-161

An iterator over all the targeting features from all the experiments
added to this container

Returns Iterator

iterator

git@github.com/:ReasonSoftware/pickpick/blob/a6938cd996f3f68216a96b210bf576e6e3ff6ece/lib/ExperimentContainer.js#L175-L177" title="Source code on GitHub">lib/ExperimentContainer.js:175-177

iterate over all the experiments in this container:

  1. let container = ExperimentContainer.create(...)
  2. for (let experiment of container) {
  3. console.log(experiment.id)
  4. }

Returns ObjectIterator

has

git@github.com/:ReasonSoftware/pickpick/blob/a6938cd996f3f68216a96b210bf576e6e3ff6ece/lib/ExperimentContainer.js#L184-L188" title="Source code on GitHub">lib/ExperimentContainer.js:184-188

check if this container contains the specified experiment

Parameters
  • experiment Expriment

Returns Boolean

hasId

git@github.com/:ReasonSoftware/pickpick/blob/a6938cd996f3f68216a96b210bf576e6e3ff6ece/lib/ExperimentContainer.js#L195-L201" title="Source code on GitHub">lib/ExperimentContainer.js:195-201

check if this container contains an experiment using an id

Parameters

Returns Boolean

toJSON

git@github.com/:ReasonSoftware/pickpick/blob/a6938cd996f3f68216a96b210bf576e6e3ff6ece/lib/ExperimentContainer.js#L208-L215" title="Source code on GitHub">lib/ExperimentContainer.js:208-215

serialize this container with all it’s experiments

Returns Object