Class: GovukAbTesting::RequestedVariant

Inherits:
Object
  • Object
show all
Defined in:
lib/govuk_ab_testing/requested_variant.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(ab_test, request_headers, dimension) ⇒ RequestedVariant

‘request.headers` in the controller. for this specific A/B test

Parameters:

  • ab_test (AbTest)

    the A/B test being performed

  • request_headers (ActionDispatch::Http::Headers)

    the

  • dimension (Integer)

    the dimension registered with Google Analytics



10
11
12
13
14
# File 'lib/govuk_ab_testing/requested_variant.rb', line 10

def initialize(ab_test, request_headers, dimension)
  @ab_test = ab_test
  @request_headers = request_headers
  @dimension = dimension
end

Instance Attribute Details

#ab_testObject (readonly)

Returns the value of attribute ab_test.



3
4
5
# File 'lib/govuk_ab_testing/requested_variant.rb', line 3

def ab_test
  @ab_test
end

#request_headersObject (readonly)

Returns the value of attribute request_headers.



3
4
5
# File 'lib/govuk_ab_testing/requested_variant.rb', line 3

def request_headers
  @request_headers
end

Instance Method Details

#analytics_meta_tagString

HTML meta tag used to track the results of your experiment

Returns:

  • (String)


65
66
67
68
69
70
71
72
73
74
# File 'lib/govuk_ab_testing/requested_variant.rb', line 65

def analytics_meta_tag
  tag = <<~HTML
    <meta name="govuk:ab-test"
      content="#{ab_test.meta_tag_name}:#{variant_name}"
      data-analytics-dimension="#{@dimension}"
      data-allowed-variants="#{ab_test.allowed_variants.join(',')}">
  HTML

  tag.gsub(/\n/, "")
end

#configure_response(response) ⇒ Object

Configure the response

Parameters:

  • the (ApplicationController::Response)

    ‘response` in the controller



58
59
60
# File 'lib/govuk_ab_testing/requested_variant.rb', line 58

def configure_response(response)
  response.headers["Vary"] = [response.headers["Vary"], ab_test.response_header].compact.join(", ")
end

#variant?(name) ⇒ Boolean

Check if the user should be served a specific variant

Parameters:

  • the (String or Symbol)

    name of the variant

Returns:

  • (Boolean)

    if the user is to be served variant :name



46
47
48
49
50
51
52
53
# File 'lib/govuk_ab_testing/requested_variant.rb', line 46

def variant?(name)
  error_message =
    "Invalid variant name '#{name}'. Allowed variants are: #{ab_test.allowed_variants}"

  raise error_message unless ab_test.allowed_variants.include?(name.to_s)

  variant_name == name.to_s
end

#variant_a?Boolean

Returns if the user is to be served variant A.

Returns:

  • (Boolean)

    if the user is to be served variant A



28
29
30
31
32
# File 'lib/govuk_ab_testing/requested_variant.rb', line 28

def variant_a?
  warn 'DEPRECATION WARNING: the method `variant_a?` is deprecated. use `variant?("A")` instead'

  variant_name == "A"
end

#variant_b?Boolean

Returns if the user is to be served variant B.

Returns:

  • (Boolean)

    if the user is to be served variant B



35
36
37
38
39
# File 'lib/govuk_ab_testing/requested_variant.rb', line 35

def variant_b?
  warn 'DEPRECATION WARNING: the method `variant_b?` is deprecated. use `variant?("B")` instead'

  variant_name == "B"
end

#variant_nameString

Get the bucket this user is in

Returns:

  • (String)

    the current variant, “A” or “B”



19
20
21
22
23
24
25
# File 'lib/govuk_ab_testing/requested_variant.rb', line 19

def variant_name
  variant = ab_test.allowed_variants.find do |allowed_variant|
    allowed_variant == request_headers[ab_test.request_header]
  end

  variant || ab_test.control_variant
end