Class: EasyAb::Experiment
- Inherits:
-
Object
- Object
- EasyAb::Experiment
- Defined in:
- lib/easy_ab/experiment.rb
Instance Attribute Summary collapse
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#rules ⇒ Object
readonly
Returns the value of attribute rules.
-
#scope ⇒ Object
readonly
Returns the value of attribute scope.
-
#variants ⇒ Object
readonly
Returns the value of attribute variants.
-
#weights ⇒ Object
readonly
Returns the value of attribute weights.
-
#winner ⇒ Object
readonly
Returns the value of attribute winner.
Class Method Summary collapse
Instance Method Summary collapse
-
#assign_variant(user_recognition, options = {}) ⇒ Object
Priority: 1.
- #equal_weighted_variant ⇒ Object
-
#find_grouping_by_user_recognition(user_recognition) ⇒ Object
TODO: add spec.
- #flexible_variant(contexted_rules = nil) ⇒ Object
- #groupings ⇒ Object
-
#initialize(name, options = {}) ⇒ Experiment
constructor
A new instance of Experiment.
- #variant_by_rule(contexted_rules) ⇒ Object
- #weighted_variant ⇒ Object
Constructor Details
#initialize(name, options = {}) ⇒ Experiment
Returns a new instance of Experiment.
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, = {}) @name = name.to_s @variants = [:variants].map(&:to_s) @weights = [:weights] @rules = [:rules] @scope = [:scope] @winner = [:winner].nil? ? nil : [: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
#name ⇒ Object (readonly)
Returns the value of attribute name.
3 4 5 |
# File 'lib/easy_ab/experiment.rb', line 3 def name @name end |
#rules ⇒ Object (readonly)
Returns the value of attribute rules.
3 4 5 |
# File 'lib/easy_ab/experiment.rb', line 3 def rules @rules end |
#scope ⇒ Object (readonly)
Returns the value of attribute scope.
3 4 5 |
# File 'lib/easy_ab/experiment.rb', line 3 def scope @scope end |
#variants ⇒ Object (readonly)
Returns the value of attribute variants.
3 4 5 |
# File 'lib/easy_ab/experiment.rb', line 3 def variants @variants end |
#weights ⇒ Object (readonly)
Returns the value of attribute weights.
3 4 5 |
# File 'lib/easy_ab/experiment.rb', line 3 def weights @weights end |
#winner ⇒ Object (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
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:
-
winner
-
url parameter or assign variant (ex: ab_test(:experiment, variant: ‘variant A’))
-
scope
-
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, = {}) # 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 [:variant] && variants.include?([:variant].to_s) grouping.variant = [:variant].to_s else # 3. scope return nil if [:scope] && ![:scope].call # 4. rules/weights grouping.variant ||= flexible_variant([:contexted_rules]) return nil if grouping.variant.nil? end if grouping.changed? && ![: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_variant ⇒ Object
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 = user_recognition[:cookie].presence raise 'User not found: both user_id and cookie are empty' if user_id.nil? && .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) && (( && grouping. = ) || true) # Case IV return grouping if ( && grouping = groupings.where(user_id: nil, cookie: ).first) && ((grouping.user_id = user_id) || true) # Assign user_id elsif # User is not signed in # Case V return grouping if grouping = groupings.where(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 |
#groupings ⇒ Object
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_variant ⇒ Object
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 |