Class: GovukAbTesting::RequestedVariant
- Inherits:
-
Object
- Object
- GovukAbTesting::RequestedVariant
- Defined in:
- lib/govuk_ab_testing/requested_variant.rb
Instance Attribute Summary collapse
-
#ab_test ⇒ Object
readonly
Returns the value of attribute ab_test.
-
#request_headers ⇒ Object
readonly
Returns the value of attribute request_headers.
Instance Method Summary collapse
-
#analytics_meta_tag ⇒ String
HTML meta tag used to track the results of your experiment.
-
#configure_response(response) ⇒ Object
Configure the response.
-
#initialize(ab_test, request_headers) ⇒ RequestedVariant
constructor
‘request.headers` in the controller.
-
#variant?(name) ⇒ Boolean
Check if the user should be served a specific variant.
-
#variant_a? ⇒ Boolean
If the user is to be served variant A.
-
#variant_b? ⇒ Boolean
If the user is to be served variant B.
-
#variant_name ⇒ String
Get the bucket this user is in.
Constructor Details
#initialize(ab_test, request_headers) ⇒ RequestedVariant
‘request.headers` in the controller.
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_test ⇒ Object (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_headers ⇒ Object (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_tag ⇒ String
HTML meta tag used to track the results of your experiment
62 63 64 65 66 67 68 69 70 |
# File 'lib/govuk_ab_testing/requested_variant.rb', line 62 def tag = <<~HTML <meta name="govuk:ab-test" content="#{ab_test.}:#{variant_name}" data-allowed-variants="#{ab_test.allowed_variants.join(',')}"> HTML tag.gsub(/\n/, "") end |
#configure_response(response) ⇒ Object
Configure the response
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
43 44 45 46 47 48 49 50 |
# File 'lib/govuk_ab_testing/requested_variant.rb', line 43 def variant?(name) = "Invalid variant name '#{name}'. Allowed variants are: #{ab_test.allowed_variants}" raise 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.
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.
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_name ⇒ String
Get the bucket this user is in
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 |