Method: Optimizely::DecisionService#get_variation_for_feature_experiment

Defined in:
lib/optimizely/decision_service.rb

#get_variation_for_feature_experiment(project_config, feature_flag, user_id, attributes = nil, decide_options = []) ⇒ Object



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
203
204
205
# File 'lib/optimizely/decision_service.rb', line 160

def get_variation_for_feature_experiment(project_config, feature_flag, user_id, attributes = nil, decide_options = [])
  # Gets the variation the user is bucketed into for the feature flag's experiment.
  #
  # project_config - project_config - Instance of ProjectConfig
  # feature_flag - The feature flag the user wants to access
  # user_id - String ID for the user
  # attributes - Hash representing user attributes
  #
  # Returns Decision struct (nil if the user is not bucketed into any of the experiments on the feature)
  # or nil if the user is not bucketed into any of the experiments on the feature
  decide_reasons = []
  feature_flag_key = feature_flag['key']
  if feature_flag['experimentIds'].empty?
    message = "The feature flag '#{feature_flag_key}' is not used in any experiments."
    @logger.log(Logger::DEBUG, message)
    decide_reasons.push(message)
    return nil, decide_reasons
  end

  # Evaluate each experiment and return the first bucketed experiment variation
  feature_flag['experimentIds'].each do |experiment_id|
    experiment = project_config.experiment_id_map[experiment_id]
    unless experiment
      message = "Feature flag experiment with ID '#{experiment_id}' is not in the datafile."
      @logger.log(Logger::DEBUG, message)
      decide_reasons.push(message)
      return nil, decide_reasons
    end

    experiment_id = experiment['id']
    variation_id, reasons_received = get_variation(project_config, experiment_id, user_id, attributes, decide_options)
    decide_reasons.push(*reasons_received)

    next unless variation_id

    variation = project_config.get_variation_from_id_by_experiment_id(experiment_id, variation_id)

    return Decision.new(experiment, variation, DECISION_SOURCES['FEATURE_TEST']), decide_reasons
  end

  message = "The user '#{user_id}' is not bucketed into any of the experiments on the feature '#{feature_flag_key}'."
  @logger.log(Logger::INFO, message)
  decide_reasons.push(message)

  [nil, decide_reasons]
end