Sha256: bd40b3b623d709a8bad988ca5a62ec06aea57b2e5fe86f7adbf32414a4353f21

Contents?: true

Size: 1.54 KB

Versions: 1

Compression:

Stored size: 1.54 KB

Contents

module Split
  module Helper
    def ab_test(experiment_name, *alternatives)
      experiment = Split::Experiment.find_or_create(experiment_name, *alternatives)
      return experiment.winner.name if experiment.winner

      if forced_alternative = override(experiment_name, alternatives)
        return forced_alternative
      end

      ab_user[experiment_name] = experiment.control.name if exclude_visitor?

      if ab_user[experiment_name]
        return ab_user[experiment_name]
      else
        alternative = experiment.next_alternative
        alternative.increment_participation
        ab_user[experiment_name] = alternative.name
        return alternative.name
      end
    end

    def finished(experiment_name)
      return if exclude_visitor?
      alternative_name = ab_user[experiment_name]
      alternative = Split::Alternative.find(alternative_name, experiment_name)
      alternative.increment_completion
      session[:split].delete(experiment_name)
    end

    def override(experiment_name, alternatives)
      return params[experiment_name] if defined?(params) && alternatives.include?(params[experiment_name])
    end

    def ab_user
      session[:split] ||= {}
    end

    def exclude_visitor?
      is_robot? or is_ignored_ip_address?
    end

    def is_robot?
      request.user_agent =~ Split.configuration.robot_regex
    end

    def is_ignored_ip_address?
      if Split.configuration.ignore_ip_addresses.any?
        Split.configuration.ignore_ip_addresses.include?(request.ip)
      else
        false
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
split-0.2.2 lib/split/helper.rb