diff --git a/app/controllers/content_items_controller.rb b/app/controllers/content_items_controller.rb index 357812a23..840861e46 100644 --- a/app/controllers/content_items_controller.rb +++ b/app/controllers/content_items_controller.rb @@ -71,7 +71,7 @@ def load_taxonomy_navigation services = Supergroups::Services.new(taxon_ids) @taxonomy_navigation = { - services: services.tagged_content, + services: (services.all_services if services.any_services?), } @tagged_taxons = taxons.map do |taxon| diff --git a/app/presenters/supergroups/services.rb b/app/presenters/supergroups/services.rb index 456c3cdaa..ad62fbb35 100644 --- a/app/presenters/supergroups/services.rb +++ b/app/presenters/supergroups/services.rb @@ -4,21 +4,50 @@ class Services def initialize(taxon_ids) @taxon_ids = taxon_ids + @content = MostPopularContent.fetch(content_ids: @taxon_ids, filter_content_purpose_supergroup: "services") + end + + def all_services + { + documents: tagged_content, + promoted_content: promoted_content, + } + end + + def any_services? + @content.any? end def tagged_content - @content = MostPopularContent.fetch(content_ids: @taxon_ids, filter_content_purpose_supergroup: "services") - format_document_data(@content) + items = @content.drop(promoted_content_count) + format_document_data(items) + end + + def promoted_content + items = @content.shift(promoted_content_count) + format_document_data(items, "HighlightBoxClicked") end private - def format_document_data(documents) - documents&.map do |document| + def promoted_content_count + 3 + end + + def format_document_data(documents, data_category = "DocumentListClicked") + documents.each.with_index(1)&.map do |document, index| data = { link: { text: document["title"], - path: document["link"] + path: document["link"], + data_attributes: { + track_category: "Services" + data_category, + track_action: index, + track_label: document["link"], + track_options: { + dimension29: document["title"], + } + } } } diff --git a/app/views/shared/_taxonomy_navigation.html.erb b/app/views/shared/_taxonomy_navigation.html.erb index efd49e1fe..8ee45d893 100644 --- a/app/views/shared/_taxonomy_navigation.html.erb +++ b/app/views/shared/_taxonomy_navigation.html.erb @@ -17,7 +17,11 @@ <%= render "govuk_publishing_components/components/highlight_boxes", { inverse: true, - items: taxonomy_navigation[:services] + items: taxonomy_navigation[:services][:promoted_content] + } %> + + <%= render "govuk_publishing_components/components/document_list", { + items: taxonomy_navigation[:services][:documents] } %> <% end %> diff --git a/test/integration/content_pages_navigation_test.rb b/test/integration/content_pages_navigation_test.rb index 46cedabf0..724e303e8 100644 --- a/test/integration/content_pages_navigation_test.rb +++ b/test/integration/content_pages_navigation_test.rb @@ -27,7 +27,7 @@ class ContentPagesNavigationTest < ActionDispatch::IntegrationTest setup_and_visit_content_item_with_taxons('guide', SINGLE_TAXON) assert page.has_css?('.taxonomy-navigation h2', text: 'Becoming an apprentice') - assert page.has_css?('.gem-c-highlight-boxes__title', text: 'Apprenticeship agreement: template') + assert page.has_css?('.gem-c-highlight-boxes__title', text: 'Free school meals form') end test "ContentPagesNav variant B renders many taxons nicely" do @@ -36,11 +36,11 @@ class ContentPagesNavigationTest < ActionDispatch::IntegrationTest setup_and_visit_content_item_with_taxons('guide', THREE_TAXONS) - assert page.has_css?('.taxonomy-navigation h2', text: 'Becoming an apprentice') - assert page.has_css?('.taxonomy-navigation h2', text: 'Becoming a wizard') - assert page.has_css?('.taxonomy-navigation h2', text: 'Becoming the sorceror supreme') + assert page.has_css?('.taxonomy-navigation h2 a[href="/education/becoming-an-apprentice"]', text: 'Becoming an apprentice') + assert page.has_css?('.taxonomy-navigation h2 a[href="/education/becoming-a-wizard"]', text: 'Becoming a wizard') + assert page.has_css?('.taxonomy-navigation h2 a[href="/education/becoming-the-sorceror-supreme"]', text: 'Becoming the sorceror supreme') - assert page.has_css?('.gem-c-highlight-boxes__title', text: 'Apprenticeship agreement: template') + assert page.has_css?('.gem-c-highlight-boxes__title', text: 'Free school meals form') end test "ContentPagesNav variant B only includes live taxons" do @@ -51,10 +51,38 @@ class ContentPagesNavigationTest < ActionDispatch::IntegrationTest setup_and_visit_content_item_with_taxons('guide', taxons) - assert page.has_css?('.taxonomy-navigation h2', text: 'Becoming an apprentice') - refute page.has_css?('.taxonomy-navigation h2', text: 'Becoming a ghostbuster') + assert page.has_css?('.taxonomy-navigation h2 a[href="/education/becoming-an-apprentice"]', text: 'Becoming an apprentice') + refute page.has_css?('.taxonomy-navigation h2 a[href="/education/becoming-a-ghostbuster"]', text: 'Becoming a ghostbuster') + end + + test "shows the Services section title and documents with tracking" do + stub_rummager + setup_variant_b + + taxons = SINGLE_TAXON + + setup_and_visit_content_item_with_taxons('guide', taxons) + + assert page.has_css?('h3', text: "Services") + assert page.has_css?('.gem-c-highlight-boxes__title', text: 'Free school meals form') + assert page.has_css?('.gem-c-highlight-boxes__title[data-track-category="ServicesHighlightBoxClicked"]', text: 'Free school meals form') + assert page.has_css?('.gem-c-highlight-boxes__title[data-track-action="1"]', text: 'Free school meals form') + assert page.has_css?('.gem-c-highlight-boxes__title[data-track-label="/government/publications/meals"]', text: 'Free school meals form') + + assert page.has_css?('.gem-c-document-list__item a[data-track-category="ServicesDocumentListClicked"]', text: 'Free school meals form') + assert page.has_css?('.gem-c-document-list__item a[data-track-action="1"]', text: 'Free school meals form') + assert page.has_css?('.gem-c-document-list__item a[data-track-label="/government/publications/meals"]', text: 'Free school meals form') + end + + test "does not show the Services section if there is no tagged content" do + stub_empty_rummager + setup_variant_b + + taxons = SINGLE_TAXON + + setup_and_visit_content_item_with_taxons('guide', taxons) - assert page.has_css?('.gem-c-highlight-boxes__title', text: 'Apprenticeship agreement: template') + refute page.has_css?('h3', text: "Services") end def setup_variant_a diff --git a/test/support/content_pages_nav_test_helper.rb b/test/support/content_pages_nav_test_helper.rb index 81cb4a471..49bea9472 100644 --- a/test/support/content_pages_nav_test_helper.rb +++ b/test/support/content_pages_nav_test_helper.rb @@ -1,27 +1,38 @@ module ContentPagesNavTestHelper def stub_rummager - stub_any_rummager_search - .to_return( - body: { - "results": [ - { - "content_store_document_type": "form", - "description": "This agreement must be signed by the apprentice and the employer at the start of the apprenticeship.", - "link": "/government/publications/apprenticeship-agreement-template", - "public_timestamp": "2012-08-28T00:00:00.000+01:00", - "title": "Apprenticeship agreement: template", - "index": "government", - "_id": "/government/publications/apprenticeship-agreement-template", - "elasticsearch_type": "edition", - "document_type": "edition" - } - ], - "total": 1, - "start": 0, - "aggregates": {}, - "suggested_queries": [] - }.to_json - ) + results = [] + + 4.times do + results.push( + "content_store_document_type": "form", + "link": "/government/publications/meals", + "title": "Free school meals form", + ) + end + + stub_any_rummager_search.to_return( + body: { + "results": results, + "total": 1, + "start": 0, + "aggregates": {}, + "suggested_queries": [] + }.to_json + ) + end + + def stub_empty_rummager + results = [] + + stub_any_rummager_search.to_return( + body: { + "results": results, + "total": 1, + "start": 0, + "aggregates": {}, + "suggested_queries": [] + }.to_json + ) end SINGLE_TAXON = [