Skip to content

[FSSDK-11389] update: experiment_id and variation_id added to payloads #361

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion lib/optimizely.rb
Original file line number Diff line number Diff line change
Expand Up @@ -185,12 +185,17 @@ def create_optimizely_decision(user_context, flag_key, decision, reasons, decide
feature_flag = config.get_feature_flag_from_key(flag_key)
experiment = nil
decision_source = Optimizely::DecisionService::DECISION_SOURCES['ROLLOUT']
experiment_id = nil
variation_id = nil

# Send impression event if Decision came from a feature test and decide options doesn't include disableDecisionEvent
if decision.is_a?(Optimizely::DecisionService::Decision)
experiment = decision.experiment
rule_key = experiment ? experiment['key'] : nil
experiment_id = experiment ? experiment['id'] : nil
variation = decision['variation']
variation_key = variation ? variation['key'] : nil
variation_id = variation ? variation['id'] : nil
feature_enabled = variation ? variation['featureEnabled'] : false
decision_source = decision.source
end
Expand Down Expand Up @@ -221,7 +226,9 @@ def create_optimizely_decision(user_context, flag_key, decision, reasons, decide
variation_key: variation_key,
rule_key: rule_key,
reasons: should_include_reasons ? reasons : [],
decision_event_dispatched: decision_event_dispatched
decision_event_dispatched: decision_event_dispatched,
experiment_id: experiment_id,
variation_id: variation_id
)

OptimizelyDecision.new(
Expand Down
4 changes: 2 additions & 2 deletions lib/optimizely/helpers/validator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -122,11 +122,11 @@ def inputs_valid?(variables, logger = NoOpLogger.new, level = Logger::ERROR)

return false unless variables.respond_to?(:each) && !variables.empty?

is_valid = true # rubocop:disable Lint/UselessAssignment
is_valid = true
if variables.include? :user_id
# Empty str is a valid user ID.
unless variables[:user_id].is_a?(String)
is_valid = false # rubocop:disable Lint/UselessAssignment
is_valid = false
logger.log(level, "#{Constants::INPUT_VARIABLES['USER_ID']} is invalid")
end
variables.delete :user_id
Expand Down
12 changes: 9 additions & 3 deletions spec/optimizely_user_context_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,9 @@
variation_key: '3324490562',
rule_key: nil,
reasons: [],
decision_event_dispatched: true
decision_event_dispatched: true,
experiment_id: nil,
variation_id: '3324490562'
)
user_context_obj = forced_decision_project_instance.create_user_context(user_id)
context = Optimizely::OptimizelyUserContext::OptimizelyDecisionContext.new(feature_key, nil)
Expand Down Expand Up @@ -347,7 +349,9 @@
variation_key: 'b',
rule_key: 'exp_with_audience',
reasons: ['Variation (b) is mapped to flag (feature_1), rule (exp_with_audience) and user (tester) in the forced decision map.'],
decision_event_dispatched: true
decision_event_dispatched: true,
experiment_id: '10390977673',
variation_id: '10416523121'
)
user_context_obj = Optimizely::OptimizelyUserContext.new(forced_decision_project_instance, user_id, original_attributes)
context = Optimizely::OptimizelyUserContext::OptimizelyDecisionContext.new(feature_key, 'exp_with_audience')
Expand Down Expand Up @@ -464,7 +468,9 @@
variation_key: '3324490562',
rule_key: nil,
reasons: [],
decision_event_dispatched: true
decision_event_dispatched: true,
experiment_id: nil,
variation_id: '3324490562'
)
user_context_obj = forced_decision_project_instance.create_user_context(user_id)
context_with_flag = Optimizely::OptimizelyUserContext::OptimizelyDecisionContext.new(feature_key, nil)
Expand Down
38 changes: 28 additions & 10 deletions spec/project_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2079,7 +2079,7 @@ def callback(_args); end
it 'should return only enabled feature flags keys' do
# Sets all feature-flags keys with randomly assigned status
features_keys = project_config.feature_flags.map do |item|
{key: (item['key']).to_s, value: [true, false].sample} # '[true, false].sample' generates random boolean
{key: item['key'].to_s, value: [true, false].sample} # '[true, false].sample' generates random boolean
end

