Class: EasyAb::Experiment

Inherits:
Object
  • Object
show all
Defined in:
lib/easy_ab/experiment.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, options = {}) ⇒ Experiment

Returns a new instance of Experiment.

Raises:

  • (ArgumentError)


5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/easy_ab/experiment.rb', line 5

def initialize(name, options = {})
  @name     = name.to_s
  @variants = options[:variants].map(&:to_s)
  @weights  = options[:weights]
  @rules    = options[:rules]
  @scope    = options[:scope]
  @winner   = options[:winner].nil? ? nil : options[:winner].to_s

  raise ArgumentError, 'Please define variants' if @variants.blank?
  raise ArgumentError, 'Number of variants and weights should be identical' if @weights.present? && @weights.size != @variants.size
  raise ArgumentError, 'Number of variants and rules should be identical' if @rules.present? && @rules.size != @variants.size
  raise ArgumentError, 'All rules should be a Proc' if @rules.present? && @rules.any? { |rule| !rule.is_a?(Proc) }
  raise ArgumentError, 'Scope should be a Proc' if @scope.present? && !@scope.is_a?(Proc)
  raise ArgumentError, 'winner should be one of variants' if @winner && !@variants.include?(@winner)
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



3
4
5
# File 'lib/easy_ab/experiment.rb', line 3

def name
  @name
end

#rulesObject (readonly)

Returns the value of attribute rules.



3
4
5
# File 'lib/easy_ab/experiment.rb', line 3

def rules
  @rules
end

#scopeObject (readonly)

Returns the value of attribute scope.



3
4
5
# File 'lib/easy_ab/experiment.rb', line 3

def scope
  @scope
end

#variantsObject (readonly)

Returns the value of attribute variants.



3
4
5
# File 'lib/easy_ab/experiment.rb', line 3

def variants
  @variants
end

#weightsObject (readonly)

Returns the value of attribute weights.



3
4
5
# File 'lib/easy_ab/experiment.rb', line 3

def weights
  @weights
end

#winnerObject (readonly)

Returns the value of attribute winner.



3
4
5
# File 'lib/easy_ab/experiment.rb', line 3

def winner
  @winner
end

Class Method Details

.find_by_name!(experiment_name) ⇒ Object

Raises:



21
22
23
24
25
26
# File 'lib/easy_ab/experiment.rb', line 21

def self.find_by_name!(experiment_name)
  experiment_name = experiment_name.to_s
  exp = EasyAb.experiments.all.find { |exp| exp.name == experiment_name }
  raise ExperimentNotFound if exp.nil?
  exp
end

Instance Method Details

#assign_variant(user_recognition, options = {}) ⇒ Object

Priority:

  1. winner

  2. url parameter or assign variant (ex: ab_test(:experiment, variant: ‘variant A’))

  3. scope

  4. rules/weights



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
# File 'lib/easy_ab/experiment.rb', line 33

def assign_variant(user_recognition, options = {})
  # 1. winner
  return winner if winner

  grouping = find_grouping_by_user_recognition(user_recognition) || ::EasyAb::Grouping.new(experiment: name, user_id: user_recognition[:user_id], cookie: user_recognition[:cookie])

  # 2. url parameter or assign variant
  if options[:variant] && variants.include?(options[:variant].to_s)
    grouping.variant = options[:variant].to_s
  else
    # 3. scope
    return nil if options[:scope] && !options[:scope].call
    # 4. rules/weights
    grouping.variant ||= flexible_variant(options[:contexted_rules])
    return nil if grouping.variant.nil?
  end

  if grouping.changed? && !options[:skip_save]
    begin
      grouping.save!
    rescue ActiveRecord::RecordNotUnique
      grouping = find_grouping_by_user_recognition(user_recognition)
    rescue ActiveRecord::RecordInvalid => e
      if grouping.errors[:user_id].present? || grouping.errors[:cookie].present?
        grouping = find_grouping_by_user_recognition(user_recognition)
      else
        raise e
      end
    end
  end

  grouping.variant
end

#equal_weighted_variantObject



132
133
134
# File 'lib/easy_ab/experiment.rb', line 132

def equal_weighted_variant
  variants.sample
end

#find_grouping_by_user_recognition(user_recognition) ⇒ Object

TODO: add spec



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
93
94
95
96
# File 'lib/easy_ab/experiment.rb', line 68

def find_grouping_by_user_recognition(user_recognition)
  user_id  = user_recognition[:user_id].presence
  cookie   = user_recognition[:cookie].presence
  raise 'User not found: both user_id and cookie are empty' if user_id.nil? && cookie.nil?

  # Cases should take into consideration
  # Case I: user participated experiment with login and return again
  # Case II: user participated experiment with login and return by another device with login
  # Case III: user participated experiment with login and return by the same device, but cookie was cleared between last and this participation
  # => Both II and III already exist a record with the same user_id but different cookie
  #    In the above two cases, we update the cookie of the exising record
  #
  # Case IV: User participated experiment without login and return with login
  # => Assign user_id to the existing record
  #
  # Case V: User participated experiment without login and return without login, too
  grouping = nil
  if user_id # User is signed in
    # Case I, II, III
    return grouping if (grouping = groupings.where(user_id: user_id).first) && ((cookie && grouping.cookie = cookie) || true)
    # Case IV
    return grouping if (cookie && grouping = groupings.where(user_id: nil, cookie: cookie).first) && ((grouping.user_id = user_id) || true) # Assign user_id
  elsif cookie # User is not signed in
    # Case V
    return grouping if grouping = groupings.where(cookie: cookie).first
  end

  grouping
end

#flexible_variant(contexted_rules = nil) ⇒ Object



102
103
104
105
106
107
108
109
110
# File 'lib/easy_ab/experiment.rb', line 102

def flexible_variant(contexted_rules = nil)
  if contexted_rules
    variant_by_rule(contexted_rules)
  elsif weights
    weighted_variant
  else
    equal_weighted_variant
  end
end

#groupingsObject



98
99
100
# File 'lib/easy_ab/experiment.rb', line 98

def groupings
  ::EasyAb::Grouping.where(experiment: name)
end

#variant_by_rule(contexted_rules) ⇒ Object



112
113
114
115
116
117
118
119
# File 'lib/easy_ab/experiment.rb', line 112

def variant_by_rule(contexted_rules)
  contexted_rules.each_with_index do |rule, i|
    return variants[i] if rule.call
  end

  # If all rules not matched, return nil
  nil
end

#weighted_variantObject



121
122
123
124
125
126
127
128
129
130
# File 'lib/easy_ab/experiment.rb', line 121

def weighted_variant
  total = weights.sum
  roll = rand
  sum = 0
  weights.each_with_index do |weight, i|
    sum += weight.to_d / total
    return variants[i] if sum >= roll
  end
  variants.last
end