Module: Optimizely::Audience

Defined in:
lib/optimizely/audience.rb

Class Method Summary collapse

Class Method Details

.get_segments(conditions) ⇒ Object



94
95
96
97
98
99
100
101
102
103
# File 'lib/optimizely/audience.rb', line 94

def get_segments(conditions)
  # Return any audience segments from provided conditions.
  #
  # conditions - Nested array of and/or conditions.
  #              Example: ['and', operand_1, ['or', operand_2, operand_3]]
  #
  # Returns unique array of segment names.
  conditions = JSON.parse(conditions) if conditions.is_a?(String)
  @parse_segments.call(conditions).uniq
end

.user_meets_audience_conditions?(config, experiment, user_context, logger, logging_hash = nil, logging_key = nil) ⇒ Boolean

Returns:

  • (Boolean)


27
28
29
30
31
32
33
34
35
36
37
38
39
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
85
86
87
88
89
90
91
92
# File 'lib/optimizely/audience.rb', line 27

def user_meets_audience_conditions?(config, experiment, user_context, logger, logging_hash = nil, logging_key = nil)
  # Determine for given experiment/rollout rule if user satisfies the audience conditions.
  #
  # config - Representation of the Optimizely project config.
  # experiment - Experiment/Rollout rule in which user is to be bucketed.
  # user_context - Optimizely user context instance
  # logger - Provides a logger instance.
  # logging_hash - Optional string representing logs hash inside Helpers::Constants.
  #                This defaults to 'EXPERIMENT_AUDIENCE_EVALUATION_LOGS'.
  # logging_key - Optional string to be logged as an identifier of experiment under evaluation.
  #               This defaults to experiment['key'].
  #
  # Returns boolean representing if user satisfies audience conditions for the audiences or not.
  decide_reasons = []
  logging_hash ||= 'EXPERIMENT_AUDIENCE_EVALUATION_LOGS'
  logging_key ||= experiment['key']

  logs_hash = Object.const_get "Optimizely::Helpers::Constants::#{logging_hash}"

  audience_conditions = experiment['audienceConditions'] || experiment['audienceIds']

  message = format(logs_hash['EVALUATING_AUDIENCES_COMBINED'], logging_key, audience_conditions)
  logger.log(Logger::DEBUG, message)

  # Return true if there are no audiences
  if audience_conditions.empty?
    message = format(logs_hash['AUDIENCE_EVALUATION_RESULT_COMBINED'], logging_key, 'TRUE')
    logger.log(Logger::INFO, message)
    decide_reasons.push(message)
    return true, decide_reasons
  end

  user_condition_evaluator = UserConditionEvaluator.new(user_context, logger)

  evaluate_user_conditions = lambda do |condition|
    user_condition_evaluator.evaluate(condition)
  end

  evaluate_audience = lambda do |audience_id|
    audience = config.get_audience_from_id(audience_id)
    return nil unless audience

    audience_conditions = audience['conditions']
    message = format(logs_hash['EVALUATING_AUDIENCE'], audience_id, audience_conditions)
    logger.log(Logger::DEBUG, message)
    decide_reasons.push(message)

    audience_conditions = JSON.parse(audience_conditions) if audience_conditions.is_a?(String)
    result = ConditionTreeEvaluator.evaluate(audience_conditions, evaluate_user_conditions)
    result_str = result.nil? ? 'UNKNOWN' : result.to_s.upcase
    message = format(logs_hash['AUDIENCE_EVALUATION_RESULT'], audience_id, result_str)
    logger.log(Logger::DEBUG, message)
    decide_reasons.push(message)

    result
  end

  eval_result = ConditionTreeEvaluator.evaluate(audience_conditions, evaluate_audience)
  eval_result ||= false

  message = format(logs_hash['AUDIENCE_EVALUATION_RESULT_COMBINED'], logging_key, eval_result.to_s.upcase)
  logger.log(Logger::INFO, message)
  decide_reasons.push(message)

  [eval_result, decide_reasons]
end