debian-mirror-gitlab/app/assets/javascripts/experimentation/utils.js

41 lines
1.2 KiB
JavaScript
Raw Normal View History

2021-04-17 20:07:23 +05:30
// This file only applies to use of experiments through https://gitlab.com/gitlab-org/gitlab-experiment
2021-11-18 22:05:49 +05:30
import { get } from 'lodash';
2021-06-08 01:23:25 +05:30
import { DEFAULT_VARIANT, CANDIDATE_VARIANT, TRACKING_CONTEXT_SCHEMA } from './constants';
2021-04-17 20:07:23 +05:30
2021-11-11 11:23:49 +05:30
function getExperimentsData() {
return get(window, ['gon', 'experiment'], {});
}
function convertExperimentDataToExperimentContext(experimentData) {
return { schema: TRACKING_CONTEXT_SCHEMA, data: experimentData };
}
2021-04-17 20:07:23 +05:30
export function getExperimentData(experimentName) {
2021-11-11 11:23:49 +05:30
return getExperimentsData()[experimentName];
2021-04-17 20:07:23 +05:30
}
2021-11-11 11:23:49 +05:30
export function getAllExperimentContexts() {
return Object.values(getExperimentsData()).map(convertExperimentDataToExperimentContext);
2021-06-08 01:23:25 +05:30
}
2021-04-17 20:07:23 +05:30
export function isExperimentVariant(experimentName, variantName) {
return getExperimentData(experimentName)?.variant === variantName;
}
2021-04-29 21:17:54 +05:30
export function getExperimentVariant(experimentName) {
return getExperimentData(experimentName)?.variant || DEFAULT_VARIANT;
}
export function experiment(experimentName, variants) {
const variant = getExperimentVariant(experimentName);
switch (variant) {
case DEFAULT_VARIANT:
return variants.use.call();
case CANDIDATE_VARIANT:
return variants.try.call();
default:
return variants[variant].call();
}
}