enabled_features = features_keys.map { |x| x[:key] if x[:value] == true }.compact
Expand Down Expand Up @@ -3758,7 +3758,9 @@ def callback(_args); end
variation_key: 'Fred',
rule_key: 'test_experiment_multivariate',
reasons: [],
decision_event_dispatched: true
decision_event_dispatched: true,
experiment_id: experiment_to_return['id'],
variation_id: variation_to_return['id']
)
allow(project_instance.event_dispatcher).to receive(:dispatch_event).with(instance_of(Optimizely::Event))
decision_to_return = Optimizely::DecisionService::Decision.new(
Expand Down Expand Up @@ -3801,7 +3803,9 @@ def callback(_args); end
variation_key: 'Fred',
rule_key: 'test_experiment_multivariate',
reasons: [],
decision_event_dispatched: true
decision_event_dispatched: true,
experiment_id: experiment_to_return['id'],
variation_id: variation_to_return['id']
)
allow(project_instance.event_dispatcher).to receive(:dispatch_event).with(instance_of(Optimizely::Event))
decision_to_return = Optimizely::DecisionService::Decision.new(
Expand Down Expand Up @@ -3883,7 +3887,9 @@ def callback(_args); end
variation_key: 'Fred',
rule_key: 'test_experiment_multivariate',
reasons: [],
decision_event_dispatched: false
decision_event_dispatched: false,
experiment_id: experiment_to_return['id'],
variation_id: variation_to_return['id']
)
allow(project_config).to receive(:send_flag_decisions).and_return(false)
allow(project_instance.event_dispatcher).to receive(:dispatch_event).with(instance_of(Optimizely::Event))
Expand Down Expand Up @@ -3921,7 +3927,9 @@ def callback(_args); end
variation_key: nil,
rule_key: nil,
reasons: [],
decision_event_dispatched: false
decision_event_dispatched: false,
experiment_id: nil,
variation_id: nil
)
allow(project_config).to receive(:send_flag_decisions).and_return(false)
allow(project_instance.event_dispatcher).to receive(:dispatch_event).with(instance_of(Optimizely::Event))
Expand Down Expand Up @@ -3958,7 +3966,9 @@ def callback(_args); end
variation_key: nil,
rule_key: nil,
reasons: [],
decision_event_dispatched: true
decision_event_dispatched: true,
experiment_id: nil,
variation_id: nil
)
allow(project_instance.event_dispatcher).to receive(:dispatch_event).with(instance_of(Optimizely::Event))
decision_to_return = nil
Expand Down Expand Up @@ -4122,7 +4132,9 @@ def callback(_args); end
"The user 'user1' is not bucketed into any of the experiments on the feature 'multi_variate_feature'.",
"Feature flag 'multi_variate_feature' is not used in a rollout."
],
decision_event_dispatched: true
decision_event_dispatched: true,
experiment_id: nil,
variation_id: nil
)
expect(project_instance.notification_center).to receive(:send_notifications)
.once.with(Optimizely::NotificationCenter::NOTIFICATION_TYPES[:LOG_EVENT], any_args)
Expand Down Expand Up @@ -4162,7 +4174,9 @@ def callback(_args); end
variation_key: nil,
rule_key: nil,
reasons: [],
decision_event_dispatched: true
decision_event_dispatched: true,
experiment_id: nil,
variation_id: nil
)
allow(project_instance.event_dispatcher).to receive(:dispatch_event).with(instance_of(Optimizely::Event))
user_context = project_instance.create_user_context('user1')
Expand Down Expand Up @@ -4481,7 +4495,9 @@ def callback(_args); end
"The user 'user1' is not bucketed into any of the experiments on the feature 'multi_variate_feature'.",
"Feature flag 'multi_variate_feature' is not used in a rollout."
],
decision_event_dispatched: true
decision_event_dispatched: true,
experiment_id: nil,
variation_id: nil
)
allow(custom_project_instance.event_dispatcher).to receive(:dispatch_event).with(instance_of(Optimizely::Event))
user_context = custom_project_instance.create_user_context('user1')
Expand Down Expand Up @@ -4521,7 +4537,9 @@ def callback(_args); end
variation_key: nil,
rule_key: nil,
reasons: [],
decision_event_dispatched: true
decision_event_dispatched: true,
experiment_id: nil,
variation_id: nil
)
allow(custom_project_instance.event_dispatcher).to receive(:dispatch_event).with(instance_of(Optimizely::Event))
user_context = custom_project_instance.create_user_context('user1')
Expand Down
Loading