🚀 Big News: Socket Acquires Coana to Bring Reachability Analysis to Every Appsec Team.Learn more
Socket
Book a DemoInstallSign in
Socket

@appannie/ab-testing

Package Overview
Dependencies
Maintainers
4
Versions
15
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@appannie/ab-testing - npm Package Compare versions

Comparing version

to
1.1.0

CHANGELOG.md

7

lib/index.d.ts

@@ -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;

56

lib/index.js

@@ -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"
}