Skip to content

fix(track): Send decisions for all experiments using an event when using track #120

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

Merged
merged 2 commits into from
Aug 28, 2018
Merged
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
55 changes: 25 additions & 30 deletions lib/optimizely/event_builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ def create_conversion_event(event_key, user_id, attributes, event_tags, experime

event_params = get_common_params(user_id, attributes)
conversion_params = get_conversion_params(event_key, event_tags, experiment_variation_map)
event_params[:visitors][0][:snapshots] = conversion_params
event_params[:visitors][0][:snapshots] = [conversion_params]

Event.new(:post, ENDPOINT, event_params, POST_HEADERS)
end
Expand Down Expand Up @@ -195,43 +195,38 @@ def get_conversion_params(event_key, event_tags, experiment_variation_map)
# event_tags - +Hash+ Values associated with the event.
# experiment_variation_map - +Hash+ Map of experiment IDs to bucketed variation IDs
#
# Returns +Hash+ Impression event params

conversion_event_params = []
# Returns +Hash+ Conversion event params

single_snapshot = {}
single_snapshot[:decisions] = []
experiment_variation_map.each do |experiment_id, variation_id|
single_snapshot = {
decisions: [{
campaign_id: @config.experiment_id_map[experiment_id]['layerId'],
experiment_id: experiment_id,
variation_id: variation_id
}],
events: []
}

event_object = {
entity_id: @config.event_key_map[event_key]['id'],
timestamp: create_timestamp,
uuid: create_uuid,
key: event_key
}

if event_tags
revenue_value = Helpers::EventTagUtils.get_revenue_value(event_tags, @logger)
event_object[:revenue] = revenue_value if revenue_value
next unless variation_id
single_snapshot[:decisions].push(
campaign_id: @config.experiment_id_map[experiment_id]['layerId'],
experiment_id: experiment_id,
variation_id: variation_id
)
end

numeric_value = Helpers::EventTagUtils.get_numeric_value(event_tags, @logger)
event_object[:value] = numeric_value if numeric_value
event_object = {
entity_id: @config.event_key_map[event_key]['id'],
timestamp: create_timestamp,
uuid: create_uuid,
key: event_key
}

event_object[:tags] = event_tags
end
if event_tags
revenue_value = Helpers::EventTagUtils.get_revenue_value(event_tags, @logger)
event_object[:revenue] = revenue_value if revenue_value

single_snapshot[:events] = [event_object]
numeric_value = Helpers::EventTagUtils.get_numeric_value(event_tags, @logger)
event_object[:value] = numeric_value if numeric_value

conversion_event_params.push(single_snapshot)
event_object[:tags] = event_tags unless event_tags.empty?
end

conversion_event_params
single_snapshot[:events] = [event_object]
single_snapshot
end

def create_timestamp
Expand Down
19 changes: 19 additions & 0 deletions spec/event_builder_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -525,4 +525,23 @@
expect(conversion_event.url).to eq(@expected_endpoint)
expect(conversion_event.http_verb).to eq(:post)
end

describe 'multiple_experiments_event' do
it 'should create_conversion_event when Event is used in multiple experiments' do
@expected_conversion_params[:visitors][0][:snapshots][0][:decisions] = [{
campaign_id: '1',
experiment_id: '111127',
variation_id: '111128'
}, {
campaign_id: '4',
experiment_id: '122230',
variation_id: '122231'
}]

conversion_event = @event_builder.create_conversion_event('test_event', 'test_user', nil, nil, '111127' => '111128', '122230' => '122231')
expect(conversion_event.params).to eq(@expected_conversion_params)
expect(conversion_event.url).to eq(@expected_endpoint)
expect(conversion_event.http_verb).to eq(:post)
end
end
end
2 changes: 1 addition & 1 deletion spec/spec_params.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ module OptimizelySpec
'version' => '2',
'events' => [{
'key' => 'test_event',
'experimentIds' => ['111127'],
'experimentIds' => %w[111127 122230],
'id' => '111095'
}, {
'key' => 'Total Revenue',
Expand Down