| 1 | /** @module WebABTest */ |
| 2 | |
| 3 | const EXCLUDED_BUCKET = 'unsampled'; |
| 4 | const TREATMENT_BUCKET_SUBSTRING = 'treatment'; |
| 5 | const WEB_AB_TEST_ENROLLMENT_HOOK = 'mediawiki.web_AB_test_enrollment'; |
| 6 | |
| 7 | let initialized = false; |
| 8 | |
| 9 | /** |
| 10 | * @typedef {Function} TreatmentBucketFunction |
| 11 | * @param {string} [a] |
| 12 | * @return {boolean} |
| 13 | */ |
| 14 | |
| 15 | /** |
| 16 | * @typedef {Object} WebABTest |
| 17 | * @property {string} name |
| 18 | * @property {function(): string} getBucket |
| 19 | * @property {function(string): boolean} isInBucket |
| 20 | * @property {function(): boolean} isInSample |
| 21 | * @property {TreatmentBucketFunction} isInTreatmentBucket |
| 22 | */ |
| 23 | |
| 24 | /** |
| 25 | * @typedef {Object} SamplingRate |
| 26 | * @property {number} samplingRate The desired sampling rate for the group in the range [0, 1]. |
| 27 | */ |
| 28 | |
| 29 | /** |
| 30 | * @typedef {Object} WebABTestProps |
| 31 | * @property {string} name The name of the experiment. |
| 32 | * @property {Record<string, SamplingRate>} buckets Dict with bucket name as key and SamplingRate |
| 33 | * object as value. There must be an `unsampled` bucket that represents a |
| 34 | * population excluded from the experiment. Additionally, the treatment |
| 35 | * bucket(s) must include a case-insensitive `treatment` substring in their name |
| 36 | * (e.g. `treatment`, `stickyHeaderTreatment`, `sticky-header-treatment`). |
| 37 | * @property {string} [token] Token that uniquely identifies the subject for the |
| 38 | * duration of the experiment. If bucketing/server occurs on the server and the |
| 39 | * bucket is a class on the body tag, this can be ignored. Otherwise, it is |
| 40 | * required. |
| 41 | */ |
| 42 | |
| 43 | /** |
| 44 | * Initializes an AB test experiment. |
| 45 | * |
| 46 | * Example props: |
| 47 | * |
| 48 | * webABTest({ |
| 49 | * name: 'nameOfExperiment', |
| 50 | * buckets: { |
| 51 | * unsampled: { |
| 52 | * samplingRate: 0.25 |
| 53 | * }, |
| 54 | * control: { |
| 55 | * samplingRate: 0.25 |
| 56 | * }, |
| 57 | * treatment1: { |
| 58 | * samplingRate: 0.25 |
| 59 | * }, |
| 60 | * treatment2: { |
| 61 | * samplingRate: 0.25 |
| 62 | * } |
| 63 | * }, |
| 64 | * token: 'token' |
| 65 | * }); |
| 66 | * |
| 67 | * @param {WebABTestProps} props |
| 68 | * @param {string} token |
| 69 | * @param {boolean} [forceInit] forces initialization of init event. |
| 70 | * Bypasses caching. Do not use outside tests. |
| 71 | * @return {WebABTest} |
| 72 | */ |
| 73 | module.exports = function webABTest( props, token, forceInit ) { |
| 74 | let /** @type {string} */ cachedBucket; |
| 75 | |
| 76 | /** |
| 77 | * Get the names of all the buckets from props.buckets. |
| 78 | * |
| 79 | * @return {string[]} |
| 80 | */ |
| 81 | function getBucketNames() { |
| 82 | return Object.keys( props.buckets ); |
| 83 | |
| 84 | } |
| 85 | |
| 86 | /** |
| 87 | * Get the name of the bucket from the class added to the body tag. |
| 88 | * |
| 89 | * @return {?string} |
| 90 | */ |
| 91 | function getBucketFromHTML() { |
| 92 | for ( const bucketName of getBucketNames() ) { |
| 93 | if ( document.body.classList.contains( `${ props.name }-${ bucketName }` ) ) { |
| 94 | return bucketName; |
| 95 | } |
| 96 | } |
| 97 | return null; |
| 98 | } |
| 99 | |
| 100 | /** |
| 101 | * Get the name of the bucket the subject is assigned to for A/B testing. |
| 102 | * |
| 103 | * @throws {Error} Will throw an error if token is undefined and body tag has |
| 104 | * not been assigned a bucket. |
| 105 | * @return {string} the name of the bucket the subject is assigned. |
| 106 | */ |
| 107 | function getBucket() { |
| 108 | if ( cachedBucket ) { |
| 109 | // If we've already cached the bucket, return early. |
| 110 | return cachedBucket; |
| 111 | } |
| 112 | |
| 113 | const bucketFromHTML = getBucketFromHTML(); |
| 114 | // If bucketing/sampling already occurred on the server, return that bucket |
| 115 | // instead of trying to do it on the client. |
| 116 | if ( bucketFromHTML ) { |
| 117 | cachedBucket = bucketFromHTML; |
| 118 | |
| 119 | return bucketFromHTML; |
| 120 | } |
| 121 | |
| 122 | if ( token === undefined ) { |
| 123 | throw new Error( 'Tried to call `getBucket` with an undefined token' ); |
| 124 | } |
| 125 | |
| 126 | cachedBucket = mw.experiments.getBucket( { |
| 127 | name: props.name, |
| 128 | enabled: true, |
| 129 | buckets: getBucketNames().reduce( |
| 130 | ( /** @type {Record<string, number>} */ buckets, key ) => { |
| 131 | buckets[ key ] = props.buckets[ key ].samplingRate; |
| 132 | return buckets; |
| 133 | }, {} ) |
| 134 | }, token ); |
| 135 | |
| 136 | return cachedBucket; |
| 137 | } |
| 138 | |
| 139 | /** |
| 140 | * Whether or not the subject is included in the experiment. |
| 141 | * |
| 142 | * @return {boolean} |
| 143 | */ |
| 144 | function isInSample() { |
| 145 | return getBucket() !== EXCLUDED_BUCKET; |
| 146 | } |
| 147 | |
| 148 | /** |
| 149 | * Determine if subject is in a particular bucket. |
| 150 | * |
| 151 | * @param {string} targetBucket The target test bucket. |
| 152 | * @return {boolean} Whether the subject is in the test bucket. |
| 153 | */ |
| 154 | function isInBucket( targetBucket ) { |
| 155 | return getBucket() === targetBucket; |
| 156 | } |
| 157 | |
| 158 | /** |
| 159 | * Whether or not the subject has been bucketed in a treatment bucket as |
| 160 | * defined by the bucket name containing the case-insensitive 'treatment', |
| 161 | * 'treatment1', or 'treatment2' substring (e.g. 'treatment', 'treatment1', |
| 162 | * 'sticky-header-treatment1' and 'stickyHeaderTreatment2' are all assumed |
| 163 | * to be treatment buckets). |
| 164 | * |
| 165 | * @param {string|null} [treatmentBucketName] lowercase name of bucket. |
| 166 | * @return {boolean} |
| 167 | */ |
| 168 | function isInTreatmentBucket( treatmentBucketName = '' ) { |
| 169 | const bucketLowerCase = getBucket().toLowerCase(); |
| 170 | // Array.prototype.includes` is ES7 |
| 171 | return bucketLowerCase.indexOf( `${ TREATMENT_BUCKET_SUBSTRING }${ treatmentBucketName }` ) > -1; |
| 172 | } |
| 173 | |
| 174 | /** |
| 175 | * Initialize and fire hook to register A/B test enrollment if necessary. |
| 176 | */ |
| 177 | function init() { |
| 178 | // Send data to WikimediaEvents to log A/B test initialization if the subject |
| 179 | // has been sampled into the experiment. |
| 180 | if ( isInSample() ) { |
| 181 | /** |
| 182 | * For use by WikimediaEvents only |
| 183 | * |
| 184 | * @event mediawiki.web_AB_test_enrollment |
| 185 | * @internal |
| 186 | * @property {string} context |
| 187 | * @property {string} action |
| 188 | */ |
| 189 | mw.hook( WEB_AB_TEST_ENROLLMENT_HOOK ).fire( { |
| 190 | group: getBucket(), |
| 191 | experimentName: props.name |
| 192 | } ); |
| 193 | // don't run this again. |
| 194 | initialized = true; |
| 195 | } |
| 196 | } |
| 197 | |
| 198 | if ( !initialized || forceInit ) { |
| 199 | init(); |
| 200 | } |
| 201 | |
| 202 | /** |
| 203 | * @typedef {Object} WebABTest |
| 204 | * @property {string} name |
| 205 | * @property {getBucket} getBucket |
| 206 | * @property {isInBucket} isInBucket |
| 207 | * @property {isInSample} isInSample |
| 208 | * @property {isInTreatmentBucket} isInTreatmentBucket |
| 209 | */ |
| 210 | return { |
| 211 | name: props.name, |
| 212 | getBucket, |
| 213 | isInBucket, |
| 214 | isInSample, |
| 215 | isInTreatmentBucket |
| 216 | }; |
| 217 | }; |