diff --git a/app/assets/javascripts/application.js b/app/assets/javascripts/application.js index 711f477f7..8a44871ee 100644 --- a/app/assets/javascripts/application.js +++ b/app/assets/javascripts/application.js @@ -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 // diff --git a/app/controllers/ab_test/search_autocomplete_ab_testable.rb b/app/controllers/ab_test/search_autocomplete_ab_testable.rb new file mode 100644 index 000000000..28ca6da40 --- /dev/null +++ b/app/controllers/ab_test/search_autocomplete_ab_testable.rb @@ -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 diff --git a/app/controllers/finders_controller.rb b/app/controllers/finders_controller.rb index d5c6f7554..78a538103 100644 --- a/app/controllers/finders_controller.rb +++ b/app/controllers/finders_controller.rb @@ -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 @@ -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 diff --git a/app/views/finders/show_all_content_finder.html.erb b/app/views/finders/show_all_content_finder.html.erb index f02f96433..912ee3572 100644 --- a/app/views/finders/show_all_content_finder.html.erb +++ b/app/views/finders/show_all_content_finder.html.erb @@ -11,6 +11,7 @@ <% end %> "> + <%= @requested_variant.analytics_meta_tag.html_safe if @requested_variant.present? %> <% end %> <% content_for :meta_title, content_item.title %> @@ -53,7 +54,7 @@
diff --git a/spec/controllers/finders_controller_spec.rb b/spec/controllers/finders_controller_spec.rb index 095fc5fa7..b7b4d6d37 100644 --- a/spec/controllers/finders_controller_spec.rb +++ b/spec/controllers/finders_controller_spec.rb @@ -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