4.0.3 • Published 10 months ago
nuxt-ab-segment v4.0.3
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
orthis.$abtest('experiment-x', true, true, 1);
- Prevent automatic reporting of a/b test analytics. E.g.
this.$abtest('experiment-x', true, false);
- Avoid activating the a/b test anywhere. E.g.
this.$abtest('experiment-x', false);
- Disable all a/b tests by cookie (
abs_disabled=1
), which is useful for E2E tests in CI/CD pipelines
Dependencies
- nuxt-segment
- Or any other alternative to inject Segment Analytics
Setup
- Add
nuxt-ab-segment
dependency to your project:
npm install nuxt-ab-segment
- Add
nuxt-ab-segment
module and configuration tonuxt.config.js
:
export default {
// ...other config options
modules: ["nuxt-ab-segment"];
abSegment: {
event: "AB Test", // optional
experiments: '~/experiments.js', // optional
debug: process.env.NODE_ENV !== 'production', // optional
}
}
- 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],
},
];
- (Optional) TypeScript support. Add
nuxt-ab-segment
to thetypes
section oftsconfig.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.
debug
- Type:
Boolean
- Default:
- Development:
true
- Production:
false
- Development:
Enables or disables printing Segment reports to the console.
Usage
It can be used inside components like:
{
data: () => ({
payBtnLabel: null as string | null,
}),
mounted() {
// Scenario: Determine an experiment variant and display a label accordingly.
const expA = this.$abtest('experiment-a');
if (expA === 0) {
this.payBtnLabel = 'Place order';
} else {
this.payBtnLabel = 'Pay now!';
}
// Scenario: Force a specific variant programmatically.
const expB = this.$abtest('experiment-b', { forceVariant: 1 });
console.log('Variant for expB is always 1');
// Scenario: Assign a variant and report it for analytics.
const expC = this.$abtest('experiment-c', { reportVariant: true });
console.log(`Variant for expC is ${expC}, and it was reported.`);
// Scenario: Avoid activating an A/B test at a specific step.
// (No variant assignment or reporting is done.)
const expD = this.$abtest('experiment-d', { assignVariant: false });
console.log('Variant for expD is always 0');
// Scenario: Report analytics and append specific properties.
// (experiment, but additional data is sent.)
const expE = this.$abtest('experiment-e', { reportVariant: true, segment: { properties: { device: 'mobile' } } });
console.log(`Variant for expE is ${expE}, and will include the properties: { experiment: 'experiment-e', variant: ${expE}, device: 'mobile' }`);
}
}
An example of event properties reported to Segment:
{
experiment: 'experiment-x',
variant: 1
}
Credits
- Brandon Mills for
weightedRandom()
License
See the LICENSE file for license rights and limitations (MIT).
3.1.0
10 months ago
3.0.5
10 months ago
4.0.1
10 months ago
4.0.0
10 months ago
4.0.3
10 months ago
4.0.2
10 months ago
3.0.3
2 years ago
3.0.2
2 years ago
3.0.1
2 years ago
1.0.2
3 years ago
1.0.1
3 years ago
2.0.0
3 years ago
1.0.0
3 years ago
1.0.3-alpha
3 years ago
1.0.2-alpha
3 years ago
1.0.1-alpha
3 years ago
1.0.0-alpha
3 years ago