Skip to content

Commit

Permalink
Merge pull request #3551 from alphagov/contents-list-AB-test-v2
Browse files Browse the repository at this point in the history
Add AB test for Contents list
  • Loading branch information
hannako authored Feb 10, 2025
2 parents a718ddf + e596e43 commit 96d0c24
Show file tree
Hide file tree
Showing 4 changed files with 122 additions and 5 deletions.
10 changes: 10 additions & 0 deletions app/controllers/content_items_controller.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
require "slimmer/headers"

class ContentItemsController < ApplicationController
include ContentsListAbTestable
include GovukPersonalisation::ControllerConcern
include Slimmer::Headers
include Slimmer::Template
Expand All @@ -15,6 +16,9 @@ class ContentItemsController < ApplicationController
rescue_from PresenterBuilder::SpecialRouteReturned, with: :error_notfound
rescue_from PresenterBuilder::GovernmentReturned, with: :error_notfound

helper_method :contents_list_variant
helper_method :step_by_step_page_under_test?

attr_accessor :content_item, :taxonomy_navigation

content_security_policy do |p|
Expand All @@ -32,6 +36,10 @@ def show
elsif is_history_page?
show_history_page
else
if step_by_step_page_under_test?
set_contents_list_response_header
end

set_guide_draft_access_token if @content_item.is_a?(GuidePresenter)
render_template
end
Expand All @@ -57,6 +65,8 @@ def service_sign_in_options

private

helper_method :show_contents_list_ab_test?

def is_history_page?
@content_item.document_type == "history"
end
Expand Down
61 changes: 61 additions & 0 deletions app/helpers/contents_list_ab_testable.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
module ContentsListAbTestable
ALLOWED_VARIANTS = %w[A B Z].freeze

AB_TEST_PAGES = [
# Help paying for childcare
"/help-with-childcare-costs",
"/help-with-childcare-costs/free-childcare-and-education-for-3-to-4-year-olds",
"/help-with-childcare-costs/free-childcare-2-year-olds-claim-benefits",
"/help-with-childcare-costs/tax-credits",
"/help-with-childcare-costs/universal-credit",
"/help-with-childcare-costs/childcare-vouchers",
"/help-with-childcare-costs/support-while-you-study",
# What to do after someone dies
"/after-a-death",
"/after-a-death/when-a-death-is-reported-to-a-coroner",
"/after-a-death/death-abroad",
"/after-a-death/organisations-you-need-to-contact-and-tell-us-once",
"/after-a-death/report-without-tell-us-once",
"/after-a-death/arrange-the-funeral",
"/after-a-death/if-a-child-or-baby-dies",
"/after-a-death/bereavement-help-and-support",
# Become a sole trader
"/become-sole-trader",
"/become-sole-trader/choose-your-business-name",
"/become-sole-trader/register-sole-trader",
# Set up a private limited company
"/limited-company-formation",
"/limited-company-formation/choose-company-name",
"/limited-company-formation/company-address",
"/limited-company-formation/appoint-directors-and-company-secretaries",
"/limited-company-formation/shareholders",
"/limited-company-formation/memorandum-and-articles-of-association",
"/limited-company-formation/register-your-company",
"/limited-company-formation/add-corporation-tax-services-to-business-tax-account",
].freeze

def contents_list_test
@contents_list_test ||= GovukAbTesting::AbTest.new(
"ContentsList",
allowed_variants: ALLOWED_VARIANTS,
control_variant: "Z",
)
end

def contents_list_variant
contents_list_test.requested_variant(request.headers)
end

def set_contents_list_response_header
contents_list_variant.configure_response(response)
end

def show_contents_list_ab_test?
var_b = contents_list_variant.variant?("B")
step_by_step_page_under_test? && var_b
end

