Skip to content

Commit

Permalink
Merge pull request #9996 from alphagov/attachments_ga4_index
Browse files Browse the repository at this point in the history
Add GA4 index to attachment component renders
  • Loading branch information
AshGDS authored Mar 3, 2025
2 parents 8b3a088 + f97aae6 commit a762950
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 3 deletions.
7 changes: 4 additions & 3 deletions app/helpers/attachments_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -60,14 +60,15 @@ def attachment_component_params(attachment, alternative_format_contact_email: ni
end

def block_attachments(attachments = [], alternative_format_contact_email = nil)
attachments
.select { |attachment| !attachment.file? || attachment.attachment_data.all_asset_variants_uploaded? }
.map do |attachment|
filtered_attachments = attachments.select { |attachment| !attachment.file? || attachment.attachment_data.all_asset_variants_uploaded? }
filtered_attachments_size = filtered_attachments.size
filtered_attachments.each_with_index.map do |attachment, index|
render(
partial: "govuk_publishing_components/components/attachment",
locals: {
attachment: attachment_component_params(attachment, alternative_format_contact_email:),
margin_bottom: 6,
details_ga4_attributes: { index_section: index + 1, index_section_count: filtered_attachments_size },
},
)
end
Expand Down
19 changes: 19 additions & 0 deletions test/unit/app/helpers/attachments_helper_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,25 @@ class AttachmentsHelperTest < ActionView::TestCase
end
end

test "block_attachments renders with GA4 indexes on the nested details component" do
alternative_format_contact_email = "[email protected]"
file_attachment_with_all_assets = create(:file_attachment)
attachments = [
file_attachment_with_all_assets,
file_attachment_with_all_assets,
file_attachment_with_all_assets,
]

rendered_attachments = block_attachments(attachments, alternative_format_contact_email)
rendered_attachments.each.with_index do |rendered, index|
assert_select_within_html(rendered, ".gem-c-details") do |details|
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

test "component params for HTML attachment" do
attachment = create(:html_attachment)
expect_params = {
Expand Down

0 comments on commit a762950

Please sign in to comment.