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

vue-a2b

v0.2.2

Published

Split testing for Vue.js

Downloads

5,957

Readme

vue-a2b

split testing for Vuejs

Usage

Add the package to your project:

yarn add vue-a2b
# or
npm install vue-a2b

…and register it to Vue:

import VueAB from 'vue-a2b'
Vue.use(VueAB)

vue-a2b uses named slots for defining test variations. Any amount of variations is supported (A/B/n). The the variation identifier should be used as slot name and can be any valid string. Selection chances are given as ratio. In the first example, test A has twice the chance to be selected over test B:

Note: Selection chances are parsed as whole numbers, so instead of 2.5 and 1 use 5 and 2.

minimal example

<template>
  <split-test>
    <component slot="A" chance="2" />
    <component slot="B" chance="1" />
  </split-test>
</template>

more examples

Chances are optional. If left out, every test gets the same chance to be picked. The selection can also be forced with the always parameter, which is useful for testing:

<template>
  <split-test always="B"> <!-- will always choose B, no matter the chances -->
    <component slot="A" chance="2" />
    <component slot="B" chance="1" />
  </split-test>
</template>

If more than one element is part of the test, use template tags:

<template>
  <split-test>
    <template slot="A" chance="2">
      <button>hello</button>
      <button>World</button>
    </template>
    <template slot="B" chance="1">
      <button>hey ho</button>
      <button>lets go</button>
    </template>
  </split-test>
</template>

functional usage

Since version 0.2 functional usage is supported. The component scope now has the $abtest method. It can be used to initialize an A/B test without the <split-test> component or to get the test value:

To initialize a new test, created() is a good spot:

export default {
  created () {
    // creates a test 'fancy-bubbles' with 75% chance for even more bubbles
    this.$abtest('fancy-bubbles', { bubbles: 25, lotsOfBubbles: 75 })
  }
}

To get a value, for example in data, just call $abtest again:

export default {
  data () {
    return {
      more_bubbles: this.$abtest('fancy-bubbles') === 'lotsOfBubbles'
    }
  }
}

The function is reachable for the template as well:

<template>
  <div :class='$abtest("fancy-bubbles")'>bubbles!</div>
</template>

<style>
.lotsOfBubbles {
  font-size: var(--extra-big);
}
</style>

outside of components

vue-a2b exports the abtest method among others, so it is possible to access it via:

import { abtest } from 'vue-a2b'

Additionally randomCandidate is exported, which allows to get a randomly picked sample out of a list of VNodes or an object:

import { randomCandidate } from 'vue-a2b'

// pics a random candidate foo, bar, baz with 75%, 20%, 5% chance respectively
const candidate = randomCandidate({
  foo: 75,
  bar: 20,
  baz: 5
})

## Settings

### Test name

The test name needs to be set with the name attribute. If no name is given,
it might be deferred from the parent components name attribute.

``` html
<template>
  <split-test name="the-one-test">
    <component slot="A" chance="2" />
    <component slot="B" chance="1" />
  </split-test>
</template>
Attention: Test name is mandatory!

Storage options

You can set storage method, name and expiry on initialization. Supported methods are cookie (the default) and localStorage.

Vue.use(VueAB, {
  storage: {
    method: 'localStorage',
    name: 'project42'
  }
})

The stored value expires after 30 days by default. This can be changed:

Vue.use(VueAB, {
  storage: {
    method: 'cookie',
    expiry: 7 // one week until the cookie expires
  }
})
Note: LocalStorage doesn't support expiration by default but the entries get
a timestamp and old entries will be ignored to make expiration possible.

Note: The expiry date is refreshed with every page visit. The entry only
expires, if the user doesn't come back in the specified time.

Component Name

By default <split-test> is the component that wraps a new test. This name can be overwritten on initialization:

Vue.use(VueAB, {component: 'a-b'})
<template>
  <a-b>
    <!-- variants -->
  </a-b>
</template>

Build setup

We recommend to use yarn but npm works too:

# Install dependencies
yarn install
# or
npm install

# Build for production with minification
yarn build
# or
npm run build

License

MIT © fromAtoB GmbH [email protected]