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) ⇒ RequestedVariant

‘request.headers` in the controller.

Parameters:

  • ab_test (AbTest)

    the A/B test being performed

  • request_headers (ActionDispatch::Http::Headers)

    the



8
9
10
11
# File 'lib/govuk_ab_testing/requested_variant.rb', line 8

def initialize(ab_test, request_headers)
  @ab_test = ab_test
  @request_headers = request_headers
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)


62
63
64
65
66
67
68
69
70
# File 'lib/govuk_ab_testing/requested_variant.rb', line 62

def analytics_meta_tag
  tag = <<~HTML
    <meta name="govuk:ab-test"
      content="#{ab_test.meta_tag_name}:#{variant_name}"
      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



55
56
57
# File 'lib/govuk_ab_testing/requested_variant.rb', line 55

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



43
44
45
46
47
48
49
50
# File 'lib/govuk_ab_testing/requested_variant.rb', line 43

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



25
26
27
28
29
# File 'lib/govuk_ab_testing/requested_variant.rb', line 25

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



32
33
34
35
36
# File 'lib/govuk_ab_testing/requested_variant.rb', line 32

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”



16
17
18
19
20
21
22
# File 'lib/govuk_ab_testing/requested_variant.rb', line 16

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