Skip to content

Commit

Permalink
Merge pull request #385 from alphagov/govspeak-details-indexes
Browse files Browse the repository at this point in the history
Add GA4 indexes to attachments that render a details component
  • Loading branch information
AshGDS authored Mar 4, 2025
2 parents beb646f + ecdda77 commit 7f47551
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 0 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## Unreleased

* Add GA4 indexes to attachments that render a details component ([#385](https://github.com/alphagov/govspeak/pull/385))

## 10.0.1

* Update dependencies
Expand Down
11 changes: 11 additions & 0 deletions lib/govspeak/post_processor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,11 @@ 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 @@ -65,11 +70,17 @@ 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: 28 additions & 0 deletions test/govspeak_attachment_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,32 @@ 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 7f47551

Please sign in to comment.