Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AB test - replace some content on UTR page #2947

Merged
merged 9 commits into from
Oct 18, 2023
29 changes: 29 additions & 0 deletions app/controllers/content_items_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,35 @@ class ContentItemsController < ApplicationController
def show
load_content_item

# TEMPORARY (author: richard.towers, expected end date: November 30 2023)
# Content specific AB test for the Find your UTR number page
if @content_item.base_path == "/find-utr-number"
ab_test = GovukAbTesting::AbTest.new(
"find_utr_number_video_links",
dimension: 300, # TODO: which dimension should we use?
allowed_variants: %w[HelpText VideoLink],
richardTowers marked this conversation as resolved.
Show resolved Hide resolved
control_variant: "HelpText",
)
@requested_variant = ab_test.requested_variant(request.headers)
@requested_variant.configure_response(response)

if @requested_variant.variant? "VideoLink"
# NOTE: this is a fragile way of doing an AB test on content.
#
# Any change to the base content, or to the way the content is rendered
# could potentially break the B variant of the test, and result in both
# variants being the same.
# We're aware of this risk, and we're going to be careful in this one off
# situation. This is not a sustainable way of doing AB tests in the
# future.
@content_item.body.sub!(
richardTowers marked this conversation as resolved.
Show resolved Hide resolved
/<li>\s*in\ the\s+<a\ href="[^"]*"><abbr\ title="[^"]+">HMRC<\/abbr>\s+app<\/a>\s*<\/li>/,
'<li>in the <a href="https://www.gov.uk/guidance/download-the-hmrc-app"><abbr title="HM Revenue and Customs">HMRC</abbr> app</a> - watch a <a href="https://www.youtube.com/watch?v=LXw9ily9rTo">video about finding your UTR number in the app</a>',
richardTowers marked this conversation as resolved.
Show resolved Hide resolved
)
end
end
# /TEMPORARY

set_expiry

if is_service_manual?
Expand Down
15 changes: 15 additions & 0 deletions test/controllers/content_items_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -364,6 +364,21 @@ class ContentItemsControllerTest < ActionController::TestCase
assert_equal "true", @response.headers[Slimmer::Headers::REMOVE_SEARCH_HEADER]
end

test "AB test replaces content on the find-utr-number page" do
content_item = content_store_has_schema_example("answer", "answer")
content_item["base_path"] = "/find-utr-number"
content_item["details"]["body"] = "<li>in the <a href=\"\"><abbr title=\"HM Revenue and Customs\">HMRC</abbr> app</a>\n</li>"
content_item["locale"] = "en"

stub_content_store_has_item(content_item["base_path"], content_item)

request.headers["HTTP_GOVUK_ABTEST_FIND_UTR_NUMBER_VIDEO_LINKS"] = "VideoLink"

get :show, params: { path: path_for(content_item) }
assert_response :success
assert_match 'watch a <a href="https://www.youtube.com/watch?v=LXw9ily9rTo">video about finding your UTR number in the app</a>', response.body
end

def path_for(content_item, locale = nil)
base_path = content_item["base_path"].sub(/^\//, "")
base_path.gsub!(/\.#{locale}$/, "") if locale
Expand Down