Skip to content

Commit

Permalink
Merge pull request #3555 from alphagov/autocomplete-ab-test
Browse files Browse the repository at this point in the history
Set up `SearchAutocomplete` AB test
  • Loading branch information
csutter authored Nov 7, 2024
2 parents 63113d9 + db0e1d2 commit 5ed4233
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 1 deletion.
1 change: 1 addition & 0 deletions app/assets/javascripts/application.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
//= require govuk_publishing_components/components/metadata
//= require govuk_publishing_components/components/option-select
//= require govuk_publishing_components/components/radio
//= require govuk_publishing_components/components/search-with-autocomplete
//
//= require support
//
Expand Down
22 changes: 22 additions & 0 deletions app/controllers/ab_test/search_autocomplete_ab_testable.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
module AbTest::SearchAutocompleteAbTestable
def search_autocomplete_ab_test
GovukAbTesting::AbTest.new(
"SearchAutocomplete",
allowed_variants: [
"A", # No autocomplete
"B", # Autocomplete
"Z", # No autocomplete
],
control_variant: "Z",
)
end

def set_search_autocomplete_ab_test_requested_variant
@requested_variant = search_autocomplete_ab_test.requested_variant(request.headers)
@requested_variant.configure_response(response)
end

def use_autocomplete?
@requested_variant&.variant?("B")
end
end
5 changes: 5 additions & 0 deletions app/controllers/finders_controller.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
class FindersController < ApplicationController
layout "finder_layout"

include AbTest::SearchAutocompleteAbTestable

before_action do
set_expiry(content_item)
set_search_autocomplete_ab_test_requested_variant if content_item.all_content_finder?
end

def show
Expand Down Expand Up @@ -58,6 +61,8 @@ def show_page_variables
@sort_presenter = sort_presenter
@pagination = pagination_presenter
@spelling_suggestion_presenter = spelling_suggestion_presenter
@ab_test_search_component = use_autocomplete? ? "search_with_autocomplete" : "search"
@autocomplete_url = ENV.fetch("SEARCH_AUTOCOMPLETE_API_URL", "")
end

private
Expand Down
5 changes: 4 additions & 1 deletion app/views/finders/show_all_content_finder.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
<meta name="govuk:discovery-engine-attribution-token" content="<%= result_set_presenter.discovery_engine_attribution_token %>">
<% end %>
<meta name="govuk:spelling-suggestion" content="<%= @spelling_suggestion_presenter.suggestions.first&.fetch(:keywords, "") %>">
<%= @requested_variant.analytics_meta_tag.html_safe if @requested_variant.present? %>
<% end %>
<% content_for :meta_title, content_item.title %>
Expand Down Expand Up @@ -53,7 +54,7 @@
<div class="govuk-grid-row">
<div class="govuk-grid-column-two-thirds-from-desktop">
<div id="keywords" role="search" aria-label="Sitewide">
<%= render "govuk_publishing_components/components/search", {
<%= render "govuk_publishing_components/components/#{@ab_test_search_component}", {
id: "finder-keyword-search",
name: "keywords",
type: 'search',
Expand All @@ -68,6 +69,8 @@
wrap_label_in_a_heading: true,
heading_level: 1,
margin_bottom: 0,
source_url: @autocomplete_url,
source_key: "suggestions"
} %>
</div>

Expand Down
26 changes: 26 additions & 0 deletions spec/controllers/finders_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,32 @@
expect(response.status).to eq(200)
expect(response).to render_template("finders/show_all_content_finder")
end

describe "SearchAutocomplete AB test" do
it "does not render the search_with_autocomplete component in the A variant" do
with_variant SearchAutocomplete: "A" do
get :show, params: { slug: "search/all", keywords: "hello" }
end

expect(response.body).not_to include('"gem-c-search-with-autocomplete"')
end

it "renders the search_with_autocomplete component in the B variant" do
with_variant SearchAutocomplete: "B" do
get :show, params: { slug: "search/all", keywords: "hello" }
end

expect(response.body).to include('"gem-c-search-with-autocomplete"')
end

it "does not render the search_with_autocomplete component in the Z variant" do
with_variant SearchAutocomplete: "Z" do
get :show, params: { slug: "search/all", keywords: "hello" }
end

expect(response.body).not_to include('"gem-c-search-with-autocomplete"')
end
end
end
end

Expand Down

0 comments on commit 5ed4233

Please sign in to comment.