ABtest for mobile, logged out users

Bug: T320993
Change-Id: I3530c540ec20f8806abcd7010b4d4933d1b53cfc
This commit is contained in:
David Lynch 2022-10-31 16:03:49 -05:00
parent 2b0d2deb4a
commit 9021a45966
3 changed files with 30 additions and 8 deletions

View file

@ -309,11 +309,22 @@ class HookUtils {
$abtest = $dtConfig->get( 'DiscussionToolsABTest' );
if (
$user->isRegistered() &&
( $feature ? ( $abtest == $feature ) : (bool)$abtest )
) {
return $user->getId() % 2 == 0 ? 'test' : 'control';
if ( $feature ? ( $abtest == $feature ) : (bool)$abtest ) {
if ( $user->isRegistered() ) {
return $user->getId() % 2 == 0 ? 'test' : 'control';
}
// logged out
$req = RequestContext::getMain()->getRequest();
$cookie = $req->getCookie( 'DTAB', '' );
if ( $cookie ) {
return $cookie;
}
// we just want to remember this across all calls in this request
static $bucket = false;
if ( !$bucket ) {
$bucket = rand( 0, 1 ) <= 0.5 ? 'test' : 'control';
}
return $bucket;
}
return '';
}

View file

@ -102,7 +102,12 @@ class PageHooks implements
}
// Load modules if any DT feature is enabled for this user
if ( HookUtils::isFeatureEnabledForOutput( $output ) ) {
if (
HookUtils::isFeatureEnabledForOutput( $output ) ||
// If there's an a/b test we need to include the JS for unregistered users just so
// we can make sure we store the bucket
( $this->config->get( 'DiscussionToolsABTest' ) && !$user->isRegistered() )
) {
$output->addModules( [
'ext.discussionTools.init'
] );

View file

@ -11,8 +11,14 @@ mw.dt.initState = {
firstLoad: true
};
// Cleaning up anonymous A/B test token; remove later.
mw.storage.remove( 'DTNewTopicABToken' );
// A/B test for logged out users:
if ( mw.user.isAnon() && mw.config.get( 'wgDiscussionToolsABTest' ) && mw.config.get( 'wgDiscussionToolsABTestBucket' ) ) {
var token = mw.cookie.get( 'DTABid', undefined, mw.user.generateRandomSessionId() );
mw.cookie.set( 'DTAB', mw.config.get( 'wgDiscussionToolsABTestBucket' ), { path: '/', expires: 90 * 86400, prefix: '' } );
mw.cookie.set( 'DTABid', token, { path: '/', expires: 90 * 86400, prefix: '' } );
mw.config.set( 'wgDiscussionToolsAnonymousUserId', token );
}
if ( url.searchParams.get( 'dtrepliedto' ) ) {
// If we had to reload the page to highlight the new comment, extract that data from the URL and