Module: SimpleAbs

Defined in:
lib/simple_abs.rb,
lib/simple_abs/version.rb

Defined Under Namespace

Classes: Alternative, Railtie

Constant Summary collapse

VERSION =
"0.0.2"

Instance Method Summary collapse

Instance Method Details

#ab_test(name, tests) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/simple_abs.rb', line 16

def ab_test(name, tests)
  
  if is_bot?
    test_value = tests[rand(tests.size)]
    return test_value
  end
  
  if params[:test_value]
    return params[:test_value]
  end
  
  test_value = cookies[name]
  
  if test_value.blank? || !tests.include?(test_value)
    test_value = tests[rand(tests.size)]
    cookies.permanent[name] = test_value
    
    find_or_create_by_experiment_and_which_method(name, test_value).increment!(:participants)
  end
  
  return test_value
end

#converted!(name) ⇒ Object



39
40
41
42
43
44
45
46
47
48
# File 'lib/simple_abs.rb', line 39

def converted!(name)

  if !is_bot?
    test_value = cookies[name]
    if test_value && cookies[name.to_s + "_converted"].blank?
      find_or_create_by_experiment_and_which_method(name, test_value).increment!(:conversions)
      cookies.permanent[name.to_s + "_converted"] = true
    end
  end
end

#find_or_create_by_experiment_and_which_method(experiment, which) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/simple_abs.rb', line 50

def find_or_create_by_experiment_and_which_method(experiment, which)
  alternative = Alternative.where(experiment: experiment, which: which).first

  if alternative.nil?
    alternative = Alternative.new
    alternative.experiment = experiment
    alternative.which = which
    alternative.save
  end

  return alternative

end

#is_bot?Boolean

Returns:



5
6
7
8
9
10
11
12
13
14
# File 'lib/simple_abs.rb', line 5

def is_bot?
  agent = request.env["HTTP_USER_AGENT"]
  matches = nil
  matches = agent.match(/(facebook|postrank|voyager|twitterbot|googlebot|slurp|butterfly|pycurl|tweetmemebot|metauri|evrinid|reddit|digg)/mi) if agent
  if (agent.nil? or matches)
    return true
  else
    return false
  end
end