Skip to content

Commit 878fc64

Browse files
authored
feat: added "enabled" field to decision metadata structure (#217)
1 parent 04f5300 commit 878fc64

File tree

6 files changed

+45
-27
lines changed

6 files changed

+45
-27
lines changed

src/Optimizely/Event/Builder/EventBuilder.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ private function getCommonParams($config, $userId, $attributes)
143143
*
144144
* @return array Hash representing parameters particular to impression event.
145145
*/
146-
private function getImpressionParams(Experiment $experiment, $variation, $flagKey, $ruleKey, $ruleType)
146+
private function getImpressionParams(Experiment $experiment, $variation, $flagKey, $ruleKey, $ruleType, $enabled)
147147
{
148148
$variationKey = $variation->getKey() ? $variation->getKey() : '';
149149
$impressionParams = [
@@ -156,7 +156,8 @@ private function getImpressionParams(Experiment $experiment, $variation, $flagKe
156156
FLAG_KEY => $flagKey,
157157
RULE_KEY => $ruleKey,
158158
RULE_TYPE => $ruleType,
159-
VARIATION_KEY => $variationKey
159+
VARIATION_KEY => $variationKey,
160+
ENABLED => $enabled
160161
],
161162
]
162163
],
@@ -228,13 +229,13 @@ private function getConversionParams($eventEntity, $eventTags)
228229
*
229230
* @return LogEvent Event object to be sent to dispatcher.
230231
*/
231-
public function createImpressionEvent($config, $experimentKey, $variationKey, $flagKey, $ruleKey, $ruleType, $userId, $attributes)
232+
public function createImpressionEvent($config, $experimentKey, $variationKey, $flagKey, $ruleKey, $ruleType, $enabled, $userId, $attributes)
232233
{
233234
$eventParams = $this->getCommonParams($config, $userId, $attributes);
234235

235236
$experiment = $config->getExperimentFromKey($experimentKey);
236237
$variation = $config->getVariationFromKey($experimentKey, $variationKey);
237-
$impressionParams = $this->getImpressionParams($experiment, $variation, $flagKey, $ruleKey, $ruleType);
238+
$impressionParams = $this->getImpressionParams($experiment, $variation, $flagKey, $ruleKey, $ruleType, $enabled);
238239

239240
$eventParams[VISITORS][0][SNAPSHOTS][] = $impressionParams;
240241

src/Optimizely/Event/Builder/Params.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,3 +44,4 @@
4444
define('RULE_KEY', 'rule_key');
4545
define('RULE_TYPE', 'rule_type');
4646
define('VARIATION_KEY', 'variation_key');
47+
define('ENABLED', 'enabled');

src/Optimizely/Optimizely.php

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -195,10 +195,10 @@ private function validateUserInputs($attributes, $eventTags = null)
195195
* @param array Associative array of user attributes
196196
* @param DatafileProjectConfig DatafileProjectConfig instance
197197
*/
198-
protected function sendImpressionEvent($config, $experimentKey, $variationKey, $flagKey, $ruleKey, $ruleType, $userId, $attributes)
198+
protected function sendImpressionEvent($config, $experimentKey, $variationKey, $flagKey, $ruleKey, $ruleType, $enabled, $userId, $attributes)
199199
{
200200
$impressionEvent = $this->_eventBuilder
201-
->createImpressionEvent($config, $experimentKey, $variationKey, $flagKey, $ruleKey, $ruleType, $userId, $attributes);
201+
->createImpressionEvent($config, $experimentKey, $variationKey, $flagKey, $ruleKey, $ruleType, $enabled, $userId, $attributes);
202202
$this->_logger->log(Logger::INFO, sprintf('Activating user "%s" in experiment "%s".', $userId, $experimentKey));
203203
$this->_logger->log(
204204
Logger::DEBUG,
@@ -274,7 +274,7 @@ public function activate($experimentKey, $userId, $attributes = null)
274274
return null;
275275
}
276276

277-
$this->sendImpressionEvent($config, $experimentKey, $variationKey, '', $experimentKey, FeatureDecision::DECITION_SOURCE_EXPERIMENT, $userId, $attributes);
277+
$this->sendImpressionEvent($config, $experimentKey, $variationKey, '', $experimentKey, FeatureDecision::DECITION_SOURCE_EXPERIMENT, true, $userId, $attributes);
278278

279279
return $variationKey;
280280
}
@@ -556,8 +556,11 @@ public function isFeatureEnabled($featureFlagKey, $userId, $attributes = null)
556556
$variation = $decision->getVariation();
557557

558558
if ($config->getSendFlagDecisions() && ($decision->getSource() == FeatureDecision::DECISION_SOURCE_ROLLOUT || !$variation)) {
559+
if ($variation) {
560+
$featureEnabled = $variation->getFeatureEnabled();
561+
}
559562
$ruleKey = $decision->getExperiment() ? $decision->getExperiment()->getKey() : '';
560-
$this->sendImpressionEvent($config, $ruleKey, $variation ? $variation->getKey() : '', $featureFlagKey, $ruleKey, $decision->getSource(), $userId, $attributes);
563+
$this->sendImpressionEvent($config, $ruleKey, $variation ? $variation->getKey() : '', $featureFlagKey, $ruleKey, $decision->getSource(), $featureEnabled, $userId, $attributes);
561564
}
562565

563566
if ($variation) {
@@ -569,7 +572,7 @@ public function isFeatureEnabled($featureFlagKey, $userId, $attributes = null)
569572
'variationKey'=> $variation->getKey()
570573
);
571574

572-
$this->sendImpressionEvent($config, $experimentKey, $variation->getKey(), $featureFlagKey, $experimentKey, $decision->getSource(), $userId, $attributes);
575+
$this->sendImpressionEvent($config, $experimentKey, $variation->getKey(), $featureFlagKey, $experimentKey, $decision->getSource(), $featureEnabled, $userId, $attributes);
573576
} else {
574577
$this->_logger->log(Logger::INFO, "The user '{$userId}' is not being experimented on Feature Flag '{$featureFlagKey}'.");
575578
}

tests/EventTests/EventBuilderTest.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,8 @@ public function setUp()
8181
'flag_key' => 'test_experiment',
8282
'rule_key' => 'test_experiment',
8383
'rule_type' => 'experiment',
84-
'variation_key'=> 'variation'
84+
'variation_key'=> 'variation',
85+
'enabled' => true
8586
]
8687
]]
8788
);
@@ -146,6 +147,7 @@ public function testCreateImpressionEventNoAttributesNoValue()
146147
'test_experiment',
147148
'test_experiment',
148149
'experiment',
150+
true,
149151
$this->testUserId,
150152
null
151153
);
@@ -205,6 +207,7 @@ public function testCreateImpressionEventWithAttributesNoValue()
205207
'test_experiment',
206208
'test_experiment',
207209
'experiment',
210+
true,
208211
$this->testUserId,
209212
$userAttributes
210213
);
@@ -245,6 +248,7 @@ public function testCreateImpressionEventWithFalseAttributesNoValue()
245248
'test_experiment',
246249
'test_experiment',
247250
'experiment',
251+
true,
248252
$this->testUserId,
249253
$userAttributes
250254
);
@@ -286,6 +290,7 @@ public function testCreateImpressionEventWithZeroAttributesNoValue()
286290
'test_experiment',
287291
'test_experiment',
288292
'experiment',
293+
true,
289294
$this->testUserId,
290295
$userAttributes
291296
);
@@ -317,6 +322,7 @@ public function testCreateImpressionEventWithInvalidAttributesNoValue()
317322
'test_experiment',
318323
'test_experiment',
319324
'experiment',
325+
true,
320326
$this->testUserId,
321327
$userAttributes
322328
);
@@ -357,6 +363,7 @@ public function testCreateImpressionEventWithUserAgentWhenBotFilteringIsEnabled(
357363
'test_experiment',
358364
'test_experiment',
359365
'experiment',
366+
true,
360367
$this->testUserId,
361368
$userAttributes
362369
);
@@ -404,6 +411,7 @@ public function testCreateImpressionEventWithInvalidAttributeTypes()
404411
'test_experiment',
405412
'test_experiment',
406413
'experiment',
414+
true,
407415
$this->testUserId,
408416
$userAttributes
409417
);
@@ -454,6 +462,7 @@ public function testCreateImpressionEventWithUserAgentWhenBotFilteringIsDisabled
454462
'test_experiment',
455463
'test_experiment',
456464
'experiment',
465+
true,
457466
$this->testUserId,
458467
$userAttributes
459468
);
@@ -500,6 +509,7 @@ public function testCreateImpressionEventWithUserAgentWhenBotFilteringIsNull()
500509
'test_experiment',
501510
'test_experiment',
502511
'experiment',
512+
true,
503513
$this->testUserId,
504514
$userAttributes
505515
);
@@ -829,6 +839,7 @@ public function testCreateImpressionEventWithBucketingIDAttribute()
829839
'test_experiment',
830840
'test_experiment',
831841
'experiment',
842+
true,
832843
$this->testUserId,
833844
$userAttributes
834845
);

