Skip to content

Commit cc2ccb8

Browse files
rashidspmikeproeng37
authored andcommitted
fix(track): Send decisions for all experiments using an event when using track (#120)
1 parent 29a511b commit cc2ccb8

File tree

3 files changed

+45
-31
lines changed

3 files changed

+45
-31
lines changed

lib/optimizely/event_builder.rb

+25-30
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ def create_conversion_event(event_key, user_id, attributes, event_tags, experime
153153

154154
event_params = get_common_params(user_id, attributes)
155155
conversion_params = get_conversion_params(event_key, event_tags, experiment_variation_map)
156-
event_params[:visitors][0][:snapshots] = conversion_params
156+
event_params[:visitors][0][:snapshots] = [conversion_params]
157157

158158
Event.new(:post, ENDPOINT, event_params, POST_HEADERS)
159159
end
@@ -195,43 +195,38 @@ def get_conversion_params(event_key, event_tags, experiment_variation_map)
195195
# event_tags - +Hash+ Values associated with the event.
196196
# experiment_variation_map - +Hash+ Map of experiment IDs to bucketed variation IDs
197197
#
198-
# Returns +Hash+ Impression event params
199-
200-
conversion_event_params = []
198+
# Returns +Hash+ Conversion event params
201199

200+
single_snapshot = {}
201+
single_snapshot[:decisions] = []
202202
experiment_variation_map.each do |experiment_id, variation_id|
203-
single_snapshot = {
204-
decisions: [{
205-
campaign_id: @config.experiment_id_map[experiment_id]['layerId'],
206-
experiment_id: experiment_id,
207-
variation_id: variation_id
208-
}],
209-
events: []
210-
}
211-
212-
event_object = {
213-
entity_id: @config.event_key_map[event_key]['id'],
214-
timestamp: create_timestamp,
215-
uuid: create_uuid,
216-
key: event_key
217-
}
218-
219-
if event_tags
220-
revenue_value = Helpers::EventTagUtils.get_revenue_value(event_tags, @logger)
221-
event_object[:revenue] = revenue_value if revenue_value
203+
next unless variation_id
204+
single_snapshot[:decisions].push(
205+
campaign_id: @config.experiment_id_map[experiment_id]['layerId'],
206+
experiment_id: experiment_id,
207+
variation_id: variation_id
208+
)
209+
end
222210

223-
numeric_value = Helpers::EventTagUtils.get_numeric_value(event_tags, @logger)
224-
event_object[:value] = numeric_value if numeric_value
211+
event_object = {
212+
entity_id: @config.event_key_map[event_key]['id'],
213+
timestamp: create_timestamp,
214+
uuid: create_uuid,
215+
key: event_key
216+
}
225217

226-
event_object[:tags] = event_tags
227-
end
218+
if event_tags
219+
revenue_value = Helpers::EventTagUtils.get_revenue_value(event_tags, @logger)
220+
event_object[:revenue] = revenue_value if revenue_value
228221

229-
single_snapshot[:events] = [event_object]
222+
numeric_value = Helpers::EventTagUtils.get_numeric_value(event_tags, @logger)
223+
event_object[:value] = numeric_value if numeric_value
230224

231-
conversion_event_params.push(single_snapshot)
225+
event_object[:tags] = event_tags unless event_tags.empty?
232226
end
233227

234-
conversion_event_params
228+
single_snapshot[:events] = [event_object]
229+
single_snapshot
235230
end
236231

237232
def create_timestamp

spec/event_builder_spec.rb

+19
Original file line numberDiff line numberDiff line change
@@ -525,4 +525,23 @@
525525
expect(conversion_event.url).to eq(@expected_endpoint)
526526
expect(conversion_event.http_verb).to eq(:post)
527527
end
528+
529+
describe 'multiple_experiments_event' do
530+
it 'should create_conversion_event when Event is used in multiple experiments' do
531+
@expected_conversion_params[:visitors][0][:snapshots][0][:decisions] = [{
532+
campaign_id: '1',
533+
experiment_id: '111127',
534+
variation_id: '111128'
535+
}, {
536+
campaign_id: '4',
537+
experiment_id: '122230',
538+
variation_id: '122231'
539+
}]
540+
541+
conversion_event = @event_builder.create_conversion_event('test_event', 'test_user', nil, nil, '111127' => '111128', '122230' => '122231')
542+
expect(conversion_event.params).to eq(@expected_conversion_params)
543+
expect(conversion_event.url).to eq(@expected_endpoint)
544+
expect(conversion_event.http_verb).to eq(:post)
545+
end
546+
end
528547
end

spec/spec_params.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ module OptimizelySpec
2727
'version' => '2',
2828
'events' => [{
2929
'key' => 'test_event',
30-
'experimentIds' => ['111127'],
30+
'experimentIds' => %w[111127 122230],
3131
'id' => '111095'
3232
}, {
3333
'key' => 'Total Revenue',

0 commit comments

Comments
 (0)