16
16
import { sprintf } from '@optimizely/js-sdk-utils' ;
17
17
18
18
import fns from '../../utils/fns' ;
19
- import bucketer from '../bucketer' ;
19
+ import * as bucketer from '../bucketer' ;
20
20
import * as enums from '../../utils/enums' ;
21
21
import projectConfig from '../project_config' ;
22
22
import AudienceEvaluator from '../audience_evaluator' ;
@@ -469,43 +469,44 @@ DecisionService.prototype.getVariationForFeature = function(configObj, feature,
469
469
470
470
DecisionService . prototype . _getVariationForFeatureExperiment = function ( configObj , feature , userId , attributes , options = { } ) {
471
471
var decideReasons = [ ] ;
472
- var experiment = null ;
473
472
var variationKey = null ;
474
- let decisionVariation ;
475
-
476
- if ( feature . hasOwnProperty ( 'groupId' ) ) {
477
- var group = configObj . groupIdMap [ feature . groupId ] ;
478
- if ( group ) {
479
- experiment = this . _getExperimentInGroup ( configObj , group , userId ) ;
480
- if ( experiment && feature . experimentIds . indexOf ( experiment . id ) !== - 1 ) {
473
+ var decisionVariation ;
474
+ var index ;
475
+ var variationForFeatureExperiment ;
476
+
477
+ // Check if the feature flag is under an experiment and the the user is bucketed into one of these experiments
478
+ if ( feature . experimentIds . length > 0 ) {
479
+ // Evaluate each experiment ID and return the first bucketed experiment variation
480
+ for ( index = 0 ; index < feature . experimentIds . length ; index ++ ) {
481
+ var experiment = projectConfig . getExperimentFromId ( configObj , feature . experimentIds [ index ] , this . logger ) ;
482
+ if ( experiment ) {
481
483
decisionVariation = this . getVariation ( configObj , experiment . key , userId , attributes , options ) ;
482
484
decideReasons . push ( ...decisionVariation . reasons ) ;
483
485
variationKey = decisionVariation . result ;
486
+ if ( variationKey ) {
487
+ var variation = experiment . variationKeyMap [ variationKey ] ;
488
+ variationForFeatureExperiment = {
489
+ experiment : experiment ,
490
+ variation : variation ,
491
+ decisionSource : DECISION_SOURCES . FEATURE_TEST ,
492
+ } ;
493
+
494
+ return {
495
+ result : variationForFeatureExperiment ,
496
+ reasons : decideReasons ,
497
+ }
498
+ }
484
499
}
485
500
}
486
- } else if ( feature . experimentIds . length > 0 ) {
487
- // If the feature does not have a group ID, then it can only be associated
488
- // with one experiment, so we look at the first experiment ID only
489
- experiment = projectConfig . getExperimentFromId ( configObj , feature . experimentIds [ 0 ] , this . logger ) ;
490
- if ( experiment ) {
491
- decisionVariation = this . getVariation ( configObj , experiment . key , userId , attributes , options ) ;
492
- decideReasons . push ( ...decisionVariation . reasons ) ;
493
- variationKey = decisionVariation . result ;
494
- }
495
501
} else {
496
502
var featureHasNoExperimentsMessage = sprintf ( LOG_MESSAGES . FEATURE_HAS_NO_EXPERIMENTS , MODULE_NAME , feature . key ) ;
497
503
this . logger . log ( LOG_LEVEL . DEBUG , featureHasNoExperimentsMessage ) ;
498
504
decideReasons . push ( featureHasNoExperimentsMessage ) ;
499
505
}
500
506
501
- var variation = null ;
502
- if ( variationKey !== null && experiment !== null ) {
503
- variation = experiment . variationKeyMap [ variationKey ] ;
504
- }
505
-
506
- var variationForFeatureExperiment = {
507
- experiment : experiment ,
508
- variation : variation ,
507
+ variationForFeatureExperiment = {
508
+ experiment : null ,
509
+ variation : null ,
509
510
decisionSource : DECISION_SOURCES . FEATURE_TEST ,
510
511
} ;
511
512
@@ -515,26 +516,6 @@ DecisionService.prototype._getVariationForFeatureExperiment = function(configObj
515
516
} ;
516
517
} ;
517
518
518
- DecisionService . prototype . _getExperimentInGroup = function ( configObj , group , userId ) {
519
- var experimentId = bucketer . bucketUserIntoExperiment ( group , userId , userId , this . logger ) ;
520
- if ( experimentId ) {
521
- this . logger . log (
522
- LOG_LEVEL . INFO ,
523
- sprintf ( LOG_MESSAGES . USER_BUCKETED_INTO_EXPERIMENT_IN_GROUP , MODULE_NAME , userId , experimentId , group . id )
524
- ) ;
525
- var experiment = projectConfig . getExperimentFromId ( configObj , experimentId , this . logger ) ;
526
- if ( experiment ) {
527
- return experiment ;
528
- }
529
- }
530
-
531
- this . logger . log (
532
- LOG_LEVEL . INFO ,
533
- sprintf ( LOG_MESSAGES . USER_NOT_BUCKETED_INTO_ANY_EXPERIMENT_IN_GROUP , MODULE_NAME , userId , group . id )
534
- ) ;
535
- return null ;
536
- } ;
537
-
538
519
DecisionService . prototype . _getVariationForRollout = function ( configObj , feature , userId , attributes ) {
539
520
var decideReasons = [ ] ;
540
521
var decisionObj = { } ;
0 commit comments