Module: Split::Helper

Defined in:
lib/split/helper.rb

Instance Method Summary collapse

Instance Method Details

#ab_test(experiment_name, *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
# File 'lib/split/helper.rb', line 3

def ab_test(experiment_name, *alternatives)
  experiment = Split::Experiment.find_or_create(experiment_name, *alternatives)
  if experiment.winner
    ret = experiment.winner.name
  else
    if forced_alternative = override(experiment.name, alternatives)
      ret = forced_alternative
    else
      begin_experiment(experiment, experiment.control.name) if exclude_visitor?

      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

  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
rescue Errno::ECONNREFUSED => e
  raise unless Split.configuration.db_failover
  Split.configuration.db_failover_on_db_error.call(e)
  ret = Hash === (a0 = alternatives.first) ? a0.keys.first : a0
  yield(ret) if block_given?
  ret
end

#ab_userObject



64
65
66
# File 'lib/split/helper.rb', line 64

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

#begin_experiment(experiment, alternative_name) ⇒ Object



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

def begin_experiment(experiment, alternative_name)
  ab_user[experiment.key] = alternative_name
end

#exclude_visitor?Boolean

Returns:

  • (Boolean)


68
69
70
# File 'lib/split/helper.rb', line 68

def exclude_visitor?
  is_robot? or is_ignored_ip_address?
end

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



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

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)


76
77
78
79
80
81
82
# File 'lib/split/helper.rb', line 76

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)


72
73
74
# File 'lib/split/helper.rb', line 72

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

#override(experiment_name, alternatives) ⇒ Object



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

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