From b19b4bec1ae6c288add52b4d51702fd89f5ed089 Mon Sep 17 00:00:00 2001 From: Andy Sellick Date: Fri, 19 Apr 2024 16:38:33 +0100 Subject: [PATCH] Add GA4 tracking to details in attachments --- app/presenters/publication_presenter.rb | 4 + .../content_items/_attachments_list.html.erb | 5 +- test/integration/publication_test.rb | 101 +++++++++++------- 3 files changed, 70 insertions(+), 40 deletions(-) diff --git a/app/presenters/publication_presenter.rb b/app/presenters/publication_presenter.rb index 10643a3c3..3bf58ee0b 100644 --- a/app/presenters/publication_presenter.rb +++ b/app/presenters/publication_presenter.rb @@ -14,6 +14,10 @@ def attachments_for_components documents.select { |doc| featured_attachments.include? doc["id"] } end + def attachments_with_details + attachments_for_components.select { |doc| doc["accessible"] == false && doc["alternative_format_contact_email"] }.count + end + def documents return [] unless content_item["details"]["attachments"] diff --git a/app/views/content_items/_attachments_list.html.erb b/app/views/content_items/_attachments_list.html.erb index 99f35a694..384bca740 100644 --- a/app/views/content_items/_attachments_list.html.erb +++ b/app/views/content_items/_attachments_list.html.erb @@ -9,7 +9,10 @@ <%= render 'govuk_publishing_components/components/attachment', { heading_level: 3, attachment: details, - margin_bottom: 6 + margin_bottom: 6, + details_ga4_attributes: { + index_section_count: @content_item.attachments_with_details + } } %> <% end %> diff --git a/test/integration/publication_test.rb b/test/integration/publication_test.rb index 90eb6c49b..bd65dcdbf 100644 --- a/test/integration/publication_test.rb +++ b/test/integration/publication_test.rb @@ -39,18 +39,11 @@ class PublicationTest < ActionDispatch::IntegrationTest "accessible" => false, "alternative_format_contact_email" => "customerservices@publicguardian.gov.uk", "attachment_type" => "file", - "command_paper_number" => "", "content_type" => "application/pdf", - "file_size" => 123_456, "filename" => "veolia-permit.pdf", - "hoc_paper_number" => "", "id" => "violia-permit", - "isbn" => "", "locale" => "en", "title" => "Permit: Veolia ES (UK) Limited", - "unique_reference" => "", - "unnumbered_command_paper" => false, - "unnumbered_hoc_paper" => false, "url" => "https://assets.publishing.service.gov.uk/media/123abc/veolia-permit.zip", }], "featured_attachments" => [], @@ -89,16 +82,8 @@ class PublicationTest < ActionDispatch::IntegrationTest "id" => "PUBLIC_1392629965.pdf", "title" => "Number of ex-regular service personnel now part of FR20", "url" => "https://assets.publishing.service.gov.uk/government/uploads/system/uploads/attachment_data/file/315163/PUBLIC_1392629965.pdf", - "command_paper_number" => "", - "hoc_paper_number" => "", - "isbn" => "", - "unique_reference" => "", - "unnumbered_command_paper" => false, - "unnumbered_hoc_paper" => false, "content_type" => "application/pdf", - "file_size" => 932, "filename" => "PUBLIC_1392629965.pdf", - "number_of_pages" => 2, "locale" => "en", }], }, @@ -118,16 +103,8 @@ class PublicationTest < ActionDispatch::IntegrationTest "id" => "PUBLIC_1392629965.pdf", "title" => "Number of ex-regular service personnel now part of FR20", "url" => "https://assets.publishing.service.gov.uk/government/uploads/system/uploads/attachment_data/file/315163/PUBLIC_1392629965.pdf", - "command_paper_number" => "", - "hoc_paper_number" => "", - "isbn" => "", - "unique_reference" => "", - "unnumbered_command_paper" => false, - "unnumbered_hoc_paper" => false, "content_type" => "application/pdf", - "file_size" => 932, "filename" => "PUBLIC_1392629965.pdf", - "number_of_pages" => 2, "locale" => "en", }], }, @@ -148,16 +125,8 @@ class PublicationTest < ActionDispatch::IntegrationTest "id" => "PUBLIC_1392629965.pdf", "title" => "Number of ex-regular service personnel now part of FR20", "url" => "https://assets.publishing.service.gov.uk/government/uploads/system/uploads/attachment_data/file/315163/PUBLIC_1392629965.pdf", - "command_paper_number" => "", - "hoc_paper_number" => "", - "isbn" => "", - "unique_reference" => "", - "unnumbered_command_paper" => false, - "unnumbered_hoc_paper" => false, "content_type" => "application/pdf", - "file_size" => 932, "filename" => "PUBLIC_1392629965.pdf", - "number_of_pages" => 2, "locale" => "en", }], }, @@ -168,6 +137,68 @@ class PublicationTest < ActionDispatch::IntegrationTest end end + test "tracks details elements in attachments correctly" do + overrides = { + "details" => { + "attachments" => [ + { + "accessible" => false, + "alternative_format_contact_email" => "ddc-modinternet@mod.gov.uk", + "id" => "PUBLIC_1392629965.pdf", + "title" => "Attachment 1 - should have details element", + "url" => "https://assets.publishing.service.gov.uk/government/uploads/system/uploads/attachment_data/file/315163/PUBLIC_1392629965.pdf", + "content_type" => "application/pdf", + "filename" => "PUBLIC_1392629965.pdf", + "locale" => "en", + }, + { + "accessible" => true, + "alternative_format_contact_email" => "ddc-modinternet@mod.gov.uk", + "id" => "PUBLIC_1392629965.pdf", + "title" => "Attachment 2", + "url" => "https://assets.publishing.service.gov.uk/government/uploads/system/uploads/attachment_data/file/315163/PUBLIC_1392629965.pdf", + "content_type" => "application/pdf", + "filename" => "PUBLIC_1392629965.pdf", + "locale" => "en", + }, + { + "accessible" => true, + "alternative_format_contact_email" => nil, + "id" => "PUBLIC_1392629965.pdf", + "title" => "Attachment 3", + "url" => "https://assets.publishing.service.gov.uk/government/uploads/system/uploads/attachment_data/file/315163/PUBLIC_1392629965.pdf", + "content_type" => "application/pdf", + "filename" => "PUBLIC_1392629965.pdf", + "locale" => "en", + }, + { + "accessible" => false, + "alternative_format_contact_email" => "ddc-modinternet@mod.gov.uk", + "id" => "PUBLIC_1392629965.pdf", + "title" => "Attachment 4 - should have details element", + "url" => "https://assets.publishing.service.gov.uk/government/uploads/system/uploads/attachment_data/file/315163/PUBLIC_1392629965.pdf", + "content_type" => "application/pdf", + "filename" => "PUBLIC_1392629965.pdf", + "locale" => "en", + }, + ], + }, + } + setup_and_visit_content_item("publication-with-featured-attachments", overrides) + within "#documents" do + attachments = page.find_all(".gem-c-attachment") + assert_equal attachments.length, overrides["details"]["attachments"].length + + attachments.each do |attachment| + next unless attachment.has_css?(".govuk-details__summary") + + details = attachment.find(".govuk-details__summary")["data-ga4-event"] + actual_tracking = JSON.parse(details) + assert_equal actual_tracking["index_section_count"], 2 + end + end + end + test "renders external links correctly" do overrides = { "details" => { @@ -178,16 +209,8 @@ class PublicationTest < ActionDispatch::IntegrationTest "id" => "PUBLIC_1392629965.pdf", "title" => "Number of ex-regular service personnel now part of FR20", "url" => "https://not-a-real-website-hopefully", - "command_paper_number" => "", - "hoc_paper_number" => "", - "isbn" => "", - "unique_reference" => "", - "unnumbered_command_paper" => false, - "unnumbered_hoc_paper" => false, "content_type" => "application/pdf", - "file_size" => 932, "filename" => "PUBLIC_1392629965.pdf", - "number_of_pages" => 2, "locale" => "en", }], },