tests/OptimizelyTest.php

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -392,7 +392,7 @@ public function testActivateWithEmptyUserID()
392392
// Verify that sendImpressionEvent is called with expected attributes
393393
$optimizelyMock->expects($this->exactly(1))
394394
->method('sendImpressionEvent')
395-
->with($this->projectConfig, 'test_experiment', 'variation', '', 'test_experiment', 'experiment', '', $userAttributes);
395+
->with($this->projectConfig, 'test_experiment', 'variation', '', 'test_experiment', 'experiment', true, '', $userAttributes);
396396

397397
// Call activate
398398
$this->assertEquals('variation', $optimizelyMock->activate('test_experiment', '', $userAttributes));
@@ -487,7 +487,7 @@ public function testActivateNoAudienceNoAttributes()
487487
// Verify that sendImpression is called with expected params
488488
$optimizelyMock->expects($this->exactly(1))
489489
->method('sendImpressionEvent')
490-
->with($this->projectConfig, 'group_experiment_1', 'group_exp_1_var_2', '', 'group_experiment_1', 'experiment', 'user_1', null);
490+
->with($this->projectConfig, 'group_experiment_1', 'group_exp_1_var_2', '', 'group_experiment_1', 'experiment', true, 'user_1', null);
491491

492492
// Call activate
493493
$this->assertSame('group_exp_1_var_2', $optimizelyMock->activate('group_experiment_1', 'user_1'));
@@ -520,7 +520,7 @@ public function testActivateNoAudienceNoAttributesAfterSetForcedVariation()
520520
// Verify that sendImpression is called with expected params
521521
$optimizelyMock->expects($this->exactly(1))
522522
->method('sendImpressionEvent')
523-
->with($this->projectConfig, 'group_experiment_1', 'group_exp_1_var_2', '', 'group_experiment_1', 'experiment', 'user_1', null);
523+
->with($this->projectConfig, 'group_experiment_1', 'group_exp_1_var_2', '', 'group_experiment_1', 'experiment', true, 'user_1', null);
524524

