Skip to content

๐Ÿ“Š NuxtJS module for A/B testing with Segment Analytics

License

Notifications You must be signed in to change notification settings

rockandre/nuxt-ab-segment

ย 
ย 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

10 Commits
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

nuxt-ab-segment

NuxtJS module for A/B testing with Segment Analytics


Table of contents

Main features

  • Run multiple experiments simultaneously
  • TypeScript support
  • Cookies to persist variants across users
  • Force a specific variant via url or param. E.g. url?abs_experiment-x=1 or this.$abtest('experiment-x', 1);
  • Disable all a/b tests by cookie (abs_disabled=1), which is useful for E2E tests in CI/CD pipelines

Dependencies

Setup

  1. Add nuxt-ab-segment dependency to your project:
npm install nuxt-ab-segment
  1. Add nuxt-ab-segment module and configuration to nuxt.config.js:
export default {
  // ...other config options
  modules: ["nuxt-ab-segment"];
  abSegment: {
    event: "AB Test", // optional
    experiments: '~/experiments.js', // optional
  }
}
  1. Create the experiments.js in project's root with an array of your experiments. An example:
/**
 * {
 *  name: string; A name to identify the experiment on this.$abtest('NAME_HERE')
 *  maxAgeDays: number; Number of days to persist the cookie of user's active variant
 *  variants: number[]; An array of variants weights
 * }
 */
module.exports = [
  {
    name: "experiment-x",
    maxAgeDays: 15,
    variants: [50, 50],
  },
];
  1. (Optional) TypeScript support. Add nuxt-ab-segment to the types section of tsconfig.json:
{
  "compilerOptions": {
    "types": ["nuxt-ab-segment"]
  }
}

Options

event

  • Type: String
  • Default: AB Test

Event name reported to Segment.

experiments

  • Type: String
  • Default: ~/experiments.js

File path for your experiments definition.

Usage

It can be used inside components like:

{
  data: () => ({
    payBtnLabel: null as string | null,
    isScenarioA: true,
  }),
  mounted() {
    // Example 1: normal usage
    const activeVariant = this.$abtest('experiment-x');
    if (activeVariant === 0) {
      this.payBtnLabel = 'Place order';
    } else {
      this.payBtnLabel = 'Pay now!';
    }

    // Example 2: force variant 1
    if (this.isScenarioA) {
      this.$abtest('experiment-y', 1)
      // do something else..
    }
  }
}

An example of event properties reported to Segment:

{
  experiment: 'experiment-x',
  variant: 1
}

Credits

License

See the LICENSE file for license rights and limitations (MIT).

About

๐Ÿ“Š NuxtJS module for A/B testing with Segment Analytics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Languages

  • TypeScript 100.0%