Robmoen has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/233763

Change subject: Setup survey bucketing
......................................................................

Setup survey bucketing

Gets a bucket for each enabled survey.
Shows the first survey in bucket A that has not been
previously dismissed.

Based on upstreamed experiment work:
Icf7f6fedf0c2deb5d5548c9e24456cc7a7c6a743

Bug: T109518
Change-Id: Ib09477d30480116cdb6f3744cb6e77dc2b57817c
---
M extension.json
M resources/ext.quicksurveys.init/init.js
2 files changed, 85 insertions(+), 38 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/QuickSurveys 
refs/changes/63/233763/1

diff --git a/extension.json b/extension.json
index 8229c7f..8cdc8cd 100644
--- a/extension.json
+++ b/extension.json
@@ -56,7 +56,8 @@
                        "position": "top",
                        "dependencies": [
                                "mediawiki.user",
-                               "mediawiki.storage"
+                               "mediawiki.storage",
+                               "mediawiki.experiments"
                        ],
                        "targets": [
                                "mobile",
@@ -108,7 +109,7 @@
                                "@schema": "Which schema to log to",
                                "schema": "QuickSurveysResponse",
                                "@enabled": "whether the survey is enabled",
-                               "enabled": false,
+                               "enabled": true,
                                "@coverage": "percentage of users that will see 
the survey",
                                "coverage": "50",
                                "@platform": "for each platform (desktop, 
mobile), which version of it is targeted (stable, beta, alpha)",
diff --git a/resources/ext.quicksurveys.init/init.js 
b/resources/ext.quicksurveys.init/init.js
index 66af138..7d9af09 100644
--- a/resources/ext.quicksurveys.init/init.js
+++ b/resources/ext.quicksurveys.init/init.js
@@ -1,52 +1,98 @@
 ( function ( $ ) {
-       var survey, token, storageId, $bodyContent, $place,
+       var survey, $bodyContent, $place,
                $panel = $( '<div class="ext-qs-loader-bar 
mw-ajax-loader"></div>' ),
                availableSurveys = mw.config.get( 'wgEnabledQuickSurveys' ),
-               // https://phabricator.wikimedia.org/T109010
-               inSample = false,
                isMainPage = mw.config.get( 'wgIsMainPage' ),
-               isArticle = mw.config.get( 'wgIsArticle' );
+               isArticle = mw.config.get( 'wgIsArticle' ),
+               sessionId = mw.user.generateRandomSessionId();
 
        mw.extQuickSurveys = mw.extQuickSurveys || {};
 
+       // Do nothing when not on an article
+       if ( isMainPage && !isArticle ) {
+               return;
+       }
+
        if ( availableSurveys.length ) {
-               survey = availableSurveys[ Math.floor( Math.random() * 
availableSurveys.length ) ];
-               storageId = 'ext-quicksurvey-' + survey.name;
-               token = mw.storage.get( storageId );
+               // Get first survey in bucket A that has not been dismissed
+               $( availableSurveys ).each( function ( i, surveyConfig ) {
+                       var bucket = getBucket( surveyConfig ),
+                               token = getSurveyToken( surveyConfig );
 
-               // local storage is supported in this case as value is not 
false and when ~ it means it was dismissed
-               if ( token !== false && token !== '~' && !isMainPage && 
isArticle ) {
-
-                       if ( !token ) {
-                               token = mw.user.generateRandomSessionId();
-                               // given token !== false we can safely run this 
without exception:
-                               mw.storage.set( storageId, token );
+                       if ( bucket === 'A' && token !== '~' ) {
+                               survey = surveyConfig;
+                               return true;
                        }
+               } );
 
-                       if ( inSample || mw.util.getParamValue( 'quicksurvey' ) 
) {
-                               $bodyContent = $( '#bodyContent' );
-                               $place = $bodyContent.find( 'h1, h2, h3, h4, 
h5, h6' ).eq( 0 );
+               if ( survey ) {
+                       $bodyContent = $( '#bodyContent' );
+                       $place = $bodyContent.find( 'h1, h2, h3, h4, h5, h6' 
).eq( 0 );
 
-                               if ( $place.length ) {
-                                       $panel.insertBefore( $place );
-                               } else {
-                                       $panel.appendTo( $bodyContent );
-                               }
-                               mw.loader.using( [ survey.module, 
'ext.quicksurveys.views' ] ).done( function () {
-                                       var panel;
-                                       panel = new 
mw.extQuickSurveys.views.QuickSurvey( {
-                                               survey: survey,
-                                               templateData: {
-                                                       question: mw.msg( 
survey.question ),
-                                                       description: mw.msg( 
survey.description )
-                                               }
-                                       } );
-                                       $panel.replaceWith( panel.$element );
-                                       panel.on( 'dismiss', function () {
-                                               mw.storage.set( storageId, '~' 
);
-                                       } );
+                       if ( $place.length ) {
+                               $panel.insertBefore( $place );
+                       } else {
+                               $panel.appendTo( $bodyContent );
+                       }
+                       mw.loader.using( [ survey.module, 
'ext.quicksurveys.views' ] ).done( function () {
+                               var panel;
+                               panel = new 
mw.extQuickSurveys.views.QuickSurvey( {
+                                       survey: survey,
+                                       templateData: {
+                                               question: mw.msg( 
survey.question ),
+                                               description: mw.msg( 
survey.description )
+                                       }
                                } );
-                       }
+                               $panel.replaceWith( panel.$element );
+                               panel.on( 'dismiss', function () {
+                                       mw.storage.set( getSurveyStorageKey( 
survey ), '~' );
+                               } );
+                       } );
                }
        }
+
+       /**
+        * Get the bucket for the given survey.
+        * Initializes the survey storage with a token
+        *
+        * @returns {String} The bucket
+        */
+       function getBucket( survey ) {
+               var control = 100 - survey.coverage,
+                       a = survey.coverage,
+                       storageId = getSurveyStorageKey( survey ),
+                       token = getSurveyToken( survey );
+
+               if ( !token ) {
+                       token = sessionId;
+                       mw.storage.set( storageId, token );
+               }
+               return mw.experiments.getBucket( {
+                       name: survey.name,
+                       enabled: true,
+                       buckets: {
+                               control: control,
+                               A: a
+                       }
+               }, token );
+       }
+
+       /**
+        * Get the storage key for the given survey.
+
+        * @returns {String} The survey localstorage key
+        */
+       function getSurveyStorageKey( survey ) {
+               return 'ext-quicksurvey-' + survey.name.replace( / /g, '-' );
+       }
+
+       /**
+        * Get the survey token for the given survey.
+
+        * @returns {String} The survey token
+        */
+       function getSurveyToken( survey ) {
+               return mw.storage.get( getSurveyStorageKey( survey ) );
+       }
+
 }( jQuery ) );

-- 
To view, visit https://gerrit.wikimedia.org/r/233763
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: Ib09477d30480116cdb6f3744cb6e77dc2b57817c
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/QuickSurveys
Gerrit-Branch: dev
Gerrit-Owner: Robmoen <rm...@wikimedia.org>

_______________________________________________
MediaWiki-commits mailing list
MediaWiki-commits@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to