Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add support for MAB split overrides #29

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from
Draft
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
23 changes: 23 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ export const DEFAULT_OPTIONS = {
experimentsConfigFile: 'manifest.json',
experimentsMetaTag: 'experiment',
experimentsQueryParameter: 'experiment',

mabConfig: '/experiments.splits.json',
};

/**
Expand Down Expand Up @@ -236,6 +238,7 @@ function getConfigForInstantExperiment(
status: context.getMetadata(`${pluginOptions.experimentsMetaTag}-status`) || 'Active',
startDate: context.getMetadata(`${pluginOptions.experimentsMetaTag}-start-date`),
endDate: context.getMetadata(`${pluginOptions.experimentsMetaTag}-end-date`),
selfLearning: context.getMetadata(`${pluginOptions.experimentsMetaTag}-auto-allocate`) || 'false',
id: experimentId,
variants: {},
variantNames: [],
Expand Down Expand Up @@ -365,6 +368,26 @@ async function getConfig(experiment, instantExperiment, pluginOptions, context)
return null;
}

// Load MAB split overrides
if (['active', 'on', 'true'].includes(context.toClassName(experimentConfig.selfLearning))) {
try {
const request = await fetch(pluginOptions.mabConfig);
const json = await request.json();
const [, pageConfig] = Object.entries(json)
.find(([url]) => new URL(url).pathname === window.location.pathname);
const mabConfig = pageConfig
? pageConfig[context.toClassName(experimentConfig.id)]
: null;
if (mabConfig) {
Object.entries(experimentConfig.variants).forEach(([k, v]) => {
v.percentageSplit = (mabConfig[k]).toFixed(4);
});
}
} catch (err) {
// Nothing to do
}
}

const forcedAudience = usp.has(pluginOptions.audiencesQueryParameter)
? context.toClassName(usp.get(pluginOptions.audiencesQueryParameter))
: null;
Expand Down