Class: Optimizely::Project
- Inherits:
-
Object
- Object
- Optimizely::Project
- Defined in:
- lib/optimizely.rb
Constant Summary collapse
- EVENT_BUILDERS_BY_VERSION =
{ Optimizely::V1_CONFIG_VERSION => EventBuilderV1, Optimizely::V2_CONFIG_VERSION => EventBuilderV2 }
Instance Attribute Summary collapse
-
#bucketer ⇒ Object
Returns the value of attribute bucketer.
-
#config ⇒ Object
Returns the value of attribute config.
-
#error_handler ⇒ Object
Returns the value of attribute error_handler.
-
#event_builder ⇒ Object
Returns the value of attribute event_builder.
-
#event_dispatcher ⇒ Object
Returns the value of attribute event_dispatcher.
-
#is_valid ⇒ Object
readonly
Boolean representing if the instance represents a usable Optimizely Project.
-
#logger ⇒ Object
Returns the value of attribute logger.
Instance Method Summary collapse
- #activate(experiment_key, user_id, attributes = nil) ⇒ Object
- #get_variation(experiment_key, user_id, attributes = nil) ⇒ Object
-
#initialize(datafile, event_dispatcher = nil, logger = nil, error_handler = nil, skip_json_validation = false) ⇒ Project
constructor
A new instance of Project.
- #track(event_key, user_id, attributes = nil, event_tags = nil) ⇒ Object
Constructor Details
#initialize(datafile, event_dispatcher = nil, logger = nil, error_handler = nil, skip_json_validation = false) ⇒ Project
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/optimizely.rb', line 45 def initialize(datafile, event_dispatcher = nil, logger = nil, error_handler = nil, skip_json_validation = false) # Constructor for Projects. # # datafile - JSON string representing the project. # event_dispatcher - Provides a dispatch_event method which if given a URL and params sends a request to it. # logger - Optional param which provides a log method to log messages. By default nothing would be logged. # error_handler - Optional param which provides a handle_error method to handle exceptions. # By default all exceptions will be suppressed. # skip_json_validation - Optional boolean param to skip JSON schema validation of the provided datafile. @is_valid = true @logger = logger || NoOpLogger.new @error_handler = error_handler || NoOpErrorHandler.new @event_dispatcher = event_dispatcher || EventDispatcher.new begin validate_inputs(datafile, skip_json_validation) rescue InvalidInputError => e @is_valid = false logger = SimpleLogger.new logger.log(Logger::ERROR, e.) return end begin @config = ProjectConfig.new(datafile, @logger, @error_handler) rescue @is_valid = false logger = SimpleLogger.new logger.log(Logger::ERROR, InvalidInputError.new('datafile').) return end begin @bucketer = Bucketer.new(@config) @event_builder = EVENT_BUILDERS_BY_VERSION[@config.version].new(@config, @bucketer) rescue @is_valid = false logger = SimpleLogger.new logger.log(Logger::ERROR, InvalidDatafileVersionError.new) end end |
Instance Attribute Details
#bucketer ⇒ Object
Returns the value of attribute bucketer.
34 35 36 |
# File 'lib/optimizely.rb', line 34 def bucketer @bucketer end |
#config ⇒ Object
Returns the value of attribute config.
33 34 35 |
# File 'lib/optimizely.rb', line 33 def config @config end |
#error_handler ⇒ Object
Returns the value of attribute error_handler.
38 39 40 |
# File 'lib/optimizely.rb', line 38 def error_handler @error_handler end |
#event_builder ⇒ Object
Returns the value of attribute event_builder.
35 36 37 |
# File 'lib/optimizely.rb', line 35 def event_builder @event_builder end |
#event_dispatcher ⇒ Object
Returns the value of attribute event_dispatcher.
36 37 38 |
# File 'lib/optimizely.rb', line 36 def event_dispatcher @event_dispatcher end |
#is_valid ⇒ Object (readonly)
Boolean representing if the instance represents a usable Optimizely Project
31 32 33 |
# File 'lib/optimizely.rb', line 31 def is_valid @is_valid end |
#logger ⇒ Object
Returns the value of attribute logger.
37 38 39 |
# File 'lib/optimizely.rb', line 37 def logger @logger end |
Instance Method Details
#activate(experiment_key, user_id, attributes = nil) ⇒ Object
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 |
# File 'lib/optimizely.rb', line 88 def activate(experiment_key, user_id, attributes = nil) # Buckets visitor and sends impression event to Optimizely. # # experiment_key - Experiment which needs to be activated. # user_id - String ID for user. # attributes - Hash representing user attributes and values to be recorded. # # Returns variation key representing the variation the user will be bucketed in. # Returns nil if experiment is not Running, if user is not in experiment, or if datafile is invalid. unless @is_valid logger = SimpleLogger.new logger.log(Logger::ERROR, InvalidDatafileError.new('activate').) return nil end if attributes && !attributes_valid?(attributes) @logger.log(Logger::INFO, "Not activating user '#{user_id}'.") return nil end unless preconditions_valid?(experiment_key, user_id, attributes) @logger.log(Logger::INFO, "Not activating user '#{user_id}'.") return nil end variation_id = @bucketer.bucket(experiment_key, user_id) if not variation_id @logger.log(Logger::INFO, "Not activating user '#{user_id}'.") return nil end # Create and dispatch impression event impression_event = @event_builder.create_impression_event(experiment_key, variation_id, user_id, attributes) @logger.log(Logger::INFO, 'Dispatching impression event to URL %s with params %s.' % [impression_event.url, impression_event.params]) begin @event_dispatcher.dispatch_event(impression_event) rescue => e @logger.log(Logger::ERROR, "Unable to dispatch impression event. Error: #{e}") end @config.get_variation_key_from_id(experiment_key, variation_id) end |
#get_variation(experiment_key, user_id, attributes = nil) ⇒ Object
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 |
# File 'lib/optimizely.rb', line 135 def get_variation(experiment_key, user_id, attributes = nil) # Gets variation where visitor will be bucketed. # # experiment_key - Experiment for which visitor variation needs to be determined. # user_id - String ID for user. # attributes - Hash representing user attributes. # # Returns variation key where visitor will be bucketed. # Returns nil if experiment is not Running, if user is not in experiment, or if datafile is invalid. unless @is_valid logger = SimpleLogger.new logger.log(Logger::ERROR, InvalidDatafileError.new('get_variation').) return nil end if attributes && !attributes_valid?(attributes) @logger.log(Logger::INFO, "Not activating user '#{user_id}.") return nil end unless preconditions_valid?(experiment_key, user_id, attributes) @logger.log(Logger::INFO, "Not activating user '#{user_id}.") return nil end variation_id = @bucketer.bucket(experiment_key, user_id) @config.get_variation_key_from_id(experiment_key, variation_id) end |
#track(event_key, user_id, attributes = nil, event_tags = nil) ⇒ Object
165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 |
# File 'lib/optimizely.rb', line 165 def track(event_key, user_id, attributes = nil, = nil) # Send conversion event to Optimizely. # # event_key - Goal key representing the event which needs to be recorded. # user_id - String ID for user. # attributes - Hash representing visitor attributes and values which need to be recorded. # event_tags - Hash representing metadata associated with the event. unless @is_valid logger = SimpleLogger.new logger.log(Logger::ERROR, InvalidDatafileError.new('track').) return nil end if and .is_a? Numeric = { 'revenue' => } @logger.log(Logger::WARN, 'Event value is deprecated in track call. Use event tags to pass in revenue value instead.') end return nil if attributes && !attributes_valid?(attributes) return nil if && !() experiment_ids = @config.get_experiment_ids_for_goal(event_key) if experiment_ids.empty? @config.logger.log(Logger::INFO, "Not tracking user '#{user_id}'.") return nil end # Filter out experiments that are not running or that do not include the user in audience conditions valid_experiment_keys = [] experiment_ids.each do |experiment_id| experiment_key = @config.experiment_id_map[experiment_id]['key'] unless preconditions_valid?(experiment_key, user_id, attributes) @config.logger.log(Logger::INFO, "Not tracking user '#{user_id}' for experiment '#{experiment_key}'.") next end valid_experiment_keys.push(experiment_key) end # Don't track events without valid experiments attached if valid_experiment_keys.empty? @logger.log(Logger::INFO, "There are no valid experiments for event '#{event_key}' to track.") return nil end conversion_event = @event_builder.create_conversion_event(event_key, user_id, attributes, , valid_experiment_keys) @logger.log(Logger::INFO, 'Dispatching conversion event to URL %s with params %s.' % [conversion_event.url, conversion_event.params]) begin @event_dispatcher.dispatch_event(conversion_event) rescue => e @logger.log(Logger::ERROR, "Unable to dispatch conversion event. Error: #{e}") end end |