525525
// set forced variation
526526
$this->assertTrue($optimizelyMock->setForcedVariation($experimentKey, $userId, $variationKey), 'Set variation for paused experiment should have failed.');
@@ -581,7 +581,7 @@ public function testActivateWithAttributes()
581581
// Verify that sendImpressionEvent is called with expected attributes
582582
$optimizelyMock->expects($this->exactly(1))
583583
->method('sendImpressionEvent')
584-
->with($this->projectConfig, 'test_experiment', 'control', '', 'test_experiment', 'experiment', 'test_user', $userAttributes);
584+
->with($this->projectConfig, 'test_experiment', 'control', '', 'test_experiment', 'experiment', true, 'test_user', $userAttributes);
585585

586586
// Call activate
587587
$this->assertEquals('control', $optimizelyMock->activate('test_experiment', 'test_user', $userAttributes));
@@ -614,7 +614,7 @@ public function testActivateWithAttributesOfDifferentTypes()
614614
// Verify that sendImpressionEvent is called with expected attributes
615615
$optimizelyMock->expects($this->exactly(1))
616616
->method('sendImpressionEvent')
617-
->with($this->projectConfig, 'test_experiment', 'control', '', 'test_experiment', 'experiment', 'test_user', $userAttributes);
617+
->with($this->projectConfig, 'test_experiment', 'control', '', 'test_experiment', 'experiment', true, 'test_user', $userAttributes);
618618

