Skip to content

Commit c8e9cb6

Browse files
PoC conditional experiment matching
1 parent c130623 commit c8e9cb6

File tree

1 file changed

+46
-1
lines changed

1 file changed

+46
-1
lines changed

injected/src/config-feature.js

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,18 @@ export default class ConfigFeature {
1616
/** @type {string} */
1717
name;
1818

19-
/** @type {{ debug?: boolean, desktopModeEnabled?: boolean, forcedZoomEnabled?: boolean, featureSettings?: Record<string, unknown>, assets?: import('./content-feature.js').AssetConfig | undefined, site: import('./content-feature.js').Site, messagingConfig?: import('@duckduckgo/messaging').MessagingConfig } | null} */
19+
/**
20+
* @type {{
21+
* debug?: boolean,
22+
* desktopModeEnabled?: boolean,
23+
* forcedZoomEnabled?: boolean,
24+
* featureSettings?: Record<string, unknown>,
25+
* assets?: import('./content-feature.js').AssetConfig | undefined,
26+
* site: import('./content-feature.js').Site,
27+
* messagingConfig?: import('@duckduckgo/messaging').MessagingConfig,
28+
* currentCohorts?: [{feature: string, cohort: string, subfeature: string}],
29+
* } | null}
30+
*/
2031
#args;
2132

2233
/**
@@ -95,6 +106,9 @@ export default class ConfigFeature {
95106
* @typedef {object} ConditionBlock
96107
* @property {string[] | string} [domain]
97108
* @property {object} [urlPattern]
109+
* @property {object} [experiment]
110+
* @property {string} [experiment.experimentName]
111+
* @property {string} [experiment.cohort]
98112
*/
99113

100114
/**
@@ -121,6 +135,7 @@ export default class ConfigFeature {
121135
const conditionChecks = {
122136
domain: this._matchDomainConditional,
123137
urlPattern: this._matchUrlPatternConditional,
138+
experiment: this._matchExperimentConditional,
124139
};
125140

126141
for (const key in conditionBlock) {
@@ -152,6 +167,36 @@ export default class ConfigFeature {
152167
return true;
153168
}
154169

170+
/**
171+
* Takes a condition block and returns true if the current experiment matches the experimentName and cohort.
172+
* Expects:
173+
* ```json
174+
* {
175+
* "experiment": {
176+
* "experimentName": "experimentName",
177+
* "cohort": "cohort-name"
178+
* }
179+
* }
180+
* ```
181+
* Where featureName "ContentScopeExperiments" has a subfeature "experimentName" and cohort "cohort-name"
182+
* @param {ConditionBlock} conditionBlock
183+
* @returns {boolean}
184+
*/
185+
_matchExperimentConditional(conditionBlock) {
186+
if (!conditionBlock.experiment) return false;
187+
const experiment = conditionBlock.experiment;
188+
if (!experiment.experimentName || !experiment.cohort) return false;
189+
const currentCohorts = this.args?.currentCohorts;
190+
if (!currentCohorts) return false;
191+
return currentCohorts.some((cohort) => {
192+
return (
193+
cohort.feature === "ContentScopeExperiments" &&
194+
cohort.subfeature === experiment.experimentName &&
195+
cohort.cohort === experiment.cohort
196+
);
197+
});
198+
}
199+
155200
/**
156201
* Takes a condtion block and returns true if the current url matches the urlPattern.
157202
* @param {ConditionBlock} conditionBlock

0 commit comments

Comments
 (0)