diff --git a/app/helpers/attachments_helper.rb b/app/helpers/attachments_helper.rb index 5e391ee6a9e..b0977f0e10b 100644 --- a/app/helpers/attachments_helper.rb +++ b/app/helpers/attachments_helper.rb @@ -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 diff --git a/test/unit/app/helpers/attachments_helper_test.rb b/test/unit/app/helpers/attachments_helper_test.rb index e9abae7fe59..ae116f6af5c 100644 --- a/test/unit/app/helpers/attachments_helper_test.rb +++ b/test/unit/app/helpers/attachments_helper_test.rb @@ -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 = "test@example.com" + 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 = {