diff --git a/packages/optimizely-sdk/lib/optimizely/index.js b/packages/optimizely-sdk/lib/optimizely/index.js index a376f1ee7..44c00dcf5 100644 --- a/packages/optimizely-sdk/lib/optimizely/index.js +++ b/packages/optimizely-sdk/lib/optimizely/index.js @@ -495,16 +495,18 @@ Optimizely.prototype.isFeatureEnabled = function(featureKey, userId, attributes) var decision = this.decisionService.getVariationForFeature(feature, userId, attributes); var variation = decision.variation; - if (!!variation && variation.featureEnabled === true) { - this.logger.log(LOG_LEVEL.INFO, sprintf(LOG_MESSAGES.FEATURE_ENABLED_FOR_USER, MODULE_NAME, featureKey, userId)); + if (!!variation) { if (decision.decisionSource === DECISION_SOURCES.EXPERIMENT) { + // got a variation from the exp, so we track the impression this._sendImpressionEvent(decision.experiment.key, decision.variation.key, userId, attributes); } - return true; - } else { - this.logger.log(LOG_LEVEL.INFO, sprintf(LOG_MESSAGES.FEATURE_NOT_ENABLED_FOR_USER, MODULE_NAME, featureKey, userId)); - return false; + if (variation.featureEnabled === true) { + this.logger.log(LOG_LEVEL.INFO, sprintf(LOG_MESSAGES.FEATURE_ENABLED_FOR_USER, MODULE_NAME, featureKey, userId)); + return true; + } } + this.logger.log(LOG_LEVEL.INFO, sprintf(LOG_MESSAGES.FEATURE_NOT_ENABLED_FOR_USER, MODULE_NAME, featureKey, userId)); + return false; }; /** diff --git a/packages/optimizely-sdk/lib/optimizely/index.tests.js b/packages/optimizely-sdk/lib/optimizely/index.tests.js index de4683acb..c9c167a92 100644 --- a/packages/optimizely-sdk/lib/optimizely/index.tests.js +++ b/packages/optimizely-sdk/lib/optimizely/index.tests.js @@ -2659,6 +2659,7 @@ describe('lib/optimizely', function() { }); describe('when the variation is toggled OFF', function() { + var result; beforeEach(function() { var experiment = optlyInstance.configObj.experimentKeyMap.test_shared_feature; var variation = experiment.variations[1]; @@ -2667,10 +2668,10 @@ describe('lib/optimizely', function() { variation: variation, decisionSource: DECISION_SOURCES.EXPERIMENT, }); + result = optlyInstance.isFeatureEnabled('shared_feature', 'user1', attributes); }); it('should return false', function() { - var result = optlyInstance.isFeatureEnabled('shared_feature', 'user1', attributes); assert.strictEqual(result, false); sinon.assert.calledOnce(optlyInstance.decisionService.getVariationForFeature); var feature = optlyInstance.configObj.featureKeyMap.shared_feature; @@ -2681,6 +2682,62 @@ describe('lib/optimizely', function() { attributes ); }); + + it('should dispatch an impression event', function() { + sinon.assert.calledOnce(eventDispatcher.dispatchEvent); + var expectedImpressionEvent = { + 'httpVerb': 'POST', + 'url': 'https://logx.optimizely.com/v1/events', + 'params': { + 'account_id': '572018', + 'project_id': '594001', + 'visitors': [ + { + 'snapshots': [ + { + 'decisions': [ + { + 'campaign_id': '599023', + 'experiment_id': '599028', + 'variation_id': '599027' + } + ], + 'events': [ + { + 'entity_id': '599023', + 'timestamp': 1509489766569, + 'key': 'campaign_activated', + 'uuid': 'a68cf1ad-0393-4e18-af87-efe8f01a7c9c' + } + ] + } + ], + 'visitor_id': 'user1', + 'attributes': [ + { + 'entity_id': '594014', + 'key': 'test_attribute', + 'type': 'custom', + 'value': 'test_value', + }, { + 'entity_id': '$opt_bot_filtering', + 'key': '$opt_bot_filtering', + 'type': 'custom', + 'value': true, + }, + ], + } + ], + 'revision': '35', + 'client_name': 'node-sdk', + 'client_version': enums.NODE_CLIENT_VERSION, + 'anonymize_ip': true + } + }; + var callArgs = eventDispatcher.dispatchEvent.getCalls()[0].args; + assert.deepEqual(callArgs[0], expectedImpressionEvent); + assert.isFunction(callArgs[1]); + }); }); describe('when the variation is missing the toggle', function() {