lib/vanity/experiment/ab_test.rb in vanity-2.0.0.beta7 vs lib/vanity/experiment/ab_test.rb in vanity-2.0.0.beta8
- old
+ new
@@ -57,26 +57,16 @@
#
# @example Find out which alternatives this test uses
# alts = experiment(:background_color).alternatives
# puts "#{alts.count} alternatives, with the colors: #{alts.map(&:value).join(", ")}"
def alternatives(*args)
- @alternatives = args.empty? ? [true, false] : args.clone
- class << self
- define_method :alternatives, instance_method(:_alternatives)
+ @alternatives ||= args.empty? ? [true, false] : args.clone
+ @alternatives.each_with_index.map do |value, i|
+ Alternative.new(self, i, value)
end
- nil
end
- def _alternatives
- alts = []
- @alternatives.each_with_index do |value, i|
- alts << Alternative.new(self, i, value)
- end
- alts
- end
- private :_alternatives
-
# Returns an Alternative with the specified value.
#
# @example
# alternative(:red) == alternatives[0]
# alternative(:blue) == alternatives[2]
@@ -135,11 +125,11 @@
# color = experiment(:which_blue).choose
def choose(request=nil)
if @playground.collecting?
if active?
identity = identity()
- index = connection.ab_showing(@id, identity)
+ index = connection.ab_showing(@id, identity) || connection.ab_assigned(@id, identity)
unless index
index = alternative_for(identity).to_i
save_assignment_if_valid_visitor(identity, index, request) unless @playground.using_js?
end
else
@@ -149,10 +139,11 @@
identity = identity()
@showing ||= {}
@showing[identity] ||= alternative_for(identity)
index = @showing[identity]
end
+
alternatives[index.to_i]
end
# -- Testing and JS Callback --
@@ -412,11 +403,11 @@
# Alternative chosen when this experiment completed.
def outcome
return unless @playground.collecting?
outcome = connection.ab_get_outcome(@id)
- outcome && _alternatives[outcome]
+ outcome && alternatives[outcome]
end
def complete!(outcome = nil)
return unless @playground.collecting? && active?
super
@@ -443,11 +434,11 @@
def destroy
connection.destroy_experiment @id
super
end
-
+
# clears all collected data for the experiment
def reset
connection.destroy_experiment @id
connection.set_experiment_created_at @id, Time.now
@outcome = @completed_at = nil
@@ -469,16 +460,16 @@
end
# Called via a hook by the associated metric.
def track!(metric_id, timestamp, count, *args)
return unless active?
- identity = identity() rescue nil
- identity ||= args.last[:identity] if args.last.is_a?(Hash) && args.last[:identity]
+ identity = args.last[:identity] if args.last.is_a?(Hash)
+ identity ||= identity() rescue nil
if identity
return if connection.ab_showing(@id, identity)
index = alternative_for(identity)
- connection.ab_add_conversion @id, index, identity, count
+ connection.ab_add_conversion(@id, index, identity, count)
check_completion!
end
end
# If you are not embarrassed by the first version of your product, you’ve
@@ -515,10 +506,11 @@
random_outcome = rand()
@use_probabilities.each do |alternative, max_prob|
return alternative.id if random_outcome < max_prob
end
end
- return Digest::MD5.hexdigest("#{name}/#{identity}").to_i(17) % @alternatives.size
+
+ Digest::MD5.hexdigest("#{name}/#{identity}").to_i(17) % @alternatives.size
end
# Saves the assignment of an alternative to a person and performs the
# necessary housekeeping. Ignores repeat identities and filters using
# Playground#request_filter.