Skip to content

Commit

Permalink
Pass attachment name to GA4 details component tracking
Browse files Browse the repository at this point in the history
  • Loading branch information
AshGDS committed Jan 31, 2025
1 parent 18a7d45 commit 781771f
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
attributes = []
url_data_attributes ||= {}
details_ga4_attributes ||= {}
details_ga4_attributes[:section] = attachment.title unless details_ga4_attributes[:section]
shared_helper = GovukPublishingComponents::Presenters::SharedHelper.new(local_assigns)

component_helper = GovukPublishingComponents::Presenters::ComponentWrapperHelper.new(local_assigns)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
event_name: "select_content",
type: "detail",
text: title,
section: title,
index_section: @ga4[:index_section],
}
ga4_event.merge!(ga4_attributes)
Expand Down
26 changes: 25 additions & 1 deletion spec/components/attachment_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -107,14 +107,38 @@ def component_name
event_name: "select_content",
type: "detail",
text: "Request an accessible format.",
section: "Request an accessible format.",
index_section: 1,
index_section_count: 4,
another_attribute: "here",
section: "Attachment",
}.to_json
assert_select ".govuk-details[data-ga4-event='#{attributes}']"
end

it "allows the default GA4 section on the details component to be overwritten" do
render_component(
attachment: {
title: "Attachment",
url: "attachment",
content_type: "application/vnd.oasis.opendocument.spreadsheet",
alternative_format_contact_email: "defra.helpline@defra.gsi.gov.uk",
},
details_ga4_attributes: {
section: "Hello",
},
)
assert_select "a[href='mailto:defra.helpline@defra.gsi.gov.uk']"
attributes = {
event_name: "select_content",
type: "detail",
text: "Request an accessible format.",
index_section: 1,
section: "Hello",
}.to_json
puts rendered
assert_select ".govuk-details[data-ga4-event='#{attributes}']"
end

it "does not show opendocument metadata if disabled" do
render_component(
attachment: {
Expand Down
8 changes: 4 additions & 4 deletions spec/components/details_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -58,26 +58,26 @@ def component_name

it "increments the GA4 index_section parameter when more than one component instance" do
render_component(title: "first details")
assert_select '.govuk-details[data-ga4-event=\'{"event_name":"select_content","type":"detail","text":"first details","section":"first details","index_section":1}\']'
assert_select '.govuk-details[data-ga4-event=\'{"event_name":"select_content","type":"detail","text":"first details","index_section":1}\']'

render_component(title: "second details")
assert_select '.govuk-details[data-ga4-event=\'{"event_name":"select_content","type":"detail","text":"second details","section":"second details","index_section":2}\']'
assert_select '.govuk-details[data-ga4-event=\'{"event_name":"select_content","type":"detail","text":"second details","index_section":2}\']'
end

it "accepts GA4 event data parameters" do
render_component(
title: "some title",
ga4_attributes: { index_section_count: 12 },
)
assert_select '.govuk-details[data-ga4-event=\'{"event_name":"select_content","type":"detail","text":"some title","section":"some title","index_section":1,"index_section_count":12}\']'
assert_select '.govuk-details[data-ga4-event=\'{"event_name":"select_content","type":"detail","text":"some title","index_section":1,"index_section_count":12}\']'
end

it "can override GA4 event data parameters" do
render_component(
title: "some title",
ga4_attributes: { type: "a mouse!", index_section_count: 12 },
)
assert_select '.govuk-details[data-ga4-event=\'{"event_name":"select_content","type":"a mouse!","text":"some title","section":"some title","index_section":1,"index_section_count":12}\']'
assert_select '.govuk-details[data-ga4-event=\'{"event_name":"select_content","type":"a mouse!","text":"some title","index_section":1,"index_section_count":12}\']'
end

it "can disable GA4" do
Expand Down

0 comments on commit 781771f

Please sign in to comment.