619619
// Call activate
620620
$this->assertEquals('control', $optimizelyMock->activate('test_experiment', 'test_user', $userAttributes));
@@ -639,7 +639,7 @@ public function testActivateWithAttributesTypedAudienceMatch()
639639
// Verify that sendImpressionEvent is called with expected attributes
640640
$optimizelyMock->expects($this->at(0))
641641
->method('sendImpressionEvent')
642-
->with($this->projectConfigForTypedAudience, 'typed_audience_experiment', 'A', '', 'typed_audience_experiment', 'experiment', 'test_user', $userAttributes);
642+
->with($this->projectConfigForTypedAudience, 'typed_audience_experiment', 'A', '', 'typed_audience_experiment', 'experiment', true, 'test_user', $userAttributes);
643643

644644
// Should be included via exact match string audience with id '3468206642'
645645
$this->assertEquals('A', $optimizelyMock->activate('typed_audience_experiment', 'test_user', $userAttributes));
@@ -651,7 +651,7 @@ public function testActivateWithAttributesTypedAudienceMatch()
651651
// Verify that sendImpressionEvent is called with expected attributes
652652
$optimizelyMock->expects($this->at(0))
653653
->method('sendImpressionEvent')
654-
->with($this->projectConfigForTypedAudience, 'typed_audience_experiment', 'A', '', 'typed_audience_experiment', 'experiment', 'test_user', $userAttributes);
654+
->with($this->projectConfigForTypedAudience, 'typed_audience_experiment', 'A', '', 'typed_audience_experiment', 'experiment', true, 'test_user', $userAttributes);
655655

656656
//Should be included via exact match number audience with id '3468206646'
657657
$this->assertEquals('A', $optimizelyMock->activate('typed_audience_experiment', 'test_user', $userAttributes));
@@ -691,7 +691,7 @@ public function testActivateWithAttributesComplexAudienceMatch()
691691
// Verify that sendImpressionEvent is called once with expected attributes
692692
$optimizelyMock->expects($this->exactly(1))
693693
->method('sendImpressionEvent')
694-
->with($this->projectConfigForTypedAudience, 'audience_combinations_experiment', 'A', '', 'audience_combinations_experiment', 'experiment', 'test_user', $userAttributes);
694+
->with($this->projectConfigForTypedAudience, 'audience_combinations_experiment', 'A', '', 'audience_combinations_experiment', 'experiment', true, 'test_user', $userAttributes);
695695

696696
// Should be included via substring match string audience with id '3988293898', and
697697
// exact match number audience with id '3468206646'
@@ -2554,7 +2554,7 @@ public function testIsFeatureEnabledGivenFeatureExperimentAndFeatureEnabledIsTru
25542554
// assert that sendImpressionEvent is called with expected params
25552555
$optimizelyMock->expects($this->exactly(1))
25562556
->method('sendImpressionEvent')
2557-
->with($this->projectConfig, 'test_experiment_double_feature', 'control', 'double_single_variable_feature', 'test_experiment_double_feature', FeatureDecision::DECISION_SOURCE_FEATURE_TEST, 'user_id', []);
2557+
->with($this->projectConfig, 'test_experiment_double_feature', 'control', 'double_single_variable_feature', 'test_experiment_double_feature', FeatureDecision::DECISION_SOURCE_FEATURE_TEST, true, 'user_id', []);
25582558

25592559
$this->loggerMock->expects($this->at(0))
25602560
->method('log')
@@ -2659,7 +2659,7 @@ public function testIsFeatureEnabledGivenFeatureExperimentAndFeatureEnabledIsFal
26592659

26602660
$optimizelyMock->expects($this->exactly(1))
26612661
->method('sendImpressionEvent')
2662-
->with($this->projectConfig, 'test_experiment_double_feature', 'variation', 'double_single_variable_feature', 'test_experiment_double_feature', FeatureDecision::DECISION_SOURCE_FEATURE_TEST, 'user_id', []);
2662+
->with($this->projectConfig, 'test_experiment_double_feature', 'variation', 'double_single_variable_feature', 'test_experiment_double_feature', FeatureDecision::DECISION_SOURCE_FEATURE_TEST, false, 'user_id', []);
26632663

