govuk_ab_testing 2.3.1 → 2.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: deb02ae4e64ef50d8e25636153612fafff21d485
4
- data.tar.gz: a9ffba9a8db8a463a011cd342616ed452b46ab7f
3
+ metadata.gz: 33722ac9662c30de4c5efa423f8de33da6341a78
4
+ data.tar.gz: 0c65ef65524a43ee0108e33c2be4599d83550386
5
5
  SHA512:
6
- metadata.gz: d9ce66cf58ec982769c9061ec3f20e62ed7e2789408ae88e409b584f72df46a5580835a30db44c3078c47fe291c0a4136826404506bb9a92884307b2f454c20b
7
- data.tar.gz: 74c8fc5a0d34a5a11630414ad828c47de97613c5c29768b22a5d4c8f7cb392899a2b79144a47dd97fdcfbee5407b83d4d8d98ed5102c98d2345b3ef9e1b6367d
6
+ metadata.gz: 0b89a6c500a941ec1ff9c099c96be9663b0628bb8df81ef74a107ae561300ba16750047587d1287cf02f0cca78b9a600f475ff2ffcf08b2eaeb79e02d19ac161
7
+ data.tar.gz: 7ef09915ff51828ad9843a44a8c2f01cb196d5edafe7fb4dcf4e5e86c29a86b09dfb80efdbbb616c7c10305c36cd7729ef748ba162f743fe3974c3f4d9325f2a
data/CHANGELOG.md CHANGED
@@ -1,3 +1,17 @@
1
+ ## 2.4.0
2
+
3
+ * Add two new optional parameters to `GovukAbTesting::AbTest`:
4
+ `allowed_variants` and `control_variant`. These allow us to override the
5
+ traditional naming of the A/B tests and also setup a multivariate test if
6
+ needed.
7
+ * Add new method to `RequestedVariant` to query if a user is in a given variant.
8
+ The method is `variant?(name)`, where `name` is the name of the variant.
9
+ This new method allows for generic variant names, which are useful when
10
+ reusing A/B testing cookies/headers. If the cookie name and header name are
11
+ generic, the variant value should provide details on what test is running.
12
+ * Add deprecation warnings to `variant_a?` and `variant_b?`, as they will be
13
+ replaced by the method `variant?(name)`.
14
+
1
15
  ## 2.3.1
2
16
 
3
17
  * Fix bug in order to allow us to set multiple headers (i.e. A/B tests) in a
data/README.md CHANGED
@@ -18,12 +18,9 @@ And then execute:
18
18
 
19
19
  ### Usage
20
20
 
21
- Before starting this, you'll need to
21
+ Before starting this, you'll need to:
22
22
 
23
- - get your cookie listed on [/help/cookies](https://www.gov.uk/help/cookies)
24
- - configure the CDN [like we did for the Education Navigation test](https://github.com/alphagov/govuk-cdn-config/pull/17).
25
- The cookie and header name in the CDN config must match the test name parameter
26
- that you pass to the Gem. The cookie name is case-sensitive.
23
+ - [read the documentation](https://docs.publishing.service.gov.uk/manual/ab-testing.html) on how to set up an a/b test. The cookie and header name in [fastly-configure](https://github.com/alphagov/fastly-configure) and [CDN config](https://github.digital.cabinet-office.gov.uk/gds/cdn-configs) must match the test name parameter that you pass to the Gem. The cookie name is case-sensitive.
27
24
  - configure Google Analytics (guidelines to follow)
28
25
 
29
26
  To enable testing in the app, your Rails app needs:
@@ -57,7 +54,7 @@ class PartyController < ApplicationController
57
54
  @requested_variant = ab_test.requested_variant(request.headers)
58
55
  @requested_variant.configure_response(response)
59
56
 
60
- if @requested_variant.variant_b?
57
+ if @requested_variant.variant?('B')
61
58
  render "new_show_template_to_be_tested"
62
59
  else
63
60
  render "show"
@@ -2,15 +2,22 @@ module GovukAbTesting
2
2
  class AbTest
3
3
  attr_reader :ab_test_name
4
4
  attr_reader :dimension
5
+ attr_reader :allowed_variants
6
+ attr_reader :control_variant
5
7
 
6
8
  alias_method :name, :ab_test_name
7
9
 
8
10
  # @param request [String] the name of the A/B test
9
11
  # @param dimension [Integer] the dimension registered with Google Analytics
10
12
  # for this specific A/B test
11
- def initialize(ab_test_name, dimension:)
13
+ # @param allowed_variants [Array] an array of Strings representing the
14
+ # possible variants
15
+ # @param control_variant [String] the control variant (typically 'A')
16
+ def initialize(ab_test_name, dimension:, allowed_variants: %w(A B), control_variant: 'A')
12
17
  @ab_test_name = ab_test_name
13
18
  @dimension = dimension
19
+ @allowed_variants = allowed_variants
20
+ @control_variant = control_variant
14
21
  end
15
22
 
16
23
  # @param request [ActionDispatch::Http::Headers] the `request.headers` in
@@ -17,19 +17,41 @@ module GovukAbTesting
17
17
  #
18
18
  # @return [String] the current variant, "A" or "B"
19
19
  def variant_name
20
- request_headers[ab_test.request_header] == "B" ? "B" : "A"
20
+ variant = ab_test.allowed_variants.find do |allowed_variant|
21
+ allowed_variant == request_headers[ab_test.request_header]
22
+ end
23
+
24
+ variant || ab_test.control_variant
21
25
  end
22
26
 
23
27
  # @return [Boolean] if the user is to be served variant A
24
28
  def variant_a?
29
+ warn 'DEPRECATION WARNING: the method `variant_a?` is deprecated. use `variant?("A")` instead'
30
+
25
31
  variant_name == "A"
26
32
  end
27
33
 
28
34
  # @return [Boolean] if the user is to be served variant B
29
35
  def variant_b?
36
+ warn 'DEPRECATION WARNING: the method `variant_b?` is deprecated. use `variant?("B")` instead'
37
+
30
38
  variant_name == "B"
31
39
  end
32
40
 
41
+ # Check if the user should be served a specific variant
42
+ #
43
+ # @param [String or Symbol] the name of the variant
44
+ #
45
+ # @return [Boolean] if the user is to be served variant :name
46
+ def variant?(name)
47
+ error_message =
48
+ "Invalid variant name '#{name}'. Allowed variants are: #{ab_test.allowed_variants}"
49
+
50
+ raise error_message unless ab_test.allowed_variants.include?(name.to_s)
51
+
52
+ variant_name == name.to_s
53
+ end
54
+
33
55
  # Configure the response
34
56
  #
35
57
  # @param [ApplicationController::Response] the `response` in the controller
@@ -43,7 +65,8 @@ module GovukAbTesting
43
65
  def analytics_meta_tag
44
66
  '<meta name="govuk:ab-test" ' +
45
67
  'content="' + ab_test.meta_tag_name + ':' + variant_name + '" ' +
46
- 'data-analytics-dimension="' + @dimension.to_s + '">'
68
+ 'data-analytics-dimension="' + @dimension.to_s + '" ' +
69
+ 'data-allowed-variants="' + ab_test.allowed_variants.join(',') + '">'
47
70
  end
48
71
  end
49
72
  end
@@ -1,3 +1,3 @@
1
1
  module GovukAbTesting
2
- VERSION = "2.3.1".freeze
2
+ VERSION = "2.4.0".freeze
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: govuk_ab_testing
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.3.1
4
+ version: 2.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - GOV.UK Dev
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2017-06-07 00:00:00.000000000 Z
11
+ date: 2017-06-12 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rake