Skip to content

Commit

Permalink
Merge pull request #3609 from alphagov/browse_ab_test
Browse files Browse the repository at this point in the history
Set up AB test logic for NewBrowse
  • Loading branch information
hannako committed May 13, 2024
2 parents c42dfa9 + fcc407c commit 9a51fda
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 1 deletion.
4 changes: 3 additions & 1 deletion app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
class ApplicationController < ActionController::Base
include Slimmer::Template
include NewBrowseAbTestable

helper_method :level_two_browse_variant
helper_method :new_browse_variant
helper_method :content_item_h
helper_method :new_browse_page_under_test?

protect_from_forgery with: :exception

Expand Down
2 changes: 2 additions & 0 deletions app/controllers/browse_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ def index
end

def show
set_new_browse_response_header if new_browse_page_under_test?

page = MainstreamBrowsePage.find("/browse/#{params[:top_level_slug]}")
setup_content_item_and_navigation_helpers(page)

Expand Down
31 changes: 31 additions & 0 deletions app/controllers/new_browse_ab_testable.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
module NewBrowseAbTestable
# placeholder custom dimension
CUSTOM_DIMENSION = 67

ALLOWED_VARIANTS = %w[A B Z].freeze

def new_browse_test
@new_browse_test ||= GovukAbTesting::AbTest.new(
"NewBrowse",
dimension: CUSTOM_DIMENSION,
allowed_variants: ALLOWED_VARIANTS,
control_variant: "Z",
)
end

def new_browse_variant
new_browse_test.requested_variant(request.headers)
end

def set_new_browse_response_header
new_browse_variant.configure_response(response)
end

def new_browse_variant_b?
page_under_test? && new_browse_variant.variant?("B")
end

def new_browse_page_under_test?
request.path.starts_with?("/browse")
end
end
1 change: 1 addition & 0 deletions app/views/layouts/application.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
<%= javascript_include_tag "application" %>
<%= csrf_meta_tags %>
<%= yield :meta_tags %>
<%= new_browse_variant.analytics_meta_tag.html_safe if new_browse_page_under_test? %>
<%= render 'govuk_publishing_components/components/meta_tags', content_item: content_item_h %>
<%=
render_component_stylesheets
Expand Down
16 changes: 16 additions & 0 deletions spec/controllers/browse_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@

expect(response.headers["Cache-Control"]).to eq("max-age=300, public")
end

it "is not included in the newbrowse AB test" do
assert_response_not_modified_for_ab_test("NewBrowse")
end
end

describe "GET top_level_browse_page" do
Expand All @@ -42,6 +46,18 @@
expect(response.content_type).to eq "text/html; charset=utf-8"
expect(response).to render_template(partial: "_cards")
end

context "NewBrowse AB test" do
subject { get :show, params: { top_level_slug: "benefits" } }

%w[A B Z].each do |variant|
it "with variant #{variant}" do
with_variant NewBrowse: variant do
expect(subject).to render_template(:show)
end
end
end
end
end

it "404 if the browse page does not exist" do
Expand Down

0 comments on commit 9a51fda

Please sign in to comment.