Commit 6a84d0fb authored by Dallas Reedy's avatar Dallas Reedy

Filter experiment data to match the gitlab_experiment context

The main thing we need to filter out, for now, is the `excluded` key.
Using an allowlist which aligns with the currently used
`gitlab_experiment` context schema is the best way to ensure all of our
frontend experiment events continue working.
parent 1bd8a690
// This file only applies to use of experiments through https://gitlab.com/gitlab-org/gitlab-experiment
import { get } from 'lodash';
import { get, mapValues, pick } from 'lodash';
import { DEFAULT_VARIANT, CANDIDATE_VARIANT, TRACKING_CONTEXT_SCHEMA } from './constants';
function getExperimentsData() {
......@@ -8,19 +8,18 @@ function getExperimentsData() {
// Pull from preferred window.gl.experiments
const experimentsFromGl = get(window, ['gl', 'experiments'], {});
return { ...experimentsFromGon, ...experimentsFromGl };
}
function convertExperimentDataToExperimentContext(experimentData) {
// Bandaid to allow-list only the properties which the current gitlab_experiment context schema suppports.
// Bandaid to allow-list only the properties which the current gitlab_experiment
// context schema suppports, since we most often use this data to create that
// Snowplow context.
// See TRACKING_CONTEXT_SCHEMA for current version (1-0-0)
// https://gitlab.com/gitlab-org/iglu/-/blob/master/public/schemas/com.gitlab/gitlab_experiment/jsonschema/1-0-0
const { experiment: experimentName, key, variant, migration_keys } = experimentData;
return mapValues({ ...experimentsFromGon, ...experimentsFromGl }, (xp) => {
return pick(xp, ['experiment', 'key', 'variant', 'migration_keys']);
});
}
return {
schema: TRACKING_CONTEXT_SCHEMA,
data: { experiment: experimentName, key, variant, migration_keys },
};
function createGitlabExperimentContext(experimentData) {
return { schema: TRACKING_CONTEXT_SCHEMA, data: experimentData };
}
export function getExperimentData(experimentName) {
......@@ -28,7 +27,7 @@ export function getExperimentData(experimentName) {
}
export function getAllExperimentContexts() {
return Object.values(getExperimentsData()).map(convertExperimentDataToExperimentContext);
return Object.values(getExperimentsData()).map(createGitlabExperimentContext);
}
export function isExperimentVariant(experimentName, variantName) {
......
......@@ -51,6 +51,29 @@ describe('experiment Utilities', () => {
expect(experimentUtils.getExperimentData(...input)).toEqual(output);
});
});
it('only collects the data properties which are supported by the schema', () => {
origGl = window.gl;
window.gl.experiments = {
my_experiment: {
experiment: 'my_experiment',
variant: 'control',
key: 'randomization-unit-key',
migration_keys: 'migration_keys object',
excluded: false,
other: 'foobar',
},
};
expect(experimentUtils.getExperimentData('my_experiment')).toEqual({
experiment: 'my_experiment',
variant: 'control',
key: 'randomization-unit-key',
migration_keys: 'migration_keys object',
});
window.gl = origGl;
});
});
describe('getAllExperimentContexts', () => {
......@@ -72,19 +95,6 @@ describe('experiment Utilities', () => {
it('returns an empty array if there are no experiments', () => {
expect(experimentUtils.getAllExperimentContexts()).toEqual([]);
});
it('only collects the data properties which are supported by the schema', () => {
origGl = window.gl;
window.gl.experiments = {
my_experiment: { experiment: 'my_experiment', variant: 'control', excluded: false },
};
expect(experimentUtils.getAllExperimentContexts()).toEqual([
{ schema, data: { experiment: 'my_experiment', variant: 'control' } },
]);
window.gl = origGl;
});
});
describe('isExperimentVariant', () => {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment