packages/optimizely-sdk/lib/core/bucketer/index.js
function(bucketerParams) {
    // Check if user is in a random group; if so, check if user is bucketed into a specific experiment
    var experiment = bucketerParams.experimentKeyMap[bucketerParams.experimentKey];
    var groupId = experiment['groupId'];
    if (groupId) {
      var group = bucketerParams.groupIdMap[groupId];
      if (!group) {
        throw new Error(sprintf(ERROR_MESSAGES.INVALID_GROUP_ID, MODULE_NAME, groupId));
      }
      if (group.policy === RANDOM_POLICY) {
        var bucketedExperimentId = module.exports.bucketUserIntoExperiment(group,
                                                                          bucketerParams.bucketingId,
                                                                          bucketerParams.userId,
                                                                          bucketerParams.logger);

        // Return if user is not bucketed into any experiment
        if (bucketedExperimentId === null) {
          var notbucketedInAnyExperimentLogMessage = sprintf(LOG_MESSAGES.USER_NOT_IN_ANY_EXPERIMENT, MODULE_NAME, bucketerParams.userId, groupId);
          bucketerParams.logger.log(LOG_LEVEL.INFO, notbucketedInAnyExperimentLogMessage);
          return null;
        }

        // Return if user is bucketed into a different experiment than the one specified
        if (bucketedExperimentId !== bucketerParams.experimentId) {
          var notBucketedIntoExperimentOfGroupLogMessage = sprintf(LOG_MESSAGES.USER_NOT_BUCKETED_INTO_EXPERIMENT_IN_GROUP, MODULE_NAME, bucketerParams.userId, bucketerParams.experimentKey, groupId);
          bucketerParams.logger.log(LOG_LEVEL.INFO, notBucketedIntoExperimentOfGroupLogMessage);
          return null;
        }

        // Continue bucketing if user is bucketed into specified experiment
        var bucketedIntoExperimentOfGroupLogMessage = sprintf(LOG_MESSAGES.USER_BUCKETED_INTO_EXPERIMENT_IN_GROUP, MODULE_NAME, bucketerParams.userId, bucketerParams.experimentKey, groupId);
        bucketerParams.logger.log(LOG_LEVEL.INFO, bucketedIntoExperimentOfGroupLogMessage);
      }
    }
    var bucketingId = sprintf('%s%s', bucketerParams.bucketingId, bucketerParams.experimentId);
    var bucketValue = module.exports._generateBucketValue(bucketingId);

    var bucketedUserLogMessage = sprintf(LOG_MESSAGES.USER_ASSIGNED_TO_VARIATION_BUCKET, MODULE_NAME, bucketValue, bucketerParams.userId);
    bucketerParams.logger.log(LOG_LEVEL.DEBUG, bucketedUserLogMessage);

    var entityId = module.exports._findBucket(bucketValue, bucketerParams.trafficAllocationConfig);
    if (entityId === null) {
      var userHasNoVariationLogMessage = sprintf(LOG_MESSAGES.USER_HAS_NO_VARIATION, MODULE_NAME, bucketerParams.userId, bucketerParams.experimentKey);
      bucketerParams.logger.log(LOG_LEVEL.DEBUG, userHasNoVariationLogMessage);
    } else if (entityId === '' || !bucketerParams.variationIdMap.hasOwnProperty(entityId)) {
      var invalidVariationIdLogMessage = sprintf(LOG_MESSAGES.INVALID_VARIATION_ID, MODULE_NAME);
      bucketerParams.logger.log(LOG_LEVEL.WARNING, invalidVariationIdLogMessage);
      return null;
    } else {
      var variationKey = bucketerParams.variationIdMap[entityId].key;
      var userInVariationLogMessage = sprintf(LOG_MESSAGES.USER_HAS_VARIATION, MODULE_NAME, bucketerParams.userId, variationKey, bucketerParams.experimentKey);
      bucketerParams.logger.log(LOG_LEVEL.INFO, userInVariationLogMessage);
    }

    return entityId;
  }
function(bucketerParams) {
    // Check if user is in a random group; if so, check if user is bucketed into a specific experiment
    var experiment = bucketerParams.experimentKeyMap[bucketerParams.experimentKey];
    var groupId = experiment['groupId'];
    if (groupId) {
      var group = bucketerParams.groupIdMap[groupId];
      if (!group) {
        throw new Error(sprintf(ERROR_MESSAGES.INVALID_GROUP_ID, MODULE_NAME, groupId));
      }
      if (group.policy === RANDOM_POLICY) {
        var bucketedExperimentId = module.exports.bucketUserIntoExperiment(group,
                                                                          bucketerParams.bucketingId,
                                                                          bucketerParams.userId,
                                                                          bucketerParams.logger);

        // Return if user is not bucketed into any experiment
        if (bucketedExperimentId === null) {
          var notbucketedInAnyExperimentLogMessage = sprintf(LOG_MESSAGES.USER_NOT_IN_ANY_EXPERIMENT, MODULE_NAME, bucketerParams.userId, groupId);
          bucketerParams.logger.log(LOG_LEVEL.INFO, notbucketedInAnyExperimentLogMessage);
          return null;
        }

        // Return if user is bucketed into a different experiment than the one specified
        if (bucketedExperimentId !== bucketerParams.experimentId) {
          var notBucketedIntoExperimentOfGroupLogMessage = sprintf(LOG_MESSAGES.USER_NOT_BUCKETED_INTO_EXPERIMENT_IN_GROUP, MODULE_NAME, bucketerParams.userId, bucketerParams.experimentKey, groupId);
          bucketerParams.logger.log(LOG_LEVEL.INFO, notBucketedIntoExperimentOfGroupLogMessage);
          return null;
        }

        // Continue bucketing if user is bucketed into specified experiment
        var bucketedIntoExperimentOfGroupLogMessage = sprintf(LOG_MESSAGES.USER_BUCKETED_INTO_EXPERIMENT_IN_GROUP, MODULE_NAME, bucketerParams.userId, bucketerParams.experimentKey, groupId);
        bucketerParams.logger.log(LOG_LEVEL.INFO, bucketedIntoExperimentOfGroupLogMessage);
      }
    }
    var bucketingId = sprintf('%s%s', bucketerParams.bucketingId, bucketerParams.experimentId);
    var bucketValue = module.exports._generateBucketValue(bucketingId);

    var bucketedUserLogMessage = sprintf(LOG_MESSAGES.USER_ASSIGNED_TO_VARIATION_BUCKET, MODULE_NAME, bucketValue, bucketerParams.userId);
    bucketerParams.logger.log(LOG_LEVEL.DEBUG, bucketedUserLogMessage);

    var entityId = module.exports._findBucket(bucketValue, bucketerParams.trafficAllocationConfig);
    if (entityId === null) {
      var userHasNoVariationLogMessage = sprintf(LOG_MESSAGES.USER_HAS_NO_VARIATION, MODULE_NAME, bucketerParams.userId, bucketerParams.experimentKey);
      bucketerParams.logger.log(LOG_LEVEL.DEBUG, userHasNoVariationLogMessage);
    } else if (entityId === '' || !bucketerParams.variationIdMap.hasOwnProperty(entityId)) {
      var invalidVariationIdLogMessage = sprintf(LOG_MESSAGES.INVALID_VARIATION_ID, MODULE_NAME);
      bucketerParams.logger.log(LOG_LEVEL.WARNING, invalidVariationIdLogMessage);
      return null;
    } else {
      var variationKey = bucketerParams.variationIdMap[entityId].key;
      var userInVariationLogMessage = sprintf(LOG_MESSAGES.USER_HAS_VARIATION, MODULE_NAME, bucketerParams.userId, variationKey, bucketerParams.experimentKey);
      bucketerParams.logger.log(LOG_LEVEL.INFO, userInVariationLogMessage);
    }

    return entityId;
  }
