lib/vanity/experiment/ab_test.rb in vanity-2.1.0 vs lib/vanity/experiment/ab_test.rb in vanity-2.1.1

- old
+ new

@@ -56,11 +56,11 @@ # -- Enabled -- # Returns true if experiment is enabled, false if disabled. def enabled? - !@playground.collecting? || ( active? && connection.is_experiment_enabled?(@id) ) + !@playground.collecting? || (active? && connection.is_experiment_enabled?(@id)) end # Enable or disable the experiment. Only works if the playground is collecting # and this experiment is enabled. # @@ -185,14 +185,14 @@ # color = experiment(:which_blue).choose def choose(request=nil) if @playground.collecting? if active? if enabled? - index = alternative_index_for_identity(request) + return assignment_for_identity(request) else # Show the default if experiment is disabled. - index = alternatives.index(default) + return default end else # If inactive, always show the outcome. Fallback to generation if one can't be found. index = connection.ab_get_outcome(@id) || alternative_for(identity) end @@ -231,11 +231,11 @@ if @playground.collecting? if value.nil? connection.ab_not_showing @id, identity else index = @alternatives.index(value) - save_assignment_if_valid_visitor(identity, index, request) + save_assignment(identity, index, request) unless filter_visitor?(request, identity) raise ArgumentError, "No alternative #{value.inspect} for #{name}" unless index if (connection.ab_showing(@id, identity) && connection.ab_showing(@id, identity) != index) || alternative_for(identity) != index connection.ab_show(@id, identity, index) @@ -586,18 +586,22 @@ end end # Returns the assigned alternative, previously chosen alternative, or # alternative_for for a given identity. - def alternative_index_for_identity(request) + def assignment_for_identity(request) identity = 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? + if filter_visitor?(request, identity) + default + else + index = connection.ab_showing(@id, identity) || connection.ab_assigned(@id, identity) + unless index + index = alternative_for(identity).to_i + save_assignment(identity, index, request) unless @playground.using_js? + end + alternatives[index.to_i] end - index end # Chooses an alternative for the identity and returns its index. This # method always returns the same alternative for a given experiment and # identity, and randomly distributed alternatives for each identity (in the @@ -616,22 +620,23 @@ end # Saves the assignment of an alternative to a person and performs the # necessary housekeeping. Ignores repeat identities and filters using # Playground#request_filter. - def save_assignment_if_valid_visitor(identity, index, request) - return if index == connection.ab_showing(@id, identity) || filter_visitor?(request) + def save_assignment(identity, index, request) + return if index == connection.ab_showing(@id, identity) call_on_assignment_if_available(identity, index) rebalance_if_necessary! connection.ab_add_participant(@id, index, identity) check_completion! end - def filter_visitor?(request) - @playground.request_filter.call(request) + def filter_visitor?(request, identity) + @playground.request_filter.call(request) || + (@request_filter_block && @request_filter_block.call(request, identity)) end def call_on_assignment_if_available(identity, index) # if we have an on_assignment block, call it on new assignments if @on_assignment_block @@ -662,12 +667,12 @@ sum_of_probability = @alternatives.values.reduce(0) { |a,b| a+b } cumulative_probability = 0.0 @use_probabilities = [] result = [] @alternatives = @alternatives.each_with_index.map do |(value, probability), i| - result << alternative = Alternative.new( self, i, value ) + result << alternative = Alternative.new(self, i, value) probability = probability.to_f / sum_of_probability - @use_probabilities << [ alternative, cumulative_probability += probability ] + @use_probabilities << [alternative, cumulative_probability += probability] value end result end