@appannie/ab-testing
Advanced tools
Comparing version
@@ -19,3 +19,5 @@ declare type ForceInclude = { | ||
export declare class Experiments { | ||
config: ABTestingConfig; | ||
config: { | ||
[experimentName: string]: Experiment; | ||
}; | ||
userId: number; | ||
@@ -25,2 +27,5 @@ userProfile: { | ||
}; | ||
matchedCohorts: { | ||
[experimentName: string]: string; | ||
}; | ||
constructor(config: ABTestingConfig, userId: number, userProfile: { | ||
@@ -27,0 +32,0 @@ [s: string]: string; |
@@ -10,33 +10,45 @@ "use strict"; | ||
} | ||
function matchUserCohort(experimentConfig, userId, userProfile) { | ||
const userSegmentNum = getModuloValue(experimentConfig.name, userId); | ||
let allocatedCohort = 'control'; | ||
for (const cohort of experimentConfig.cohorts) { | ||
if (cohort.force_include) { | ||
for (const key in cohort.force_include) { | ||
if (cohort.force_include[key].includes(userProfile[key])) { | ||
return cohort.name; | ||
} | ||
} | ||
} | ||
if (allocatedCohort === 'control') { | ||
for (const allocation of cohort.allocation || []) { | ||
if (allocation[0] <= userSegmentNum && userSegmentNum < allocation[1]) { | ||
allocatedCohort = cohort.name; | ||
} | ||
} | ||
} | ||
} | ||
return allocatedCohort; | ||
} | ||
class Experiments { | ||
constructor(config, userId, userProfile) { | ||
this.getCohort = (experimentName) => { | ||
const experimentConfig = this.config.experiments.find(e => e.name === experimentName); | ||
if (!experimentConfig) { | ||
console.error(`unrecognized ab testing experiment name: ${experimentName}`); | ||
return 'control'; | ||
} | ||
const userSegmentNum = getModuloValue(experimentName, this.userId); | ||
let allocatedCohort = 'control'; | ||
for (const cohort of experimentConfig.cohorts) { | ||
if (cohort.force_include) { | ||
for (const key in cohort.force_include) { | ||
if (cohort.force_include[key].includes(this.userProfile[key])) { | ||
return cohort.name; | ||
} | ||
} | ||
if (!(experimentName in this.matchedCohorts)) { | ||
const experimentConfig = this.config[experimentName]; | ||
if (experimentConfig == null) { | ||
console.error(`unrecognized ab testing experiment name: ${experimentName}`); | ||
this.matchedCohorts[experimentName] = 'control'; | ||
} | ||
if (allocatedCohort === 'control') { | ||
for (const allocation of cohort.allocation || []) { | ||
if (allocation[0] <= userSegmentNum && userSegmentNum < allocation[1]) { | ||
allocatedCohort = cohort.name; | ||
} | ||
} | ||
else { | ||
this.matchedCohorts[experimentName] = matchUserCohort(experimentConfig, this.userId, this.userProfile); | ||
} | ||
} | ||
return allocatedCohort; | ||
return this.matchedCohorts[experimentName]; | ||
}; | ||
this.config = config; | ||
this.config = {}; | ||
this.userId = userId; | ||
this.userProfile = userProfile; | ||
this.matchedCohorts = {}; | ||
for (const experimentConfig of config.experiments) { | ||
this.config[experimentConfig.name] = experimentConfig; | ||
} | ||
} | ||
@@ -43,0 +55,0 @@ } |
@@ -7,3 +7,3 @@ { | ||
}, | ||
"version": "1.0.5", | ||
"version": "1.1.0", | ||
"description": "", | ||
@@ -24,5 +24,5 @@ "files": [ | ||
"devDependencies": { | ||
"@appannie/ab-testing-hash-object": "^1.0.5" | ||
"@appannie/ab-testing-hash-object": "^1.1.0" | ||
}, | ||
"gitHead": "2cdcf6543bc02d17713cbbe9307c59909e1ab949" | ||
"gitHead": "93c711a97928c479e96f2cc87000f68655ddb569" | ||
} |
No README
QualityPackage does not have a README. This may indicate a failed publish or a low quality package.
Found 1 instance in 1 package
7171
44.26%7
40%90
23.29%1
-50%50
Infinity%