Skip to content

Commit 74ce2fe

Browse files
mikeproeng37Tyler Brandt
authored and
Tyler Brandt
committed
Fix impression sent from feature experiment variation toggled off. (#117)
1 parent aa6f4e0 commit 74ce2fe

File tree

2 files changed

+66
-7
lines changed

2 files changed

+66
-7
lines changed

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

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -494,16 +494,18 @@ Optimizely.prototype.isFeatureEnabled = function(featureKey, userId, attributes)
494494

495495
var decision = this.decisionService.getVariationForFeature(feature, userId, attributes);
496496
var variation = decision.variation;
497-
if (!!variation && variation.featureEnabled === true) {
498-
this.logger.log(LOG_LEVEL.INFO, sprintf(LOG_MESSAGES.FEATURE_ENABLED_FOR_USER, MODULE_NAME, featureKey, userId));
497+
if (!!variation) {
499498
if (decision.decisionSource === DECISION_SOURCES.EXPERIMENT) {
499+
// got a variation from the exp, so we track the impression
500500
this._sendImpressionEvent(decision.experiment.key, decision.variation.key, userId, attributes);
501501
}
502-
return true;
503-
} else {
504-
this.logger.log(LOG_LEVEL.INFO, sprintf(LOG_MESSAGES.FEATURE_NOT_ENABLED_FOR_USER, MODULE_NAME, featureKey, userId));
505-
return false;
502+
if (variation.featureEnabled === true) {
503+
this.logger.log(LOG_LEVEL.INFO, sprintf(LOG_MESSAGES.FEATURE_ENABLED_FOR_USER, MODULE_NAME, featureKey, userId));
504+
return true;
505+
}
506506
}
507+
this.logger.log(LOG_LEVEL.INFO, sprintf(LOG_MESSAGES.FEATURE_NOT_ENABLED_FOR_USER, MODULE_NAME, featureKey, userId));
508+
return false;
507509
};
508510

509511
/**

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

Lines changed: 58 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2654,6 +2654,7 @@ describe('lib/optimizely', function() {
26542654
});
26552655

26562656
describe('when the variation is toggled OFF', function() {
2657+
var result;
26572658
beforeEach(function() {
26582659
var experiment = optlyInstance.configObj.experimentKeyMap.test_shared_feature;
26592660
var variation = experiment.variations[1];
@@ -2662,10 +2663,10 @@ describe('lib/optimizely', function() {
26622663
variation: variation,
26632664
decisionSource: DECISION_SOURCES.EXPERIMENT,
26642665
});
2666+
result = optlyInstance.isFeatureEnabled('shared_feature', 'user1', attributes);
26652667
});
26662668

26672669
it('should return false', function() {
2668-
var result = optlyInstance.isFeatureEnabled('shared_feature', 'user1', attributes);
26692670
assert.strictEqual(result, false);
26702671
sinon.assert.calledOnce(optlyInstance.decisionService.getVariationForFeature);
26712672
var feature = optlyInstance.configObj.featureKeyMap.shared_feature;
@@ -2676,6 +2677,62 @@ describe('lib/optimizely', function() {
26762677
attributes
26772678
);
26782679
});
2680+
2681+
it('should dispatch an impression event', function() {
2682+
sinon.assert.calledOnce(eventDispatcher.dispatchEvent);
2683+
var expectedImpressionEvent = {
2684+
'httpVerb': 'POST',
2685+
'url': 'https://logx.optimizely.com/v1/events',
2686+
'params': {
2687+
'account_id': '572018',
2688+
'project_id': '594001',
2689+
'visitors': [
2690+
{
2691+
'snapshots': [
2692+
{
2693+
'decisions': [
2694+
{
2695+
'campaign_id': '599023',
2696+
'experiment_id': '599028',
2697+
'variation_id': '599027'
2698+
}
2699+
],
2700+
'events': [
2701+
{
2702+
'entity_id': '599023',
2703+
'timestamp': 1509489766569,
2704+
'key': 'campaign_activated',
2705+
'uuid': 'a68cf1ad-0393-4e18-af87-efe8f01a7c9c'
2706+
}
2707+
]
2708+
}
2709+
],
2710+
'visitor_id': 'user1',
2711+
'attributes': [
2712+
{
2713+
'entity_id': '594014',
2714+
'key': 'test_attribute',
2715+
'type': 'custom',
2716+
'value': 'test_value',
2717+
}, {
2718+
'entity_id': '$opt_bot_filtering',
2719+
'key': '$opt_bot_filtering',
2720+
'type': 'custom',
2721+
'value': true,
2722+
},
2723+
],
2724+
}
2725+
],
2726+
'revision': '35',
2727+
'client_name': 'node-sdk',
2728+
'client_version': enums.NODE_CLIENT_VERSION,
2729+
'anonymize_ip': true
2730+
}
2731+
};
2732+
var callArgs = eventDispatcher.dispatchEvent.getCalls()[0].args;
2733+
assert.deepEqual(callArgs[0], expectedImpressionEvent);
2734+
assert.isFunction(callArgs[1]);
2735+
});
26792736
});
26802737

26812738
describe('when the variation is missing the toggle', function() {

0 commit comments

Comments
 (0)