Class: Optimizely::DecisionService

Inherits:
Object
  • Object
show all
Defined in:
lib/optimizely/decision_service.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config, user_profile_service = nil) ⇒ DecisionService

Returns a new instance of DecisionService.



34
35
36
37
38
# File 'lib/optimizely/decision_service.rb', line 34

def initialize(config,  = nil)
  @config = config
  @user_profile_service = 
  @bucketer = Bucketer.new(@config)
end

Instance Attribute Details

#bucketerObject (readonly)

Optimizely’s decision service that determines into which variation of an experiment a user will be allocated.

The decision service contains all logic relating to how a user bucketing decisions is made. This includes all of the following (in order):

  1. Checking experiment status

  2. Checking whitelisting

  3. Checking user profile service for past bucketing decisions (sticky bucketing)

  4. Checking audience targeting

  5. Using Murmurhash3 to bucket the user



31
32
33
# File 'lib/optimizely/decision_service.rb', line 31

def bucketer
  @bucketer
end

#configObject (readonly)

Returns the value of attribute config.



32
33
34
# File 'lib/optimizely/decision_service.rb', line 32

def config
  @config
end

Instance Method Details

#get_variation(experiment_key, user_id, attributes = nil) ⇒ Object



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
# File 'lib/optimizely/decision_service.rb', line 40

def get_variation(experiment_key, user_id, attributes = nil)
  # Determines variation into which user 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 ID where visitor will be bucketed (nil if experiment is inactive or user does not meet audience conditions)

  # Check to make sure experiment is active
  unless @config.experiment_running?(experiment_key)
    @config.logger.log(Logger::INFO, "Experiment '#{experiment_key}' is not running.")
    return nil
  end

  experiment_id = @config.get_experiment_id(experiment_key)

  # Check if user is in a forced variation
  forced_variation_id = get_forced_variation_id(experiment_key, user_id)
  return forced_variation_id if forced_variation_id

  # Check for saved bucketing decisions
   = (user_id)
  saved_variation_id = get_saved_variation_id(experiment_id, )
  if saved_variation_id
    @config.logger.log(
      Logger::INFO,
      "Returning previously activated variation ID #{saved_variation_id} of experiment '#{experiment_key}' for user '#{user_id}' from user profile."
    )
    return saved_variation_id
  end

  # Check audience conditions
  unless Audience.user_in_experiment?(@config, experiment_key, attributes)
    @config.logger.log(
      Logger::INFO,
      "User '#{user_id}' does not meet the conditions to be in experiment '#{experiment_key}'."
    )
    return nil
  end

  # Bucket normally
  variation_id = @bucketer.bucket(experiment_key, user_id)

  # Persist bucketing decision
  (, experiment_id, variation_id)
  variation_id
end