function ( bucketerParams ) { // Check if user is in a random group; if so, check if user is bucketed into a specific experiment var experiment = bucketerParams . experimentKeyMap [ bucketerParams . experimentKey ] ; var groupId = experiment [ 'groupId' ] ; if ( groupId ) { var group = bucketerParams . groupIdMap [ groupId ] ; if ( ! group ) { throw new Error ( sprintf ( ERROR_MESSAGES . INVALID_GROUP_ID , MODULE_NAME , groupId ) ) ; } if ( group . policy === RANDOM_POLICY ) { var bucketedExperimentId = module . exports . bucketUserIntoExperiment ( group , bucketerParams . bucketingId , bucketerParams . userId , bucketerParams . logger ) ; // Return if user is not bucketed into any experiment if ( bucketedExperimentId === null ) { var notbucketedInAnyExperimentLogMessage = sprintf ( LOG_MESSAGES . USER_NOT_IN_ANY_EXPERIMENT , MODULE_NAME , bucketerParams . userId , groupId ) ; bucketerParams . logger . log ( LOG_LEVEL . INFO , notbucketedInAnyExperimentLogMessage ) ; return null ; } // Return if user is bucketed into a different experiment than the one specified if ( bucketedExperimentId !== bucketerParams . experimentId ) { var notBucketedIntoExperimentOfGroupLogMessage = sprintf ( LOG_MESSAGES . USER_NOT_BUCKETED_INTO_EXPERIMENT_IN_GROUP , MODULE_NAME , bucketerParams . userId , bucketerParams . experimentKey , groupId ) ; bucketerParams . logger . log ( LOG_LEVEL . INFO , notBucketedIntoExperimentOfGroupLogMessage ) ; return null ; } // Continue bucketing if user is bucketed into specified experiment var bucketedIntoExperimentOfGroupLogMessage = sprintf ( LOG_MESSAGES . USER_BUCKETED_INTO_EXPERIMENT_IN_GROUP , MODULE_NAME , bucketerParams . userId , bucketerParams . experimentKey , groupId ) ; bucketerParams . logger . log ( LOG_LEVEL . INFO , bucketedIntoExperimentOfGroupLogMessage ) ; } } var bucketingId = sprintf ( '%s%s' , bucketerParams . bucketingId , bucketerParams . experimentId ) ; var bucketValue = module . exports . _generateBucketValue ( bucketingId ) ; var bucketedUserLogMessage = sprintf ( LOG_MESSAGES . USER_ASSIGNED_TO_VARIATION_BUCKET , MODULE_NAME , bucketValue , bucketerParams . userId ) ; bucketerParams . logger . log ( LOG_LEVEL . DEBUG , bucketedUserLogMessage ) ; var entityId = module . exports . _findBucket ( bucketValue , bucketerParams . trafficAllocationConfig ) ; if ( entityId === null ) { var userHasNoVariationLogMessage = sprintf ( LOG_MESSAGES . USER_HAS_NO_VARIATION , MODULE_NAME , bucketerParams . userId , bucketerParams . experimentKey ) ; bucketerParams . logger . log ( LOG_LEVEL . DEBUG , userHasNoVariationLogMessage ) ; } else if ( entityId === '' || ! bucketerParams . variationIdMap . hasOwnProperty ( entityId ) ) { var invalidVariationIdLogMessage = sprintf ( LOG_MESSAGES . INVALID_VARIATION_ID , MODULE_NAME ) ; bucketerParams . logger . log ( LOG_LEVEL . WARNING , invalidVariationIdLogMessage ) ; return null ; } else { var variationKey = bucketerParams . variationIdMap [ entityId ] . key ; var userInVariationLogMessage = sprintf ( LOG_MESSAGES . USER_HAS_VARIATION , MODULE_NAME , bucketerParams . userId , variationKey , bucketerParams . experimentKey ) ; bucketerParams . logger . log ( LOG_LEVEL . INFO , userInVariationLogMessage ) ; } return entityId ; }
Determines ID of variation to be shown for the given input params
@param  {Object}         bucketerParams
@param  {string}         bucketerParams.experimentId
@param  {string}         bucketerParams.experimentKey
@param  {string}         bucketerParams.userId
@param  {Object[]}       bucketerParams.trafficAllocationConfig
@param  {Array}          bucketerParams.experimentKeyMap
@param  {Object}         bucketerParams.groupIdMap
@param  {Object}         bucketerParams.variationIdMap
@param  {string}         bucketerParams.varationIdMap[].key
@param  {Object}         bucketerParams.logger
@param  {string}         bucketerParams.bucketingId
@return Variation ID that user has been bucketed into, null if user is not bucketed into any experiment
Determines ID of variation to be shown for the given input params