2022-03-17 23:01:17 +00:00
|
|
|
/** @module WebABTest */
|
|
|
|
|
|
|
|
const EXCLUDED_BUCKET = 'unsampled';
|
|
|
|
const TREATMENT_BUCKET_SUBSTRING = 'treatment';
|
|
|
|
const WEB_AB_TEST_ENROLLMENT_HOOK = 'mediawiki.web_AB_test_enrollment';
|
2023-05-11 19:03:15 +00:00
|
|
|
|
|
|
|
let initialized = false;
|
|
|
|
|
Sticky header AB test bucketing for 2 treatment buckets
For idwiki/viwiki, we wish to run the sticky header edit button AB
test so that treatment1 group sees the sticky header without edit
buttons, treatment2 groups sees the sticky header with edit buttons,
and the control/unsampled groups see no sticky header at all.
This patch overrides the configuration to make the sticky header
w/o edit buttons for treatment1, sticky header w/ edit buttons for
treatment2, and hides sticky header for everyone else. This depends
on a configuration with the treatment groups having "treatment1"
and "treatment2" as substrings in their bucket names.
The full configuration for idwiki/viwiki would be something like
the following:
```
$wgVectorStickyHeader = [
"logged_in" => true,
"logged_out" => false,
];
$wgVectorStickyHeaderEdit = [
"logged_in" => true,
"logged_out" => false,
];
$wgVectorWebABTestEnrollment = [
"name" => "vector.sticky_header_edit",
"enabled" => true,
"buckets" => [
"unsampled" => [
"samplingRate" => 0
],
"noStickyHeaderControl" => [
"samplingRate" => 0.34
],
"stickyHeaderNoEditButtonTreatment1" => [
"samplingRate" => 0.33
],
"stickyHeaderEditButtonTreatment2" => [
"samplingRate" => 0.33
]
],
];
```
Bug: T312573
Change-Id: I15c360fdf5393f5594602acc33b5b916e904016d
2022-07-07 19:06:14 +00:00
|
|
|
/**
|
|
|
|
* @typedef {Function} TreatmentBucketFunction
|
|
|
|
* @param {string} [a]
|
|
|
|
* @return {boolean}
|
|
|
|
*/
|
2022-03-17 23:01:17 +00:00
|
|
|
|
2022-04-14 03:58:05 +00:00
|
|
|
/**
|
|
|
|
* @typedef {Object} WebABTest
|
|
|
|
* @property {string} name
|
|
|
|
* @property {function(): string} getBucket
|
|
|
|
* @property {function(string): boolean} isInBucket
|
|
|
|
* @property {function(): boolean} isInSample
|
Sticky header AB test bucketing for 2 treatment buckets
For idwiki/viwiki, we wish to run the sticky header edit button AB
test so that treatment1 group sees the sticky header without edit
buttons, treatment2 groups sees the sticky header with edit buttons,
and the control/unsampled groups see no sticky header at all.
This patch overrides the configuration to make the sticky header
w/o edit buttons for treatment1, sticky header w/ edit buttons for
treatment2, and hides sticky header for everyone else. This depends
on a configuration with the treatment groups having "treatment1"
and "treatment2" as substrings in their bucket names.
The full configuration for idwiki/viwiki would be something like
the following:
```
$wgVectorStickyHeader = [
"logged_in" => true,
"logged_out" => false,
];
$wgVectorStickyHeaderEdit = [
"logged_in" => true,
"logged_out" => false,
];
$wgVectorWebABTestEnrollment = [
"name" => "vector.sticky_header_edit",
"enabled" => true,
"buckets" => [
"unsampled" => [
"samplingRate" => 0
],
"noStickyHeaderControl" => [
"samplingRate" => 0.34
],
"stickyHeaderNoEditButtonTreatment1" => [
"samplingRate" => 0.33
],
"stickyHeaderEditButtonTreatment2" => [
"samplingRate" => 0.33
]
],
];
```
Bug: T312573
Change-Id: I15c360fdf5393f5594602acc33b5b916e904016d
2022-07-07 19:06:14 +00:00
|
|
|
* @property {TreatmentBucketFunction} isInTreatmentBucket
|
2022-04-14 03:58:05 +00:00
|
|
|
*/
|
|
|
|
|
2021-10-26 23:37:56 +00:00
|
|
|
/**
|
2022-03-17 23:01:17 +00:00
|
|
|
* @typedef {Object} SamplingRate
|
|
|
|
* @property {number} samplingRate The desired sampling rate for the group in the range [0, 1].
|
2021-10-26 23:37:56 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
2022-03-17 23:01:17 +00:00
|
|
|
* @typedef {Object} WebABTestProps
|
2023-03-28 18:45:35 +00:00
|
|
|
* @property {string} name The name of the experiment.
|
2023-04-11 17:22:58 +00:00
|
|
|
* @property {Record<string, SamplingRate>} buckets Dict with bucket name as key and SamplingRate
|
2022-03-17 23:01:17 +00:00
|
|
|
* object as value. There must be an `unsampled` bucket that represents a
|
|
|
|
* population excluded from the experiment. Additionally, the treatment
|
|
|
|
* bucket(s) must include a case-insensitive `treatment` substring in their name
|
|
|
|
* (e.g. `treatment`, `stickyHeaderTreatment`, `sticky-header-treatment`).
|
|
|
|
* @property {string} [token] Token that uniquely identifies the subject for the
|
|
|
|
* duration of the experiment. If bucketing/server occurs on the server and the
|
|
|
|
* bucket is a class on the body tag, this can be ignored. Otherwise, it is
|
|
|
|
* required.
|
2021-10-26 23:37:56 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
2022-03-17 23:01:17 +00:00
|
|
|
* Initializes an AB test experiment.
|
|
|
|
*
|
|
|
|
* Example props:
|
|
|
|
*
|
|
|
|
* webABTest({
|
|
|
|
* name: 'nameOfExperiment',
|
|
|
|
* buckets: {
|
|
|
|
* unsampled: {
|
Sticky header AB test bucketing for 2 treatment buckets
For idwiki/viwiki, we wish to run the sticky header edit button AB
test so that treatment1 group sees the sticky header without edit
buttons, treatment2 groups sees the sticky header with edit buttons,
and the control/unsampled groups see no sticky header at all.
This patch overrides the configuration to make the sticky header
w/o edit buttons for treatment1, sticky header w/ edit buttons for
treatment2, and hides sticky header for everyone else. This depends
on a configuration with the treatment groups having "treatment1"
and "treatment2" as substrings in their bucket names.
The full configuration for idwiki/viwiki would be something like
the following:
```
$wgVectorStickyHeader = [
"logged_in" => true,
"logged_out" => false,
];
$wgVectorStickyHeaderEdit = [
"logged_in" => true,
"logged_out" => false,
];
$wgVectorWebABTestEnrollment = [
"name" => "vector.sticky_header_edit",
"enabled" => true,
"buckets" => [
"unsampled" => [
"samplingRate" => 0
],
"noStickyHeaderControl" => [
"samplingRate" => 0.34
],
"stickyHeaderNoEditButtonTreatment1" => [
"samplingRate" => 0.33
],
"stickyHeaderEditButtonTreatment2" => [
"samplingRate" => 0.33
]
],
];
```
Bug: T312573
Change-Id: I15c360fdf5393f5594602acc33b5b916e904016d
2022-07-07 19:06:14 +00:00
|
|
|
* samplingRate: 0.25
|
2022-03-17 23:01:17 +00:00
|
|
|
* },
|
|
|
|
* control: {
|
|
|
|
* samplingRate: 0.25
|
|
|
|
* },
|
Sticky header AB test bucketing for 2 treatment buckets
For idwiki/viwiki, we wish to run the sticky header edit button AB
test so that treatment1 group sees the sticky header without edit
buttons, treatment2 groups sees the sticky header with edit buttons,
and the control/unsampled groups see no sticky header at all.
This patch overrides the configuration to make the sticky header
w/o edit buttons for treatment1, sticky header w/ edit buttons for
treatment2, and hides sticky header for everyone else. This depends
on a configuration with the treatment groups having "treatment1"
and "treatment2" as substrings in their bucket names.
The full configuration for idwiki/viwiki would be something like
the following:
```
$wgVectorStickyHeader = [
"logged_in" => true,
"logged_out" => false,
];
$wgVectorStickyHeaderEdit = [
"logged_in" => true,
"logged_out" => false,
];
$wgVectorWebABTestEnrollment = [
"name" => "vector.sticky_header_edit",
"enabled" => true,
"buckets" => [
"unsampled" => [
"samplingRate" => 0
],
"noStickyHeaderControl" => [
"samplingRate" => 0.34
],
"stickyHeaderNoEditButtonTreatment1" => [
"samplingRate" => 0.33
],
"stickyHeaderEditButtonTreatment2" => [
"samplingRate" => 0.33
]
],
];
```
Bug: T312573
Change-Id: I15c360fdf5393f5594602acc33b5b916e904016d
2022-07-07 19:06:14 +00:00
|
|
|
* treatment1: {
|
|
|
|
* samplingRate: 0.25
|
|
|
|
* },
|
|
|
|
* treatment2: {
|
2022-03-17 23:01:17 +00:00
|
|
|
* samplingRate: 0.25
|
|
|
|
* }
|
|
|
|
* },
|
|
|
|
* token: 'token'
|
|
|
|
* });
|
2021-10-26 23:37:56 +00:00
|
|
|
*
|
2022-03-17 23:01:17 +00:00
|
|
|
* @param {WebABTestProps} props
|
2023-05-11 19:03:15 +00:00
|
|
|
* @param {string} token
|
|
|
|
* @param {boolean} [forceInit] forces initialization of init event.
|
|
|
|
* Bypasses caching. Do not use outside tests.
|
2022-03-17 23:01:17 +00:00
|
|
|
* @return {WebABTest}
|
2021-10-26 23:37:56 +00:00
|
|
|
*/
|
2023-05-11 19:03:15 +00:00
|
|
|
module.exports = function webABTest( props, token, forceInit ) {
|
2022-03-17 23:01:17 +00:00
|
|
|
let /** @type {string} */ cachedBucket;
|
|
|
|
|
2021-10-26 23:37:56 +00:00
|
|
|
/**
|
2022-03-17 23:01:17 +00:00
|
|
|
* Get the names of all the buckets from props.buckets.
|
2021-10-26 23:37:56 +00:00
|
|
|
*
|
2022-03-17 23:01:17 +00:00
|
|
|
* @return {string[]}
|
2021-10-26 23:37:56 +00:00
|
|
|
*/
|
2022-03-17 23:01:17 +00:00
|
|
|
function getBucketNames() {
|
|
|
|
return Object.keys( props.buckets );
|
2021-10-26 23:37:56 +00:00
|
|
|
|
2022-03-17 23:01:17 +00:00
|
|
|
}
|
2021-10-26 23:37:56 +00:00
|
|
|
|
2022-03-17 23:01:17 +00:00
|
|
|
/**
|
|
|
|
* Get the name of the bucket from the class added to the body tag.
|
|
|
|
*
|
|
|
|
* @return {?string}
|
|
|
|
*/
|
|
|
|
function getBucketFromHTML() {
|
|
|
|
for ( const bucketName of getBucketNames() ) {
|
2024-01-11 19:08:25 +00:00
|
|
|
if ( document.body.classList.contains( `${ props.name }-${ bucketName }` ) ) {
|
2022-03-17 23:01:17 +00:00
|
|
|
return bucketName;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return null;
|
2021-10-26 23:37:56 +00:00
|
|
|
}
|
|
|
|
|
2022-03-17 23:01:17 +00:00
|
|
|
/**
|
|
|
|
* Get the name of the bucket the subject is assigned to for A/B testing.
|
|
|
|
*
|
|
|
|
* @throws {Error} Will throw an error if token is undefined and body tag has
|
|
|
|
* not been assigned a bucket.
|
|
|
|
* @return {string} the name of the bucket the subject is assigned.
|
|
|
|
*/
|
|
|
|
function getBucket() {
|
|
|
|
if ( cachedBucket ) {
|
|
|
|
// If we've already cached the bucket, return early.
|
|
|
|
return cachedBucket;
|
|
|
|
}
|
2021-12-03 19:19:27 +00:00
|
|
|
|
2022-03-17 23:01:17 +00:00
|
|
|
const bucketFromHTML = getBucketFromHTML();
|
|
|
|
// If bucketing/sampling already occurred on the server, return that bucket
|
|
|
|
// instead of trying to do it on the client.
|
|
|
|
if ( bucketFromHTML ) {
|
|
|
|
cachedBucket = bucketFromHTML;
|
|
|
|
|
|
|
|
return bucketFromHTML;
|
|
|
|
}
|
|
|
|
|
2023-05-11 19:03:15 +00:00
|
|
|
if ( token === undefined ) {
|
2022-03-17 23:01:17 +00:00
|
|
|
throw new Error( 'Tried to call `getBucket` with an undefined token' );
|
2021-10-26 23:37:56 +00:00
|
|
|
}
|
2022-03-17 23:01:17 +00:00
|
|
|
|
|
|
|
cachedBucket = mw.experiments.getBucket( {
|
|
|
|
name: props.name,
|
|
|
|
enabled: true,
|
2023-04-11 17:22:58 +00:00
|
|
|
buckets: getBucketNames().reduce(
|
|
|
|
( /** @type {Record<string, number>} */ buckets, key ) => {
|
|
|
|
buckets[ key ] = props.buckets[ key ].samplingRate;
|
|
|
|
return buckets;
|
|
|
|
}, {} )
|
2023-05-11 19:03:15 +00:00
|
|
|
}, token );
|
2022-03-17 23:01:17 +00:00
|
|
|
|
|
|
|
return cachedBucket;
|
2021-10-26 23:37:56 +00:00
|
|
|
}
|
2022-03-17 23:01:17 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Whether or not the subject is included in the experiment.
|
|
|
|
*
|
|
|
|
* @return {boolean}
|
|
|
|
*/
|
|
|
|
function isInSample() {
|
|
|
|
return getBucket() !== EXCLUDED_BUCKET;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Determine if subject is in a particular bucket.
|
|
|
|
*
|
|
|
|
* @param {string} targetBucket The target test bucket.
|
|
|
|
* @return {boolean} Whether the subject is in the test bucket.
|
|
|
|
*/
|
|
|
|
function isInBucket( targetBucket ) {
|
|
|
|
return getBucket() === targetBucket;
|
2022-01-26 22:10:35 +00:00
|
|
|
}
|
2022-03-17 23:01:17 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Whether or not the subject has been bucketed in a treatment bucket as
|
Sticky header AB test bucketing for 2 treatment buckets
For idwiki/viwiki, we wish to run the sticky header edit button AB
test so that treatment1 group sees the sticky header without edit
buttons, treatment2 groups sees the sticky header with edit buttons,
and the control/unsampled groups see no sticky header at all.
This patch overrides the configuration to make the sticky header
w/o edit buttons for treatment1, sticky header w/ edit buttons for
treatment2, and hides sticky header for everyone else. This depends
on a configuration with the treatment groups having "treatment1"
and "treatment2" as substrings in their bucket names.
The full configuration for idwiki/viwiki would be something like
the following:
```
$wgVectorStickyHeader = [
"logged_in" => true,
"logged_out" => false,
];
$wgVectorStickyHeaderEdit = [
"logged_in" => true,
"logged_out" => false,
];
$wgVectorWebABTestEnrollment = [
"name" => "vector.sticky_header_edit",
"enabled" => true,
"buckets" => [
"unsampled" => [
"samplingRate" => 0
],
"noStickyHeaderControl" => [
"samplingRate" => 0.34
],
"stickyHeaderNoEditButtonTreatment1" => [
"samplingRate" => 0.33
],
"stickyHeaderEditButtonTreatment2" => [
"samplingRate" => 0.33
]
],
];
```
Bug: T312573
Change-Id: I15c360fdf5393f5594602acc33b5b916e904016d
2022-07-07 19:06:14 +00:00
|
|
|
* defined by the bucket name containing the case-insensitive 'treatment',
|
|
|
|
* 'treatment1', or 'treatment2' substring (e.g. 'treatment', 'treatment1',
|
|
|
|
* 'sticky-header-treatment1' and 'stickyHeaderTreatment2' are all assumed
|
|
|
|
* to be treatment buckets).
|
2022-03-17 23:01:17 +00:00
|
|
|
*
|
Sticky header AB test bucketing for 2 treatment buckets
For idwiki/viwiki, we wish to run the sticky header edit button AB
test so that treatment1 group sees the sticky header without edit
buttons, treatment2 groups sees the sticky header with edit buttons,
and the control/unsampled groups see no sticky header at all.
This patch overrides the configuration to make the sticky header
w/o edit buttons for treatment1, sticky header w/ edit buttons for
treatment2, and hides sticky header for everyone else. This depends
on a configuration with the treatment groups having "treatment1"
and "treatment2" as substrings in their bucket names.
The full configuration for idwiki/viwiki would be something like
the following:
```
$wgVectorStickyHeader = [
"logged_in" => true,
"logged_out" => false,
];
$wgVectorStickyHeaderEdit = [
"logged_in" => true,
"logged_out" => false,
];
$wgVectorWebABTestEnrollment = [
"name" => "vector.sticky_header_edit",
"enabled" => true,
"buckets" => [
"unsampled" => [
"samplingRate" => 0
],
"noStickyHeaderControl" => [
"samplingRate" => 0.34
],
"stickyHeaderNoEditButtonTreatment1" => [
"samplingRate" => 0.33
],
"stickyHeaderEditButtonTreatment2" => [
"samplingRate" => 0.33
]
],
];
```
Bug: T312573
Change-Id: I15c360fdf5393f5594602acc33b5b916e904016d
2022-07-07 19:06:14 +00:00
|
|
|
* @param {string|null} [treatmentBucketName] lowercase name of bucket.
|
2022-03-17 23:01:17 +00:00
|
|
|
* @return {boolean}
|
|
|
|
*/
|
Sticky header AB test bucketing for 2 treatment buckets
For idwiki/viwiki, we wish to run the sticky header edit button AB
test so that treatment1 group sees the sticky header without edit
buttons, treatment2 groups sees the sticky header with edit buttons,
and the control/unsampled groups see no sticky header at all.
This patch overrides the configuration to make the sticky header
w/o edit buttons for treatment1, sticky header w/ edit buttons for
treatment2, and hides sticky header for everyone else. This depends
on a configuration with the treatment groups having "treatment1"
and "treatment2" as substrings in their bucket names.
The full configuration for idwiki/viwiki would be something like
the following:
```
$wgVectorStickyHeader = [
"logged_in" => true,
"logged_out" => false,
];
$wgVectorStickyHeaderEdit = [
"logged_in" => true,
"logged_out" => false,
];
$wgVectorWebABTestEnrollment = [
"name" => "vector.sticky_header_edit",
"enabled" => true,
"buckets" => [
"unsampled" => [
"samplingRate" => 0
],
"noStickyHeaderControl" => [
"samplingRate" => 0.34
],
"stickyHeaderNoEditButtonTreatment1" => [
"samplingRate" => 0.33
],
"stickyHeaderEditButtonTreatment2" => [
"samplingRate" => 0.33
]
],
];
```
Bug: T312573
Change-Id: I15c360fdf5393f5594602acc33b5b916e904016d
2022-07-07 19:06:14 +00:00
|
|
|
function isInTreatmentBucket( treatmentBucketName = '' ) {
|
2023-03-28 18:45:35 +00:00
|
|
|
const bucketLowerCase = getBucket().toLowerCase();
|
|
|
|
// Array.prototype.includes` is ES7
|
2024-01-11 19:08:25 +00:00
|
|
|
return bucketLowerCase.indexOf( `${ TREATMENT_BUCKET_SUBSTRING }${ treatmentBucketName }` ) > -1;
|
2022-03-17 23:01:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Initialize and fire hook to register A/B test enrollment if necessary.
|
|
|
|
*/
|
|
|
|
function init() {
|
|
|
|
// Send data to WikimediaEvents to log A/B test initialization if the subject
|
|
|
|
// has been sampled into the experiment.
|
|
|
|
if ( isInSample() ) {
|
2023-10-04 00:32:27 +00:00
|
|
|
/**
|
2024-01-24 20:23:29 +00:00
|
|
|
* For use by WikimediaEvents only
|
2023-10-04 00:32:27 +00:00
|
|
|
* @event mediawiki.web_AB_test_enrollment
|
2024-01-24 20:23:29 +00:00
|
|
|
* @internal
|
2023-10-04 00:32:27 +00:00
|
|
|
* @property {string} context
|
|
|
|
* @property {string} action
|
|
|
|
*/
|
2022-03-17 23:01:17 +00:00
|
|
|
mw.hook( WEB_AB_TEST_ENROLLMENT_HOOK ).fire( {
|
|
|
|
group: getBucket(),
|
|
|
|
experimentName: props.name
|
|
|
|
} );
|
2023-05-11 19:03:15 +00:00
|
|
|
// don't run this again.
|
|
|
|
initialized = true;
|
2022-03-17 23:01:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-11 19:03:15 +00:00
|
|
|
if ( !initialized || forceInit ) {
|
|
|
|
init();
|
|
|
|
}
|
2022-03-17 23:01:17 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @typedef {Object} WebABTest
|
|
|
|
* @property {string} name
|
|
|
|
* @property {getBucket} getBucket
|
|
|
|
* @property {isInBucket} isInBucket
|
|
|
|
* @property {isInSample} isInSample
|
|
|
|
* @property {isInTreatmentBucket} isInTreatmentBucket
|
|
|
|
*/
|
|
|
|
return {
|
|
|
|
name: props.name,
|
|
|
|
getBucket,
|
|
|
|
isInBucket,
|
|
|
|
isInSample,
|
|
|
|
isInTreatmentBucket
|
|
|
|
};
|
2021-10-26 23:37:56 +00:00
|
|
|
};
|