Skip to content

Commit

Permalink
Merge pull request #3553 from alphagov/ga4-attachment-link-fix
Browse files Browse the repository at this point in the history
Ensure file attachments have the GA4 event_name 'file_download'
  • Loading branch information
AshGDS authored Aug 16, 2023
2 parents a4778a3 + 92de2b9 commit b7fa66c
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
## Unreleased

* Add GA4 tracking to the emergency banner ([PR #3549](https://github.com/alphagov/govuk_publishing_components/pull/3549))
* Ensure file attachments have the GA4 event_name 'file_download' ([PR #3553](https://github.com/alphagov/govuk_publishing_components/pull/3553))

## 35.13.2

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

container_class_names = %w[gem-c-attachment govuk-!-display-none-print]
container_class_names << shared_helper.get_margin_bottom if local_assigns.key?(:margin_bottom)
ga4_link = { 'event_name': 'navigation', 'type': 'attachment' }

case attachment.type
when "file"
Expand Down Expand Up @@ -38,6 +39,9 @@
class: "gem-c-attachment__attribute",
)
end

ga4_link[:event_name] = 'file_download'

when "html"
attributes << tag.span(
"HTML",
Expand All @@ -51,7 +55,7 @@
end

%>
<%= tag.section class: class_names(container_class_names), data: { module: "ga4-link-tracker", ga4_track_links_only: "", ga4_link: { 'event_name': 'navigation', 'type': 'attachment' } } do %>
<%= tag.section class: class_names(container_class_names), data: { module: "ga4-link-tracker", ga4_track_links_only: "", ga4_link: ga4_link } do %>
<%= tag.div class: "gem-c-attachment__thumbnail" do %>
<%= link_to attachment.url,
class: "govuk-link",
Expand Down
17 changes: 16 additions & 1 deletion spec/components/attachment_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ def assert_thumbnail(type, src: nil)
assert_select '.gem-c-attachment:not([class*="govuk-!-margin-bottom-"])'
end

it "includes GA4 tracking on attachment links by default" do
it "includes GA4 tracking on HTML attachment links by default" do
render_component(
attachment: {
title: "Test",
Expand All @@ -283,4 +283,19 @@ def assert_thumbnail(type, src: nil)
assert_select "section[data-ga4-link='{\"event_name\":\"navigation\",\"type\":\"attachment\"}']"
assert_select "section[data-ga4-track-links-only]"
end

it "includes GA4 tracking on file attachment links by default" do
render_component(
attachment: {
title: "Test",
url: "https://example.com",
filename: "test",
content_type: "application/pdf",
},
)

assert_select "section[data-module=ga4-link-tracker]"
assert_select "section[data-ga4-link='{\"event_name\":\"file_download\",\"type\":\"attachment\"}']"
assert_select "section[data-ga4-track-links-only]"
end
end

0 comments on commit b7fa66c

Please sign in to comment.