Module: FieldTest::Helpers

Included in:
Controller, Mailer
Defined in:
lib/field_test/helpers.rb

Instance Method Summary collapse

Instance Method Details

#field_test(experiment, **options) ⇒ 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
# File 'lib/field_test/helpers.rb', line 3

def field_test(experiment, **options)
  exp = FieldTest::Experiment.find(experiment)

  participants = FieldTest::Participant.standardize(options[:participant] || field_test_participant)

  if try(:request)
    options = options.dup

    if !options[:variant] && params[:field_test] && params[:field_test][experiment] && exp.variants.include?(params[:field_test][experiment])
      params_variant = params[:field_test][experiment]
    end

    if FieldTest.exclude_bots?
      options[:exclude] = Browser.new(request.user_agent).bot?
    end

    options[:exclude] ||= FieldTest.excluded_ips.any? { |ip| ip.include?(request.remote_ip) }

    options[:ip] = request.remote_ip
    options[:user_agent] = request.user_agent
  end

  # don't update variant when passed via params
  if params_variant
    params_variant
  else
    # cache results for request
    # TODO possibly remove in 0.4.0
    cache_key = [exp.id, participants.map(&:where_values), options.slice(:variant, :exclude)]
    @field_test_cache ||= {}
    @field_test_cache[cache_key] ||= exp.variant(participants, options)
  end
end

#field_test_converted(experiment, **options) ⇒ Object



37
38
39
40
41
42
43
# File 'lib/field_test/helpers.rb', line 37

def field_test_converted(experiment, **options)
  exp = FieldTest::Experiment.find(experiment)

  participants = FieldTest::Participant.standardize(options[:participant] || field_test_participant)

  exp.convert(participants, goal: options[:goal])
end

#field_test_experiments(**options) ⇒ Object

TODO fetch in single query



46
47
48
49
50
51
52
53
54
55
# File 'lib/field_test/helpers.rb', line 46

def field_test_experiments(**options)
  participants = FieldTest::Participant.standardize(options[:participant] || field_test_participant)
  experiments = {}
  participants.each do |participant|
    FieldTest::Membership.where(participant.where_values).each do |membership|
      experiments[membership.experiment] ||= membership.variant
    end
  end
  experiments
end