Skip to content
This repository has been archived by the owner on May 20, 2024. It is now read-only.

isEligible should fire on route change #7

Closed
wants to merge 3 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 28 additions & 15 deletions lib/plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,23 @@ import experiments from '<%= options.experimentsDir %>'
const MAX_AGE = <%= options.maxAge %>

export default function (ctx, inject) {
ctx.app.router.beforeEach((to, from, next) => {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Won't this override any existing function on beforeEach? Just wondering.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

beforeEach is a register that adds callback function, so it won't override existing functions, i think.

https://github.com/vuejs/vue-router/blob/v3.0.2/src/index.js#L121-L123

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like you're right, cool. Didn't know that :)

trackExperiment(ctx);
next();
});

trackExperiment(ctx);

// Inject $exp
inject('exp', ctx.experiment)
}

function trackExperiment(ctx) {
// Assign experiment and variant to user
assignExperiment(ctx)

// Google optimize integration
googleOptimize(ctx)

// Inject $exp
inject('exp', ctx.experiment)
}

function assignExperiment(ctx) {
Expand All @@ -27,25 +36,29 @@ function assignExperiment(ctx) {
const [cookieExp, cookieVars] = cookie.split('.')
if (cookieExp.length) {
// Try to find experiment with that id
experimentIndex = experiments.findIndex(exp => exp.experimentID === cookie[0])
experimentIndex = experiments.findIndex(exp => exp.experimentID === cookieExp)

// Variant indexes
variantIndexes = cookieVars.split('-').map(v => parseInt(v))
}

// Choose one experiment
const experimentWeights = experiments.map(exp => exp.weight === undefined ? 1 : exp.weight)
let retries = experiments.length
while (experimentIndex === -1 && retries-- > 0) {
experimentIndex = weightedRandom(experimentWeights)
if (experimentIndex !== -1) {
experiment = experiments[experimentIndex]

// Check if current user is eligible for experiment
if (typeof experiment.isEligible === 'function') {
if (!experiment.isEligible(ctx)) {
// Try another one
experimentWeights[experimentIndex] = 0
experimentIndex = -1
} else {
const experimentWeights = experiments.map(exp => exp.weight === undefined ? 1 : exp.weight)
let retries = experiments.length
while (experimentIndex === -1 && retries-- > 0) {
experimentIndex = weightedRandom(experimentWeights)
experiment = experiments[experimentIndex]

// Check if current user is eligible for experiment
if (typeof experiment.isEligible === 'function') {
if (!experiment.isEligible(ctx)) {
// Try another one
experimentWeights[experimentIndex] = 0
experimentIndex = -1
}
}
}
}
Expand Down