extensions/amp-experiment/0.1/variant.js
/**
 * Allocates the current page view to an experiment variant based on the given
 * experiment config.
 * @param {!../../../src/service/ampdoc-impl.AmpDoc} ampdoc
 * @param {string} experimentName
 * @param {!JsonObject} config
 * @return {!Promise<?string>}
 */
Allocates the current page view to an experiment variant based on the given
experiment config.
Allocates the current page view to an experiment variant based on the given experiment config .
function allocateVariant(ampdoc, experimentName, config) {
  assertName(experimentName);
  validateConfig(config);
  const viewer = Services.viewerForDoc(ampdoc);
  const override = viewer.getParam(ATTR_PREFIX + experimentName);
  if (override && config['variants'].hasOwnProperty(override)) {
    return Promise.resolve( (override));
  }
  const sticky = config['sticky'] !== false;
  const cidScope = config['cidScope'] || 'amp-experiment';
  let hasConsentPromise = Promise.resolve(true);
  if (sticky && config['consentNotificationId']) {
    hasConsentPromise = Services.userNotificationManagerForDoc(ampdoc)
        .then(manager => manager.getNotification(
            config['consentNotificationId']))
        .then(userNotification => {
          user().assert(userNotification,
              `Notification not found: ${config['consentNotificationId']}`);
          return userNotification.isDismissed();
        });
  }
  return hasConsentPromise.then(hasConsent => {
    if (!hasConsent) {
      return null;
    }
    const group = config['group'] || experimentName;
    return getBucketTicket(ampdoc, group, sticky ? cidScope : null)
        .then(ticket => {
          let upperBound = 0;
          const variantNames = Object.keys(config['variants']).sort();
          for (let i = 0; i < variantNames.length; i++) {
            upperBound += config['variants'][variantNames[i]];
            if (ticket < upperBound) {
              return variantNames[i];
            }
          }
          return null;
        });
  });
}
function allocateVariant ( ampdoc , experimentName , config ) { assertName ( experimentName ) ; validateConfig ( config ) ; const viewer = Services . viewerForDoc ( ampdoc ) ; const override = viewer . getParam ( ATTR_PREFIX + experimentName ) ; if ( override && config [ 'variants' ] . hasOwnProperty ( override ) ) { return Promise . resolve ( ( override ) ) ; } const sticky = config [ 'sticky' ] !== false ; const cidScope = config [ 'cidScope' ] || 'amp-experiment' ; let hasConsentPromise = Promise . resolve ( true ) ; if ( sticky && config [ 'consentNotificationId' ] ) { hasConsentPromise = Services . userNotificationManagerForDoc ( ampdoc ) . then ( manager => manager . getNotification ( config [ 'consentNotificationId' ] ) ) . then ( userNotification => { user ( ) . assert ( userNotification , ` ${ config [ 'consentNotificationId' ] } ` ) ; return userNotification . isDismissed ( ) ; } ) ; } return hasConsentPromise . then ( hasConsent => { if ( ! hasConsent ) { return null ; } const group = config [ 'group' ] || experimentName ; return getBucketTicket ( ampdoc , group , sticky ? cidScope : null ) . then ( ticket => { let upperBound = 0 ; const variantNames = Object . keys ( config [ 'variants' ] ) . sort ( ) ; for ( let i = 0 ; i < variantNames . length ; i ++ ) { upperBound += config [ 'variants' ] [ variantNames [ i ] ] ; if ( ticket < upperBound ) { return variantNames [ i ] ; } } return null ; } ) ; } ) ; }
Allocates the current page view to an experiment variant based on the given
 experiment config.
Allocates the current page view to an experiment variant based on the given experiment config .
// Variant can be overridden from URL fragment. /** @type {?string} */ // Loop through keys in a specific order since the default object key // enumeration is implementation (browser) dependent.