diff --git a/app/assets/javascripts/application.js b/app/assets/javascripts/application.js index 06f6bb5bb..b9092959f 100644 --- a/app/assets/javascripts/application.js +++ b/app/assets/javascripts/application.js @@ -57,3 +57,20 @@ if ($allContentFinderTaxonomySelect) { taxonomySelect.update() }) } + +const allContentFinderForm = document.getElementById('all-content-finder-form') + +if (allContentFinderForm) { + allContentFinderForm.addEventListener('change', (event) => { + let ga4ChangeCategory = event.target.closest('[data-ga4-change-category]') + if (ga4ChangeCategory) { + ga4ChangeCategory = ga4ChangeCategory.getAttribute('data-ga4-change-category') + + const consentCookie = GOVUK.getConsentCookie() + + if (consentCookie && consentCookie.usage) { + GOVUK.analyticsGa4.Ga4FinderTracker.trackChangeEvent(event.target, ga4ChangeCategory) + } + } + }) +} diff --git a/app/views/components/_filter_panel.html.erb b/app/views/components/_filter_panel.html.erb index fe638a702..4087ee9f6 100644 --- a/app/views/components/_filter_panel.html.erb +++ b/app/views/components/_filter_panel.html.erb @@ -7,13 +7,14 @@ panel_content_id = "filter-panel-#{id_suffix}" button_id = "filter-button-#{id_suffix}" result_text ||= "" + section_count ||= "" show_reset_link ||= false raise ArgumentError, "reset_link_href is required" if show_reset_link && !local_assigns[:reset_link_href] component_helper = GovukPublishingComponents::Presenters::ComponentWrapperHelper.new(local_assigns) shared_helper = GovukPublishingComponents::Presenters::SharedHelper.new(local_assigns) - component_helper.add_data_attribute({ module: "filter-panel" }) + component_helper.add_data_attribute({ module: "filter-panel ga4-event-tracker" }) component_helper.add_class("app-c-filter-panel") component_helper.add_class(shared_helper.get_margin_bottom) if local_assigns[:margin_bottom] %> @@ -23,7 +24,16 @@ <%= tag.button( id: button_id, class: "app-c-filter-panel__button govuk-link", - aria: { expanded: "false", controls: panel_content_id } + aria: { expanded: "false", controls: panel_content_id }, + "data-ga4-expandable": "", + "data-ga4-event": { + event_name: "select_content", + type: "finder", + section: button_text, + text: button_text, + index_section: 0, + index_section_count: section_count + }.to_json ) { button_text } %> <% if result_text.present? %> @@ -41,14 +51,32 @@ ) do %> <%= yield %> +
- <%= submit_tag "Apply filters", class: "govuk-button app-c-filter-panel__action app-c-filter-panel__action--submit" %> + <%= submit_tag "Apply filters", + class: "govuk-button app-c-filter-panel__action app-c-filter-panel__action--submit", + "data-ga4-event" => { + event_name: "select_content", + type: "finder", + text: button_text, + section: button_text, + action: "search", + index_section: 0, + index_section_count: section_count, + }.to_json %> <% if show_reset_link %> <%= link_to "Clear all filters", reset_link_href, - class: "govuk-link govuk-link--no-visited-state app-c-filter-panel__action app-c-filter-panel__action--reset" + class: "govuk-link govuk-link--no-visited-state app-c-filter-panel__action app-c-filter-panel__action--reset", + "data-ga4-event" => { + event_name: "select_content", + type: "finder", + text: button_text, + section: button_text, + action: "remove", + }.to_json %> <% end %>
diff --git a/app/views/components/_filter_section.html.erb b/app/views/components/_filter_section.html.erb index f90d39ec5..334b3ac21 100644 --- a/app/views/components/_filter_section.html.erb +++ b/app/views/components/_filter_section.html.erb @@ -7,14 +7,29 @@ visually_hidden_heading_prefix ||= "" heading_level ||= 2 summary_aria_attributes ||= {} + index_section ||= 0 + index_section_count ||= 0 + change_category ||= nil component_helper = GovukPublishingComponents::Presenters::ComponentWrapperHelper.new(local_assigns) + component_helper.add_data_attribute({ module: "filter-section" }) + component_helper.add_data_attribute({ "ga4-section": heading_text, "ga4-filter-parent": heading_text }) + component_helper.add_data_attribute("ga4-change-category": change_category) component_helper.add_class("app-c-filter-section") component_helper.set_open(open) %> <%= tag.details(**component_helper.all_attributes) do %> - <%= tag.summary class: "app-c-filter-section__summary", aria: summary_aria_attributes do %> + <%= tag.summary class: "app-c-filter-section__summary", aria: summary_aria_attributes, + "data-ga4-expandable": "", + "data-ga4-event" => { + event_name: "select_content", + type: "finder", + section: heading_text, + text: heading_text, + index_section: index_section, + index_section_count: index_section_count, + }.to_json do %> <%= content_tag("h#{heading_level}", class: "app-c-filter-section__summary-heading") do %> <% if visually_hidden_heading_prefix.present? %> <%= visually_hidden_heading_prefix %> diff --git a/app/views/components/_filter_summary.html.erb b/app/views/components/_filter_summary.html.erb index 6381bfeaf..6883c0ec5 100644 --- a/app/views/components/_filter_summary.html.erb +++ b/app/views/components/_filter_summary.html.erb @@ -11,6 +11,7 @@ component_helper = GovukPublishingComponents::Presenters::ComponentWrapperHelper.new(local_assigns) component_helper.add_class(shared_helper.get_margin_bottom) if local_assigns[:margin_bottom] + component_helper.add_data_attribute({ module: "ga4-event-tracker" }) component_helper.add_class("app-c-filter-summary") %> @@ -25,7 +26,14 @@