mediawiki-skins-MinervaNeue/resources/skins.minerva.scripts/AB.js
Jan Drewniak e27cbf2a49 Reading depth hook should send additional sampling bucket parameter
The hook that enables the Reading depth test should send an
additional paramter that specifies which test bucket the hook
being is calling from.

Bug: T191532
Change-Id: Ifd9f43220c476ece8a0c0cee46b62b58a717c616
2018-07-31 11:40:29 +00:00

79 lines
1.8 KiB
JavaScript

/*
* Bucketing wrapper for creating AB-tests.
*
* Given a test name, sampling rate, and session ID, provides a class
* that buckets users into predefined bucket ("control", "A", "B") and
* starts an AB-test.
*/
( function ( mw, M, mwExperiments ) {
/**
* Buckets users based on params and exposes an `isEnabled` and `getBucket` method.
*
* @param {Object} config configuration object for AB test.
* @param {string} config.testName
* @param {number} config.samplingRate sampling rate for the AB-test.
* @param {number} config.sessionId session ID for user bucketing
* @param {function} [config.onABStart] function that triggers event-logging when user is either
* in bucket A or B.
* @constructor
*/
function AB( config ) {
var CONTROL_BUCKET = 'control',
testName = config.testName,
samplingRate = config.samplingRate,
sessionId = config.sessionId,
onABStart = config.onABStart || function () {},
test = {
name: testName,
enabled: !!samplingRate,
buckets: {
control: 1 - samplingRate,
A: samplingRate / 2,
B: samplingRate / 2
}
};
/**
* Gets the users AB-test bucket
*
* @return {string} AB-test bucket, CONTROL_BUCKET by default, "A" or "B" buckets otherwise.
*/
function getBucket() {
return mwExperiments.getBucket( test, sessionId );
}
/**
* Checks whether or not a user is in the AB-test,
*
* @return {boolean}
*/
function isEnabled() {
return getBucket() !== CONTROL_BUCKET;
}
/**
* Initiates the AB-test.
*
* return {void}
*/
function init() {
if ( isEnabled() ) {
onABStart( getBucket() );
}
}
init();
return {
getBucket: getBucket,
isEnabled: isEnabled
};
}
M.define( 'skins.minerva.scripts/AB', AB );
}( mw, mw.mobileFrontend, mw.experiments ) );