Skip to content

Commit

Permalink
Apply GA4 tracking to flash alerts
Browse files Browse the repository at this point in the history
Introduces the data_attributes required for `ga4-auto-tracker` to
only the flash alert component. This has been achieved by:

1. Passing `data_attributes` to `flash_message_component.html.erb`.
2. Adding a  `data_attributes` property to `Admin::FlashMessageComponent`
   (so it is available to all extending components).
3. Populating `data_attributes` in `Admin::FlashAlertComponent` with
   required properties.
  • Loading branch information
gtvj committed Jan 16, 2025
1 parent e797422 commit fa9af73
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 1 deletion.
12 changes: 12 additions & 0 deletions app/components/admin/flash_alert_component.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,16 @@ class Admin::FlashAlertComponent < Admin::FlashMessageComponent
def component_name
"error_alert"
end

def data_attributes
{
module: "ga4-auto-tracker",
"ga4-auto": {
event_name: "flash_message",
type: component_name,
text: message,
action: "error",
}.to_json,
}
end
end
5 changes: 4 additions & 1 deletion app/components/admin/flash_message_component.html.erb
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
<%=
content_tag(:div) do
render "govuk_publishing_components/components/#{component_name}", { message: }
render "govuk_publishing_components/components/#{component_name}", {
message: message,
data_attributes: data_attributes,
}
end
%>
2 changes: 2 additions & 0 deletions app/components/admin/flash_message_component.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,6 @@ def message
@message
end
end

def data_attributes; end
end

0 comments on commit fa9af73

Please sign in to comment.