Class: FieldTest::Experiment
- Inherits:
-
Object
- Object
- FieldTest::Experiment
- Defined in:
- lib/field_test/experiment.rb
Instance Attribute Summary collapse
-
#description ⇒ Object
readonly
Returns the value of attribute description.
-
#ended_at ⇒ Object
readonly
Returns the value of attribute ended_at.
-
#goals ⇒ Object
readonly
Returns the value of attribute goals.
-
#id ⇒ Object
readonly
Returns the value of attribute id.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#started_at ⇒ Object
readonly
Returns the value of attribute started_at.
-
#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
- #active? ⇒ Boolean
- #closed? ⇒ Boolean
- #control ⇒ Object
- #convert(participants, goal: nil) ⇒ Object
- #events ⇒ Object
-
#initialize(attributes) ⇒ Experiment
constructor
A new instance of Experiment.
- #keep_variant? ⇒ Boolean
- #memberships ⇒ Object
- #multiple_goals? ⇒ Boolean
- #results(goal: nil) ⇒ Object
- #use_events? ⇒ Boolean
- #variant(participants, options = {}) ⇒ Object
Constructor Details
#initialize(attributes) ⇒ Experiment
Returns a new instance of Experiment.
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
# File 'lib/field_test/experiment.rb', line 5 def initialize(attributes) attributes = attributes.symbolize_keys @id = attributes[:id] @name = attributes[:name] || @id.to_s.titleize @description = attributes[:description] @variants = attributes[:variants] @weights = @variants.size.times.map { |i| attributes[:weights].to_a[i] || 1 } @winner = attributes[:winner] @closed = attributes[:closed] @keep_variant = attributes[:keep_variant] @started_at = Time.zone.parse(attributes[:started_at].to_s) if attributes[:started_at] @ended_at = Time.zone.parse(attributes[:ended_at].to_s) if attributes[:ended_at] @goals = attributes[:goals] || ["conversion"] @goals_defined = !attributes[:goals].nil? @use_events = attributes[:use_events] end |
Instance Attribute Details
#description ⇒ Object (readonly)
Returns the value of attribute description.
3 4 5 |
# File 'lib/field_test/experiment.rb', line 3 def description @description end |
#ended_at ⇒ Object (readonly)
Returns the value of attribute ended_at.
3 4 5 |
# File 'lib/field_test/experiment.rb', line 3 def ended_at @ended_at end |
#goals ⇒ Object (readonly)
Returns the value of attribute goals.
3 4 5 |
# File 'lib/field_test/experiment.rb', line 3 def goals @goals end |
#id ⇒ Object (readonly)
Returns the value of attribute id.
3 4 5 |
# File 'lib/field_test/experiment.rb', line 3 def id @id end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
3 4 5 |
# File 'lib/field_test/experiment.rb', line 3 def name @name end |
#started_at ⇒ Object (readonly)
Returns the value of attribute started_at.
3 4 5 |
# File 'lib/field_test/experiment.rb', line 3 def started_at @started_at end |
#variants ⇒ Object (readonly)
Returns the value of attribute variants.
3 4 5 |
# File 'lib/field_test/experiment.rb', line 3 def variants @variants end |
#weights ⇒ Object (readonly)
Returns the value of attribute weights.
3 4 5 |
# File 'lib/field_test/experiment.rb', line 3 def weights @weights end |
#winner ⇒ Object (readonly)
Returns the value of attribute winner.
3 4 5 |
# File 'lib/field_test/experiment.rb', line 3 def winner @winner end |
Class Method Details
.all ⇒ Object
212 213 214 215 216 |
# File 'lib/field_test/experiment.rb', line 212 def self.all FieldTest.config["experiments"].map do |id, settings| FieldTest::Experiment.new(settings.merge(id: id.to_s)) end end |
.find(id) ⇒ Object
205 206 207 208 209 210 |
# File 'lib/field_test/experiment.rb', line 205 def self.find(id) experiment = all.index_by(&:id)[id.to_s] raise FieldTest::ExperimentNotFound unless experiment experiment end |
Instance Method Details
#active? ⇒ Boolean
181 182 183 |
# File 'lib/field_test/experiment.rb', line 181 def active? !winner end |
#closed? ⇒ Boolean
185 186 187 |
# File 'lib/field_test/experiment.rb', line 185 def closed? @closed end |
#control ⇒ Object
193 194 195 |
# File 'lib/field_test/experiment.rb', line 193 def control variants.first end |
#convert(participants, goal: nil) ⇒ Object
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 |
# File 'lib/field_test/experiment.rb', line 58 def convert(participants, goal: nil) return false if winner goal ||= goals.first participants = FieldTest::Participant.standardize(participants) check_participants(participants) membership = membership_for(participants) if membership if membership.respond_to?(:converted) membership.converted = true membership.save! if membership.changed? end if use_events? FieldTest::Event.create!( name: goal, field_test_membership_id: membership.id ) end true else false end end |
#events ⇒ Object
90 91 92 |
# File 'lib/field_test/experiment.rb', line 90 def events FieldTest::Event.joins(:field_test_membership).where(field_test_memberships: {experiment: id}) end |
#keep_variant? ⇒ Boolean
189 190 191 |
# File 'lib/field_test/experiment.rb', line 189 def keep_variant? @keep_variant end |
#memberships ⇒ Object
86 87 88 |
# File 'lib/field_test/experiment.rb', line 86 def memberships FieldTest::Membership.where(experiment: id) end |
#multiple_goals? ⇒ Boolean
94 95 96 |
# File 'lib/field_test/experiment.rb', line 94 def multiple_goals? goals.size > 1 end |
#results(goal: nil) ⇒ Object
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 |
# File 'lib/field_test/experiment.rb', line 98 def results(goal: nil) goal ||= goals.first relation = memberships.group(:variant) relation = relation.where("field_test_memberships.created_at >= ?", started_at) if started_at relation = relation.where("field_test_memberships.created_at <= ?", ended_at) if ended_at if use_events? && @goals_defined data = {} participated = relation.count adapter_name = relation.connection.adapter_name column = if FieldTest.legacy_participants :participant elsif adapter_name =~ /postg/i # postgres "(participant_type, participant_id)" elsif adapter_name =~ /mysql/i "participant_type, participant_id" else # not perfect, but it'll do "COALESCE(participant_type, '') || ':' || participant_id" end converted = events.merge(relation).where(field_test_events: {name: goal}).distinct.count(column) (participated.keys + converted.keys).uniq.each do |variant| data[[variant, true]] = converted[variant].to_i data[[variant, false]] = participated[variant].to_i - converted[variant].to_i end else data = relation.group(:converted).count end results = {} variants.each do |variant| converted = data[[variant, true]].to_i participated = converted + data[[variant, false]].to_i results[variant] = { participated: participated, converted: converted, conversion_rate: participated > 0 ? converted.to_f / participated : nil } end case variants.size when 1, 2, 3 total = 0.0 (variants.size - 1).times do |i| c = results.values[i] b = results.values[(i + 1) % variants.size] a = results.values[(i + 2) % variants.size] alpha_a = 1 + a[:converted] beta_a = 1 + a[:participated] - a[:converted] alpha_b = 1 + b[:converted] beta_b = 1 + b[:participated] - b[:converted] alpha_c = 1 + c[:converted] beta_c = 1 + c[:participated] - c[:converted] # TODO calculate this incrementally by caching intermediate results prob_winning = if variants.size == 2 cache_fetch ["field_test", "prob_b_beats_a", alpha_b, beta_b, alpha_c, beta_c] do Calculations.prob_b_beats_a(alpha_b, beta_b, alpha_c, beta_c) end else cache_fetch ["field_test", "prob_c_beats_a_and_b", alpha_a, beta_a, alpha_b, beta_b, alpha_c, beta_c] do Calculations.prob_c_beats_a_and_b(alpha_a, beta_a, alpha_b, beta_b, alpha_c, beta_c) end end results[variants[i]][:prob_winning] = prob_winning total += prob_winning end results[variants.last][:prob_winning] = 1 - total end results end |
#use_events? ⇒ Boolean
197 198 199 200 201 202 203 |
# File 'lib/field_test/experiment.rb', line 197 def use_events? if @use_events.nil? FieldTest.events_supported? else @use_events end end |
#variant(participants, options = {}) ⇒ Object
22 23 24 25 26 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 |
# File 'lib/field_test/experiment.rb', line 22 def variant(participants, = {}) return winner if winner && !keep_variant? return control if [:exclude] participants = FieldTest::Participant.standardize(participants) check_participants(participants) membership = membership_for(participants) || FieldTest::Membership.new(experiment: id) if winner # and keep_variant? return membership.variant || winner end if [:variant] && variants.include?([:variant]) membership.variant = [:variant] else membership.variant ||= closed? ? control : weighted_variant end participant = participants.first # upgrade to preferred participant membership.participant = participant.participant if membership.respond_to?(:participant=) membership.participant_type = participant.type if membership.respond_to?(:participant_type=) membership.participant_id = participant.id if membership.respond_to?(:participant_id=) if membership.changed? && (!closed? || membership.persisted?) begin membership.save! rescue ActiveRecord::RecordNotUnique membership = memberships.find_by(participant.where_values) end end membership.try(:variant) || control end |