Skip to content

Commit

Permalink
Add GA4 tracking to details in attachments
Browse files Browse the repository at this point in the history
  • Loading branch information
andysellick committed Apr 19, 2024
1 parent bb66644 commit b19b4be
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 40 deletions.
4 changes: 4 additions & 0 deletions app/presenters/publication_presenter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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"]

Expand Down
5 changes: 4 additions & 1 deletion app/views/content_items/_attachments_list.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -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 %>
</section>
Expand Down
101 changes: 62 additions & 39 deletions test/integration/publication_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,18 +39,11 @@ class PublicationTest < ActionDispatch::IntegrationTest
"accessible" => false,
"alternative_format_contact_email" => "[email protected]",
"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" => [],
Expand Down Expand Up @@ -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",
}],
},
Expand All @@ -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",
}],
},
Expand All @@ -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",
}],
},
Expand All @@ -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" => "[email protected]",
"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" => "[email protected]",
"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" => "[email protected]",
"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" => {
Expand All @@ -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",
}],
},
Expand Down

0 comments on commit b19b4be

Please sign in to comment.