26642664
$this->loggerMock->expects($this->at(0))
26652665
->method('log')
@@ -3042,7 +3042,7 @@ public function testIsFeatureEnabledWithEmptyUserID()
30423042
// assert that sendImpressionEvent is called with expected params
30433043
$optimizelyMock->expects($this->exactly(1))
30443044
->method('sendImpressionEvent')
3045-
->with($this->projectConfig, 'test_experiment_double_feature', 'control', 'double_single_variable_feature', 'test_experiment_double_feature', FeatureDecision::DECISION_SOURCE_FEATURE_TEST, '', []);
3045+
->with($this->projectConfig, 'test_experiment_double_feature', 'control', 'double_single_variable_feature', 'test_experiment_double_feature', FeatureDecision::DECISION_SOURCE_FEATURE_TEST, true, '', []);
30463046

30473047
$this->loggerMock->expects($this->at(0))
30483048
->method('log')
@@ -4682,6 +4682,7 @@ public function testSendImpressionEventWithNoAttributes()
46824682
'group_experiment_1',
46834683
'group_experiment_1',
46844684
'experiment',
4685+
true,
46854686
'user_1',
46864687
null
46874688
)
@@ -4734,7 +4735,7 @@ public function testSendImpressionEventWithNoAttributes()
47344735
'Dispatching impression event to URL logx.optimizely.com/decision with params {"param1":"val1","param2":"val2"}.'
47354736
);
47364737

4737-
$optlyObject->sendImpressionEvent($this->projectConfig, 'group_experiment_1', 'group_exp_1_var_2', 'group_experiment_1', 'group_experiment_1', 'experiment', 'user_1', null);
4738+
$optlyObject->sendImpressionEvent($this->projectConfig, 'group_experiment_1', 'group_exp_1_var_2', 'group_experiment_1', 'group_experiment_1', 'experiment', true, 'user_1', null);
47384739
}
47394740

47404741
public function testSendImpressionEventDispatchFailure()
@@ -4757,7 +4758,7 @@ public function testSendImpressionEventDispatchFailure()
47574758
->method('log')
47584759
->with(Logger::ERROR, 'Unable to dispatch impression event. Error ');
47594760

4760-
$optlyObject->sendImpressionEvent($this->projectConfig, 'test_experiment', 'control', 'test_experiment', 'test_experiment', 'experiment', 'test_user', []);
4761+
$optlyObject->sendImpressionEvent($this->projectConfig, 'test_experiment', 'control', 'test_experiment', 'test_experiment', 'experiment', true, 'test_user', []);
47614762
}
47624763

47634764
public function testSendImpressionEventWithAttributes()
@@ -4780,6 +4781,7 @@ public function testSendImpressionEventWithAttributes()
47804781
'test_experiment',
47814782
'test_experiment',
47824783
'experiment',
4784+
true,
47834785
'test_user',
47844786
$userAttributes
47854787
)
@@ -4822,7 +4824,7 @@ public function testSendImpressionEventWithAttributes()
48224824

48234825
$optlyObject->notificationCenter = $this->notificationCenterMock;
48244826

4825-
$optlyObject->sendImpressionEvent($this->projectConfig, 'test_experiment', 'control', 'test_experiment', 'test_experiment', 'experiment', 'test_user', $userAttributes);
4827+
$optlyObject->sendImpressionEvent($this->projectConfig, 'test_experiment', 'control', 'test_experiment', 'test_experiment', 'experiment', true, 'test_user', $userAttributes);
48264828
}
48274829

48284830
/*
@@ -4998,7 +5000,7 @@ public function testRolloutSendImpressionWhenSendFlagDecisionFlagInDatafile()
49985000
// Verify that sendImpressionEvent is called with expected attributes
49995001
$optimizelyMock->expects($this->exactly(1))
50005002
->method('sendImpressionEvent')
5001-
->with($this->anything(), 'rollout_1_exp_1', '177771', 'boolean_single_variable_feature', 'rollout_1_exp_1', 'rollout', 'user_id', []);
5003+
->with($this->anything(), 'rollout_1_exp_1', '177771', 'boolean_single_variable_feature', 'rollout_1_exp_1', 'rollout', true, 'user_id', []);
50025004

50035005
$this->assertTrue($optimizelyMock->isFeatureEnabled('boolean_single_variable_feature', 'user_id', []));
50045006
}

0 commit comments

Comments
 (0)