Skip to content

Commit 4c8b534

Browse files
rashidspaliabbasrizvi
authored andcommitted
feat(track): Introducing easier event tracking (#155)
1 parent ce0cf47 commit 4c8b534

File tree

6 files changed

+93
-214
lines changed

6 files changed

+93
-214
lines changed

lib/optimizely.rb

Lines changed: 6 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# frozen_string_literal: true
22

33
#
4-
# Copyright 2016-2018, Optimizely and contributors
4+
# Copyright 2016-2019, Optimizely and contributors
55
#
66
# Licensed under the Apache License, Version 2.0 (the "License");
77
# you may not use this file except in compliance with the License.
@@ -201,24 +201,14 @@ def track(event_key, user_id, attributes = nil, event_tags = nil)
201201

202202
return nil unless user_inputs_valid?(attributes, event_tags)
203203

204-
experiment_ids = @config.get_experiment_ids_for_event(event_key)
205-
if experiment_ids.empty?
206-
@config.logger.log(Logger::INFO, "Not tracking user '#{user_id}'.")
204+
event = @config.get_event_from_key(event_key)
205+
unless event
206+
@config.logger.log(Logger::INFO, "Not tracking user '#{user_id}' for event '#{event_key}'.")
207207
return nil
208208
end
209209

210-
# Filter out experiments that are not running or that do not include the user in audience conditions
211-
212-
experiment_variation_map = get_valid_experiments_for_event(event_key, user_id, attributes)
213-
214-
# Don't track events without valid experiments attached
215-
if experiment_variation_map.empty?
216-
@logger.log(Logger::INFO, "There are no valid experiments for event '#{event_key}' to track.")
217-
return nil
218-
end
219-
220-
conversion_event = @event_builder.create_conversion_event(event_key, user_id, attributes,
221-
event_tags, experiment_variation_map)
210+
conversion_event = @event_builder.create_conversion_event(event, user_id, attributes, event_tags)
211+
@config.logger.log(Logger::INFO, "Tracking event '#{event_key}' for user '#{user_id}'.")
222212
@logger.log(Logger::INFO,
223213
"Dispatching conversion event to URL #{conversion_event.url} with params #{conversion_event.params}.")
224214
begin
@@ -502,34 +492,6 @@ def get_feature_variable_for_type(feature_flag_key, variable_key, variable_type,
502492
variable_value
503493
end
504494

505-
def get_valid_experiments_for_event(event_key, user_id, attributes)
506-
# Get the experiments that we should be tracking for the given event.
507-
#
508-
# event_key - Event key representing the event which needs to be recorded.
509-
# user_id - String ID for user.
510-
# attributes - Map of attributes of the user.
511-
#
512-
# Returns Map where each object contains the ID of the experiment to track and the ID of the variation the user
513-
# is bucketed into.
514-
515-
valid_experiments = {}
516-
experiment_ids = @config.get_experiment_ids_for_event(event_key)
517-
experiment_ids.each do |experiment_id|
518-
experiment_key = @config.get_experiment_key(experiment_id)
519-
variation_key = get_variation(experiment_key, user_id, attributes)
520-
521-
if variation_key.nil?
522-
@logger.log(Logger::INFO, "Not tracking user '#{user_id}' for experiment '#{experiment_key}'.")
523-
next
524-
end
525-
526-
variation_id = @config.get_variation_id_from_key(experiment_key, variation_key)
527-
valid_experiments[experiment_id] = variation_id
528-
end
529-
530-
valid_experiments
531-
end
532-
533495
def user_inputs_valid?(attributes = nil, event_tags = nil)
534496
# Helper method to validate user inputs.
535497
#

lib/optimizely/event_builder.rb

Lines changed: 9 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# frozen_string_literal: true
22

33
#
4-
# Copyright 2016-2018, Optimizely and contributors
4+
# Copyright 2016-2019, Optimizely and contributors
55
#
66
# Licensed under the Apache License, Version 2.0 (the "License");
77
# you may not use this file except in compliance with the License.
@@ -113,6 +113,7 @@ def get_common_params(user_id, attributes)
113113
anonymize_ip: @config.anonymize_ip,
114114
revision: @config.revision,
115115
client_name: CLIENT_ENGINE,
116+
enrich_decisions: true,
116117
client_version: VERSION
117118
}
118119

@@ -142,19 +143,18 @@ def create_impression_event(experiment, variation_id, user_id, attributes)
142143
Event.new(:post, ENDPOINT, event_params, POST_HEADERS)
143144
end
144145

145-
def create_conversion_event(event_key, user_id, attributes, event_tags, experiment_variation_map)
146+
def create_conversion_event(event, user_id, attributes, event_tags)
146147
# Create conversion Event to be sent to the logging endpoint.
147148
#
148-
# event_key - +String+ Event key representing the event which needs to be recorded.
149+
# event - +Object+ Event which needs to be recorded.
149150
# user_id - +String+ ID for user.
150151
# attributes - +Hash+ representing user attributes and values which need to be recorded.
151152
# event_tags - +Hash+ representing metadata associated with the event.
152-
# experiment_variation_map - +Map+ of experiment ID to the ID of the variation that the user is bucketed into.
153153
#
154154
# Returns +Event+ encapsulating the conversion event.
155155

156156
event_params = get_common_params(user_id, attributes)
157-
conversion_params = get_conversion_params(event_key, event_tags, experiment_variation_map)
157+
conversion_params = get_conversion_params(event, event_tags)
158158
event_params[:visitors][0][:snapshots] = [conversion_params]
159159

160160
Event.new(:post, ENDPOINT, event_params, POST_HEADERS)
@@ -190,32 +190,20 @@ def get_impression_params(experiment, variation_id)
190190
impression_event_params
191191
end
192192

193-
def get_conversion_params(event_key, event_tags, experiment_variation_map)
193+
def get_conversion_params(event, event_tags)
194194
# Creates object of params specific to conversion events
195195
#
196-
# event_key - +String+ Key representing the event which needs to be recorded
196+
# event - +Object+ Event which needs to be recorded.
197197
# event_tags - +Hash+ Values associated with the event.
198-
# experiment_variation_map - +Hash+ Map of experiment IDs to bucketed variation IDs
199198
#
200199
# Returns +Hash+ Conversion event params
201200

202201
single_snapshot = {}
203-
single_snapshot[:decisions] = []
204-
experiment_variation_map.each do |experiment_id, variation_id|
205-
next unless variation_id
206-
207-
single_snapshot[:decisions].push(
208-
campaign_id: @config.experiment_id_map[experiment_id]['layerId'],
209-
experiment_id: experiment_id,
210-
variation_id: variation_id
211-
)
212-
end
213-
214202
event_object = {
215-
entity_id: @config.event_key_map[event_key]['id'],
203+
entity_id: event['id'],
216204
timestamp: create_timestamp,
217205
uuid: create_uuid,
218-
key: event_key
206+
key: event['key']
219207
}
220208

221209
if event_tags

lib/optimizely/project_config.rb

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -185,19 +185,19 @@ def get_experiment_key(experiment_id)
185185
nil
186186
end
187187

188-
def get_experiment_ids_for_event(event_key)
189-
# Get experiment IDs for the provided event key.
188+
def get_event_from_key(event_key)
189+
# Get event for the provided event key.
190190
#
191-
# event_key - Event key for which experiment IDs are to be retrieved.
191+
# event_key - Event key for which event is to be determined.
192192
#
193-
# Returns array of all experiment IDs for the event.
193+
# Returns Event corresponding to the provided event key.
194194

195195
event = @event_key_map[event_key]
196-
return event['experimentIds'] if event
196+
return event if event
197197

198198
@logger.log Logger::ERROR, "Event '#{event_key}' is not in datafile."
199199
@error_handler.handle_error InvalidEventError
200-
[]
200+
nil
201201
end
202202

203203
def get_audience_from_id(audience_id)

0 commit comments

Comments
 (0)