Module: Split::Helper

Defined in:
lib/split/helper.rb

Instance Method Summary collapse

Instance Method Details

#ab_test(experiment_name, control, *alternatives) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/split/helper.rb', line 3

def ab_test(experiment_name, control, *alternatives)
  puts 'WARNING: You should always pass the control alternative through as the second argument with any other alternatives as the third because the order of the hash is not preserved in ruby 1.8' if RUBY_VERSION.match(/1\.8/) && alternatives.length.zero?
  begin
    experiment = Split::Experiment.find_or_create(experiment_name, *([control] + alternatives))
    if experiment.winner
      ret = experiment.winner.name
    else
      if forced_alternative = override(experiment.name, experiment.alternative_names)
        ret = forced_alternative
      else
        clean_old_versions(experiment)
        begin_experiment(experiment) if exclude_visitor? or not_allowed_to_test?(experiment.key)

        if ab_user[experiment.key] 
          ret = ab_user[experiment.key]
        else
          alternative = experiment.next_alternative
          alternative.increment_participation
          begin_experiment(experiment, alternative.name)
          ret = alternative.name
        end
      end
    end
  rescue Errno::ECONNREFUSED => e
    raise unless Split.configuration.db_failover
    Split.configuration.db_failover_on_db_error.call(e)
    ret = Hash === control ? control.keys.first : control
  end
  if block_given?
    if defined?(capture) # a block in a rails view
      block = Proc.new { yield(ret) }
      concat(capture(ret, &block))
      false
    else
      yield(ret)
    end
  else
    ret
  end
end

#ab_userObject



66
67
68
# File 'lib/split/helper.rb', line 66

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

#begin_experiment(experiment, alternative_name = nil) ⇒ Object



61
62
63
64
# File 'lib/split/helper.rb', line 61

def begin_experiment(experiment, alternative_name = nil)
  alternative_name ||= experiment.control.name
  ab_user[experiment.key] = alternative_name
end

#clean_old_versions(experiment) ⇒ Object



82
83
84
85
86
# File 'lib/split/helper.rb', line 82

def clean_old_versions(experiment)
  old_versions(experiment).each do |old_key|
    ab_user.delete old_key
  end
end

#doing_other_tests?(experiment_key) ⇒ Boolean

Returns:

  • (Boolean)


78
79
80
# File 'lib/split/helper.rb', line 78

def doing_other_tests?(experiment_key)
  ab_user.keys.reject{|k| k == experiment_key}.length > 0
end

#exclude_visitor?Boolean

Returns:

  • (Boolean)


70
71
72
# File 'lib/split/helper.rb', line 70

def exclude_visitor?
  is_robot? or is_ignored_ip_address?
end

#finished(experiment_name, options = {:reset => true}) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/split/helper.rb', line 44

def finished(experiment_name, options = {:reset => true})
  return if exclude_visitor?
  return unless (experiment = Split::Experiment.find(experiment_name))
  if alternative_name = ab_user[experiment.key]
    alternative = Split::Alternative.new(alternative_name, experiment_name)
    alternative.increment_completion
    session[:split].delete(experiment_name) if options[:reset]
  end
rescue Errno::ECONNREFUSED => e
  raise unless Split.configuration.db_failover
  Split.configuration.db_failover_on_db_error.call(e)
end

#is_ignored_ip_address?Boolean

Returns:

  • (Boolean)


100
101
102
103
104
105
106
# File 'lib/split/helper.rb', line 100

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

#is_robot?Boolean

Returns:

  • (Boolean)


96
97
98
# File 'lib/split/helper.rb', line 96

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

#not_allowed_to_test?(experiment_key) ⇒ Boolean

Returns:

  • (Boolean)


74
75
76
# File 'lib/split/helper.rb', line 74

def not_allowed_to_test?(experiment_key)
  !Split.configuration.allow_multiple_experiments && doing_other_tests?(experiment_key)
end

#old_versions(experiment) ⇒ Object



88
89
90
91
92
93
94
# File 'lib/split/helper.rb', line 88

def old_versions(experiment)
  if experiment.version > 0
    ab_user.keys.select{|k| k.match(Regexp.new(experiment.name))}.reject{|k| k == experiment.key}
  else
    []
  end
end

#override(experiment_name, alternatives) ⇒ Object



57
58
59
# File 'lib/split/helper.rb', line 57

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