def step_by_step_page_under_test?
AB_TEST_PAGES.include? request.path
end
end
14 changes: 9 additions & 5 deletions app/views/content_items/guide.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
canonical_url: @content_item.canonical_url,
body: @content_item.has_parts? ? @content_item.current_part_body : nil
) %>

<%= contents_list_variant.analytics_meta_tag.html_safe if step_by_step_page_under_test? %>
<%= @requested_variant.analytics_meta_tag.html_safe if @requested_variant.present? %>
<% end %>

Expand All @@ -19,8 +19,12 @@

<div class="govuk-grid-row gem-print-columns-none">
<div class="govuk-grid-column-two-thirds">
<%= render 'govuk_publishing_components/components/title', { title: @content_item.content_title } %>
<% if @content_item.show_guide_navigation? %>
<% if show_contents_list_ab_test? %>
<%= render 'govuk_publishing_components/components/title', { title: @content_item.title } %>
<% else %>
<%= render 'govuk_publishing_components/components/title', { title: @content_item.content_title } %>
<% end %>
<% if show_contents_list_ab_test? || @content_item.show_guide_navigation? %>
<%= render "govuk_publishing_components/components/skip_link", {
text: t("guide.skip_contents"),
href: "#guide-contents"
Expand All @@ -33,7 +37,7 @@

<div class="govuk-grid-column-two-thirds govuk-!-margin-top-6" id="guide-contents">
<% if @content_item.has_parts? %>
<% if @content_item.show_guide_navigation? %>
<% if show_contents_list_ab_test? || @content_item.show_guide_navigation? %>
<%= render 'govuk_publishing_components/components/heading', heading_level: 1, font_size: 'l', margin_bottom: 6, text: @content_item.current_part_title %>
<% end %>
<%
Expand All @@ -50,7 +54,7 @@
<%= raw(@content_item.current_part_body) %>
<% end %>

<% if @content_item.show_guide_navigation? %>
<% if show_contents_list_ab_test? || @content_item.show_guide_navigation? %>
<%= render 'govuk_publishing_components/components/previous_and_next_navigation', @content_item.previous_and_next_navigation %>

<div class="responsive-bottom-margin">
Expand Down
42 changes: 42 additions & 0 deletions test/controllers/content_items_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -368,6 +368,48 @@ class ContentItemsControllerTest < ActionController::TestCase
assert_equal "true", @response.headers[Slimmer::Headers::REMOVE_SEARCH_HEADER]
end

test "Contents List AB test variant A" do
content_item = content_store_has_schema_example("guide", "guide-with-step-navs")
content_item["base_path"] = "/help-with-childcare-costs/support-while-you-study"
content_item["details"]["hide_chapter_navigation"] = true

stub_content_store_has_item(content_item["base_path"], content_item)

with_variant ContentsList: "A" do
get :show, params: { path: "help-with-childcare-costs/support-while-you-study" }
assert_response :success
assert_not response.body.include?("contents-list")
end
end

test "Contents List AB test variant B" do
content_item = content_store_has_schema_example("guide", "guide-with-step-navs")
content_item["base_path"] = "/help-with-childcare-costs/support-while-you-study"
content_item["details"]["hide_chapter_navigation"] = true

stub_content_store_has_item(content_item["base_path"], content_item)

with_variant ContentsList: "B" do
get :show, params: { path: "help-with-childcare-costs/support-while-you-study" }
assert_response :success
assert response.body.include?("contents-list")
end
end

test "Contents List AB test variant Z" do
content_item = content_store_has_schema_example("guide", "guide-with-step-navs")
content_item["base_path"] = "/help-with-childcare-costs/support-while-you-study"
content_item["details"]["hide_chapter_navigation"] = true

stub_content_store_has_item(content_item["base_path"], content_item)

with_variant ContentsList: "Z" do
get :show, params: { path: "help-with-childcare-costs/support-while-you-study" }
assert_response :success
assert_not response.body.include?("contents-list")
end
end

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

0 comments on commit 96d0c24

Please sign in to comment.