Skip to content

Commit 3c69a02

Browse files
mfahadahmedjordangarcia
authored andcommitted
feat(api): Decision Notification Listener for feature variable APIs. (#246)
1 parent 9827911 commit 3c69a02

File tree

5 files changed

+590
-29
lines changed

5 files changed

+590
-29
lines changed

packages/optimizely-sdk/lib/core/decision_service/index.tests.js

+46
Original file line numberDiff line numberDiff line change
@@ -743,6 +743,29 @@ describe('lib/core/decision_service', function() {
743743
],
744744
'featureEnabled': true,
745745
'key': 'control'
746+
},
747+
{
748+
'id': '594099',
749+
'variables': [
750+
{
751+
'id': '4792309476491264',
752+
'value': '40'
753+
},
754+
{
755+
'id': '5073784453201920',
756+
'value': 'true'
757+
},
758+
{
759+
'id': '5636734406623232',
760+
'value': 'Buy me Later'
761+
},
762+
{
763+
'id': '6199684360044544',
764+
'value': '99.99'
765+
}
766+
],
767+
'featureEnabled': false,
768+
'key': 'variation2'
746769
}
747770
],
748771
'audienceIds': [],
@@ -798,6 +821,29 @@ describe('lib/core/decision_service', function() {
798821
'featureEnabled': true,
799822
'key': 'variation'
800823
},
824+
variation2: {
825+
'id': '594099',
826+
'variables': [
827+
{
828+
'id': '4792309476491264',
829+
'value': '40'
830+
},
831+
{
832+
'id': '5073784453201920',
833+
'value': 'true'
834+
},
835+
{
836+
'id': '5636734406623232',
837+
'value': 'Buy me Later'
838+
},
839+
{
840+
'id': '6199684360044544',
841+
'value': '99.99'
842+
}
843+
],
844+
'featureEnabled': false,
845+
'key': 'variation2'
846+
}
801847
},
802848
},
803849
variation: {

packages/optimizely-sdk/lib/optimizely/index.js

+34-23
Original file line numberDiff line numberDiff line change
@@ -632,35 +632,46 @@ Optimizely.prototype._getFeatureVariableForType = function(featureKey, variableK
632632
return null;
633633
}
634634

635-
var decision = this.decisionService.getVariationForFeature(this.configObj, featureFlag, userId, attributes);
636635
var variableValue;
636+
var featureEnabled = false;
637+
var decision = this.decisionService.getVariationForFeature(this.configObj, featureFlag, userId, attributes);
638+
637639
if (decision.variation !== null) {
638-
variableValue = projectConfig.getVariableValueForVariation(
639-
this.configObj,
640-
variable,
641-
decision.variation,
642-
this.logger
643-
);
644-
this.logger.log(
645-
LOG_LEVEL.INFO,
646-
sprintf(
647-
LOG_MESSAGES.USER_RECEIVED_VARIABLE_VALUE,
648-
MODULE_NAME,
649-
variableKey,
650-
featureFlag.key,
651-
variableValue,
652-
userId
653-
)
654-
);
640+
featureEnabled = decision.variation.featureEnabled;
641+
variableValue = projectConfig.getVariableValueForVariation(this.configObj, variable, decision.variation, this.logger);
642+
this.logger.log(LOG_LEVEL.INFO, sprintf(LOG_MESSAGES.USER_RECEIVED_VARIABLE_VALUE, MODULE_NAME, variableKey, featureFlag.key, variableValue, userId));
655643
} else {
656644
variableValue = variable.defaultValue;
657-
this.logger.log(
658-
LOG_LEVEL.INFO,
659-
sprintf(LOG_MESSAGES.USER_RECEIVED_DEFAULT_VARIABLE_VALUE, MODULE_NAME, userId, variableKey, featureFlag.key)
660-
);
645+
this.logger.log(LOG_LEVEL.INFO, sprintf(LOG_MESSAGES.USER_RECEIVED_DEFAULT_VARIABLE_VALUE, MODULE_NAME, userId, variableKey, featureFlag.key));
661646
}
662647

663-
return projectConfig.getTypeCastValue(variableValue, variableType, this.logger);
648+
var experimentKey = null;
649+
var variationKey = null;
650+
if (decision.decisionSource === DECISION_SOURCES.EXPERIMENT) {
651+
experimentKey = decision.experiment.key;
652+
variationKey = decision.variation.key;
653+
}
654+
655+
var typeCastedValue = projectConfig.getTypeCastValue(variableValue, variableType, this.logger);
656+
this.notificationCenter.sendNotifications(
657+
enums.NOTIFICATION_TYPES.DECISION,
658+
{
659+
type: DECISION_INFO_TYPES.FEATURE_VARIABLE,
660+
userId: userId,
661+
attributes: attributes || {},
662+
decisionInfo: {
663+
featureKey: featureKey,
664+
featureEnabled: featureEnabled,
665+
variableKey: variableKey,
666+
variableValue: typeCastedValue,
667+
variableType: variableType,
668+
source: decision.decisionSource,
669+
sourceExperimentKey: experimentKey,
670+
sourceVariationKey: variationKey
671+
}
672+
}
673+
);
674+
return typeCastedValue;
664675
};
665676

666677
/**

0 commit comments

Comments
 (0)