From 92ad10c54373ca6473766754757c71573e1c8e9f Mon Sep 17 00:00:00 2001 From: Sohail Hussain Date: Mon, 9 Nov 2020 14:59:42 -0800 Subject: [PATCH] added enabled field in metadata. --- .../EventTests/EventFactoryTest.cs | 25 +++++++++++++------ .../Event/Entity/DecisionMetadata.cs | 5 +++- OptimizelySDK/Event/UserEventFactory.cs | 10 +++++--- OptimizelySDK/Optimizely.cs | 13 +++++----- 4 files changed, 34 insertions(+), 19 deletions(-) diff --git a/OptimizelySDK.Tests/EventTests/EventFactoryTest.cs b/OptimizelySDK.Tests/EventTests/EventFactoryTest.cs index eb53073c..eea84d6b 100644 --- a/OptimizelySDK.Tests/EventTests/EventFactoryTest.cs +++ b/OptimizelySDK.Tests/EventTests/EventFactoryTest.cs @@ -87,7 +87,8 @@ public void TestCreateImpressionEventNoAttributes() { "rule_type", "experiment" }, { "rule_key", "test_experiment" }, { "flag_key", "test_experiment" }, - { "variation_key", "control" } + { "variation_key", "control" }, + {"enabled", false } } } } } @@ -168,7 +169,9 @@ public void TestCreateImpressionEventWithAttributes() { "rule_type", "experiment" }, { "rule_key", "test_experiment" }, { "flag_key", "test_experiment" }, - { "variation_key", "control" } + { "variation_key", "control" }, + {"enabled", false } + } } } @@ -268,7 +271,8 @@ public void TestCreateImpressionEventWithTypedAttributes() { "rule_type", "experiment" }, { "rule_key", "test_experiment" }, { "flag_key", "test_experiment" }, - { "variation_key", "control" } + { "variation_key", "control" }, + {"enabled", false } } } } @@ -390,7 +394,8 @@ public void TestCreateImpressionEventRemovesInvalidAttributesFromPayload() { "rule_type", "experiment" }, { "rule_key", "test_experiment" }, { "flag_key", "test_experiment" }, - { "variation_key", "control" } + { "variation_key", "control" }, + {"enabled", false } } } } @@ -512,7 +517,8 @@ public void TestCreateImpressionEventRemovesInvalidAttributesFromPayloadRollout( { "rule_type", "rollout" }, { "rule_key", "" }, { "flag_key", "test_feature" }, - { "variation_key", "" } + { "variation_key", "" }, + { "enabled", false } } } } @@ -1526,7 +1532,8 @@ public void TestCreateImpressionEventWithBucketingIDAttribute() { "rule_type", "experiment" }, { "rule_key", "test_experiment" }, { "flag_key", "test_experiment" }, - { "variation_key", "control" } + { "variation_key", "control" }, + {"enabled", false } } } } @@ -1633,7 +1640,8 @@ public void TestCreateImpressionEventWhenBotFilteringIsProvidedInDatafile() { "rule_type", "experiment" }, { "rule_key", "test_experiment" }, { "flag_key", "test_experiment" }, - { "variation_key", "control" } + { "variation_key", "control" }, + {"enabled", false } } } } @@ -1736,7 +1744,8 @@ public void TestCreateImpressionEventWhenBotFilteringIsNotProvidedInDatafile() { "rule_type", "experiment" }, { "rule_key", "test_experiment" }, { "flag_key", "test_experiment" }, - { "variation_key", "control" } + { "variation_key", "control" }, + {"enabled", false } } } } diff --git a/OptimizelySDK/Event/Entity/DecisionMetadata.cs b/OptimizelySDK/Event/Entity/DecisionMetadata.cs index 551eb5a0..9f4b4428 100644 --- a/OptimizelySDK/Event/Entity/DecisionMetadata.cs +++ b/OptimizelySDK/Event/Entity/DecisionMetadata.cs @@ -32,13 +32,16 @@ public class DecisionMetadata public string RuleType { get; private set; } [JsonProperty("variation_key")] public string VariationKey { get; private set; } + [JsonProperty("enabled")] + public bool Enabled { get; private set; } - public DecisionMetadata(string flagKey, string ruleKey, string ruleType, string variationKey = "") + public DecisionMetadata(string flagKey, string ruleKey, string ruleType, string variationKey = "", bool enabled = false) { FlagKey = flagKey; RuleKey = ruleKey; RuleType = ruleType; VariationKey = variationKey; + Enabled = enabled; } } } diff --git a/OptimizelySDK/Event/UserEventFactory.cs b/OptimizelySDK/Event/UserEventFactory.cs index ff78d495..339c8e68 100644 --- a/OptimizelySDK/Event/UserEventFactory.cs +++ b/OptimizelySDK/Event/UserEventFactory.cs @@ -40,10 +40,11 @@ public static ImpressionEvent CreateImpressionEvent(ProjectConfig projectConfig, string userId, UserAttributes userAttributes, string flagKey, - string ruleType) + string ruleType, + bool enabled = false) { Variation variation = projectConfig.GetVariationFromId(activatedExperiment?.Key, variationId); - return CreateImpressionEvent(projectConfig, activatedExperiment, variation, userId, userAttributes, flagKey, ruleType); + return CreateImpressionEvent(projectConfig, activatedExperiment, variation, userId, userAttributes, flagKey, ruleType, enabled); } /// @@ -63,7 +64,8 @@ public static ImpressionEvent CreateImpressionEvent(ProjectConfig projectConfig, string userId, UserAttributes userAttributes, string flagKey, - string ruleType) + string ruleType, + bool enabled = false) { if ((ruleType == FeatureDecision.DECISION_SOURCE_ROLLOUT || variation == null) && !projectConfig.SendFlagDecisions) { @@ -84,7 +86,7 @@ public static ImpressionEvent CreateImpressionEvent(ProjectConfig projectConfig, variationKey = variation.Key; ruleKey = activatedExperiment.Key; } - var metadata = new DecisionMetadata(flagKey, ruleKey, ruleType, variationKey); + var metadata = new DecisionMetadata(flagKey, ruleKey, ruleType, variationKey, enabled); return new ImpressionEvent.Builder() .WithEventContext(eventContext) diff --git a/OptimizelySDK/Optimizely.cs b/OptimizelySDK/Optimizely.cs index cfc1a186..35e2c6c5 100644 --- a/OptimizelySDK/Optimizely.cs +++ b/OptimizelySDK/Optimizely.cs @@ -254,7 +254,7 @@ public Variation Activate(string experimentKey, string userId, UserAttributes us return null; } - SendImpressionEvent(experiment, variation, userId, userAttributes, config, SOURCE_TYPE_EXPERIMENT); + SendImpressionEvent(experiment, variation, userId, userAttributes, config, SOURCE_TYPE_EXPERIMENT, true); return variation; } @@ -475,7 +475,6 @@ public virtual bool IsFeatureEnabled(string featureKey, string userId, UserAttri var variation = decision.Variation; var decisionSource = decision?.Source ?? FeatureDecision.DECISION_SOURCE_ROLLOUT; - SendImpressionEvent(decision.Experiment, variation, userId, userAttributes, config, featureKey, decisionSource); if (variation != null) { @@ -507,6 +506,8 @@ public virtual bool IsFeatureEnabled(string featureKey, string userId, UserAttri { "sourceInfo", sourceInfo }, }; + SendImpressionEvent(decision.Experiment, variation, userId, userAttributes, config, featureKey, decisionSource, featureEnabled); + NotificationCenter.SendNotifications(NotificationCenter.NotificationType.Decision, DecisionNotificationTypes.FEATURE, userId, userAttributes ?? new UserAttributes(), decisionInfo); return featureEnabled; @@ -692,9 +693,9 @@ public OptimizelyJSON GetFeatureVariableJSON(string featureKey, string variableK /// It can either be experiment in case impression event is sent from activate or it's feature-test or rollout private void SendImpressionEvent(Experiment experiment, Variation variation, string userId, UserAttributes userAttributes, ProjectConfig config, - string ruleType) + string ruleType, bool enabled) { - SendImpressionEvent(experiment, variation, userId, userAttributes, config, "", ruleType); + SendImpressionEvent(experiment, variation, userId, userAttributes, config, "", ruleType, enabled); } /// @@ -708,14 +709,14 @@ private void SendImpressionEvent(Experiment experiment, Variation variation, str /// It can either be experiment in case impression event is sent from activate or it's feature-test or rollout private void SendImpressionEvent(Experiment experiment, Variation variation, string userId, UserAttributes userAttributes, ProjectConfig config, - string flagKey, string ruleType) + string flagKey, string ruleType, bool enabled) { if (experiment != null && !experiment.IsExperimentRunning) { Logger.Log(LogLevel.ERROR, @"Experiment has ""Launched"" status so not dispatching event during activation."); } - var userEvent = UserEventFactory.CreateImpressionEvent(config, experiment, variation, userId, userAttributes, flagKey, ruleType); + var userEvent = UserEventFactory.CreateImpressionEvent(config, experiment, variation, userId, userAttributes, flagKey, ruleType, enabled); if (userEvent == null) { return;