Skip to content

Commit

Permalink
Revert "Add GA4 indexes to attachments that render a details component"
Browse files Browse the repository at this point in the history
This reverts commit ecdda77.

The code was working, but it isn't giving us exactly what we want - it was returning all attachments for a given document, but we want to be able to filter by ones which are actually being rendered on the page. Therefore we'll explore using JS to handle this, or shelve this work.
  • Loading branch information
AshGDS committed Mar 5, 2025
1 parent 2529272 commit 35983ff
Show file tree
Hide file tree
Showing 2 changed files with 0 additions and 39 deletions.
11 changes: 0 additions & 11 deletions lib/govspeak/post_processor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,6 @@ def self.extension(title, &block)
end

extension("embed attachment HTML") do |document|
# Attachments with details components need indexes set for GA4 tracking purposes
details_attachments = govspeak_document.attachments.select { |attachment| attachment[:alternative_format_contact_email] }
details_attachments_size = details_attachments.size
details_attachment_index = 0

document.css("govspeak-embed-attachment").map do |el|
attachment = govspeak_document.attachments.detect { |a| a[:id] == el["id"] }

Expand All @@ -70,17 +65,11 @@ def self.extension(title, &block)
next
end

if attachment[:alternative_format_contact_email]
details_attachment_index += 1
details_ga4_attributes = { index_section: details_attachment_index, index_section_count: details_attachments_size }
end

attachment_html = GovukPublishingComponents.render(
"govuk_publishing_components/components/attachment",
attachment:,
margin_bottom: 6,
locale: govspeak_document.locale,
details_ga4_attributes:,
)
el.swap(attachment_html)
end
Expand Down
28 changes: 0 additions & 28 deletions test/govspeak_attachment_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,32 +52,4 @@ def render_govspeak(govspeak, attachments = [])
assert html_has_selector?(rendered, "section.gem-c-attachment")
assert_match(/<p>some more text<\/p>/, rendered)
end

test "renders attachments with details elements with the correct GA4 index" do
attachments = []

(1..3).each do |index|
attachments << {
id: "attachment#{index}.ods",
url: "http://example.com/attachment#{index}",
title: "Attachment Title #{index}",
alternative_format_contact_email: "example@gov.uk",
}
end

# Insert an attachment without a details element, to ensure our code to increment the index ignores these
attachments.insert(1, {
id: "attachment.pdf",
url: "http://example.com/attachment.pdf",
title: "Attachment Title",
})

rendered = render_govspeak("[Attachment:attachment1.ods]\n[Attachment:attachment.pdf]\n[Attachment:attachment2.ods]\n[Attachment:attachment3.ods]", attachments)
node = Nokogiri::HTML(rendered)
node.css(".gem-c-details").each_with_index.map do |details, index|
ga4_event = JSON.parse(details.attribute("data-ga4-event"))
assert_equal ga4_event["index_section"], index + 1
assert_equal ga4_event["index_section_count"], 3
end
end
end

0 comments on commit 35983ff

Please sign in to comment.