Class: Optimizely::Project
- Inherits:
-
Object
- Object
- Optimizely::Project
- Defined in:
- lib/optimizely.rb
Instance Attribute Summary collapse
-
#config ⇒ Object
readonly
Returns the value of attribute config.
-
#decision_service ⇒ Object
readonly
Returns the value of attribute decision_service.
-
#error_handler ⇒ Object
readonly
Returns the value of attribute error_handler.
-
#event_builder ⇒ Object
readonly
Returns the value of attribute event_builder.
-
#event_dispatcher ⇒ Object
readonly
Returns the value of attribute event_dispatcher.
-
#is_valid ⇒ Object
readonly
Boolean representing if the instance represents a usable Optimizely Project.
-
#logger ⇒ Object
readonly
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, user_profile_service = nil) ⇒ 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, user_profile_service = nil) ⇒ Project
Returns a new instance of Project.
40 41 42 43 44 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 |
# File 'lib/optimizely.rb', line 40 def initialize(datafile, event_dispatcher = nil, logger = nil, error_handler = nil, skip_json_validation = false, user_profile_service = nil) # 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 component which provides a log method to log messages. By default nothing would be logged. # error_handler - Optional component which provides a handle_error method to handle exceptions. # By default all exceptions will be suppressed. # user_profile_service - Optional component which provides methods to store and retreive user profiles. # 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 @user_profile_service = user_profile_service begin (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 unless @config.parsing_succeeded? @is_valid = false logger = SimpleLogger.new logger.log(Logger::ERROR, InvalidDatafileVersionError.new.) return end @decision_service = DecisionService.new(@config, @user_profile_service) @event_builder = EventBuilderV2.new(@config) end |
Instance Attribute Details
#config ⇒ Object (readonly)
Returns the value of attribute config.
33 34 35 |
# File 'lib/optimizely.rb', line 33 def config @config end |
#decision_service ⇒ Object (readonly)
Returns the value of attribute decision_service.
34 35 36 |
# File 'lib/optimizely.rb', line 34 def decision_service @decision_service end |
#error_handler ⇒ Object (readonly)
Returns the value of attribute error_handler.
35 36 37 |
# File 'lib/optimizely.rb', line 35 def error_handler @error_handler end |
#event_builder ⇒ Object (readonly)
Returns the value of attribute event_builder.
36 37 38 |
# File 'lib/optimizely.rb', line 36 def event_builder @event_builder end |
#event_dispatcher ⇒ Object (readonly)
Returns the value of attribute event_dispatcher.
37 38 39 |
# File 'lib/optimizely.rb', line 37 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 (readonly)
Returns the value of attribute logger.
38 39 40 |
# File 'lib/optimizely.rb', line 38 def logger @logger end |
Instance Method Details
#activate(experiment_key, user_id, attributes = nil) ⇒ Object
86 87 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 |
# File 'lib/optimizely.rb', line 86 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 variation_key = get_variation(experiment_key, user_id, attributes) if variation_key.nil? @logger.log(Logger::INFO, "Not activating user '#{user_id}'.") return nil end # Create and dispatch impression event variation_id = @config.get_variation_id_from_key(experiment_key, variation_key) 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 variation_key end |
#get_variation(experiment_key, user_id, attributes = nil) ⇒ Object
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 |
# File 'lib/optimizely.rb', line 124 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 unless user_inputs_valid?(attributes) @logger.log(Logger::INFO, "Not activating user '#{user_id}.") return nil end variation_id = @decision_service.get_variation(experiment_key, user_id, attributes) unless variation_id.nil? return @config.get_variation_key_from_id(experiment_key, variation_id) end nil end |
#track(event_key, user_id, attributes = nil, event_tags = nil) ⇒ Object
153 154 155 156 157 158 159 160 161 162 163 164 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 |
# File 'lib/optimizely.rb', line 153 def track(event_key, user_id, attributes = nil, = nil) # Send conversion event to Optimizely. # # event_key - Event 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 unless user_inputs_valid?(attributes, ) experiment_ids = @config.get_experiment_ids_for_event(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 experiment_variation_map = get_valid_experiments_for_event(event_key, user_id, attributes) # Don't track events without valid experiments attached if experiment_variation_map.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, , experiment_variation_map) @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 |