From 96cca173ea51d43648f2f7a5c8e5bcba2cfde534 Mon Sep 17 00:00:00 2001 From: AshGDS <8880610+AshGDS@users.noreply.github.com> Date: Tue, 19 Sep 2023 11:53:06 +0100 Subject: [PATCH] Split data-ga4-section into two attributes so that sections can be set on individual filter inputs Previously, data-ga4-section was used to set indexes on filter parent elements, as well as give a label to the filter. However, there has been a request for the labels to be more granual. For example, instead of having one label Topics, have a label Topic and Sub-topic on the individual filters, rather than one label on their parent div. To achieve this, data-ga4-section is now used to set the label on each input. data-ga4-filter-parent now exists in order to identify where to calculate the indexes for each filter. --- .../analytics-ga4/ga4-finder-tracker.js | 15 ++++++++------- .../javascripts/modules/mobile-filters-modal.js | 2 +- app/views/components/_date_filter.html.erb | 10 ++++++++-- app/views/components/_expander.html.erb | 2 +- app/views/components/_option_select.html.erb | 5 ++--- app/views/finders/_radio_facet.html.erb | 2 +- app/views/finders/_taxon_facet.html.erb | 8 +++++--- docs/analytics-ga4/ga4-filter-expand-collapse.md | 2 +- docs/analytics-ga4/ga4-finder-tracker.md | 6 +++--- .../analytics-ga4/ga4-finder-tracker.spec.js | 10 +++++++--- 10 files changed, 37 insertions(+), 25 deletions(-) diff --git a/app/assets/javascripts/analytics-ga4/ga4-finder-tracker.js b/app/assets/javascripts/analytics-ga4/ga4-finder-tracker.js index 76a4932eec..f0f0b6c98b 100644 --- a/app/assets/javascripts/analytics-ga4/ga4-finder-tracker.js +++ b/app/assets/javascripts/analytics-ga4/ga4-finder-tracker.js @@ -7,7 +7,7 @@ GOVUK.analyticsGa4.Ga4FinderTracker = { - // Finds the parent div containing the filters. Loops through each child div that has data-ga4-section on it . Sets an index on each of these child divs. + // Finds the parent div containing the filters. Loops through each child div that has data-ga4-filter-parent on it . Sets an index on each of these child divs. setFilterIndexes: function () { var filterContainer = document.querySelector('[data-ga4-filter-container]') @@ -15,10 +15,10 @@ return } - var filterSections = filterContainer.querySelectorAll('[data-ga4-section]') + var filterParents = filterContainer.querySelectorAll('[data-ga4-filter-parent]') - for (var i = 0; i < filterSections.length; i++) { - filterSections[i].setAttribute('data-ga4-index', JSON.stringify({ index_section: i + 1, index_section_count: filterSections.length })) + for (var i = 0; i < filterParents.length; i++) { + filterParents[i].setAttribute('data-ga4-index', JSON.stringify({ index_section: i + 1, index_section_count: filterParents.length })) } window.GOVUK.triggerEvent(window, 'ga4-filter-indexes-added') @@ -28,6 +28,7 @@ // changeEventMetadata is a string referring to the type of form change and the element type that triggered it. For example 'update-filter checkbox'. trackChangeEvent: function (eventTarget, changeEventMetadata) { changeEventMetadata = changeEventMetadata.split(' ') + var filterParent = eventTarget.closest('[data-ga4-filter-parent]') var section = eventTarget.closest('[data-ga4-section]') var changeType = changeEventMetadata[0] var elementType = changeEventMetadata[1] @@ -45,7 +46,7 @@ var elementValue = elementInfo.elementValue data.text = elementValue var wasFilterRemoved = elementInfo.wasFilterRemoved - data = this.setSchemaBasedOnChangeType(data, elementValue, elementType, wasFilterRemoved, changeType, section) + data = this.setSchemaBasedOnChangeType(data, elementValue, elementType, wasFilterRemoved, changeType, section, filterParent) var schemas = new window.GOVUK.analyticsGa4.Schemas() var schema = schemas.mergeProperties(data, 'event_data') @@ -107,7 +108,7 @@ }, // Takes the GTM schema, the event target value, the event target HTML type, whether the filter was removed, the type of filter change it was, and the parent section heading. Populates the GTM object based on these values. - setSchemaBasedOnChangeType: function (schema, elementValue, elementType, wasFilterRemoved, changeType, section) { + setSchemaBasedOnChangeType: function (schema, elementValue, elementType, wasFilterRemoved, changeType, section, filterParent) { var PIIRemover = new window.GOVUK.analyticsGa4.PIIRemover() switch (changeType) { @@ -121,7 +122,7 @@ schema.text = elementType === 'text' ? undefined : elementValue } else { schema.action = elementType === 'text' ? 'search' : 'select' - schema.index = this.getSectionIndex(section) + schema.index = this.getSectionIndex(filterParent) } break diff --git a/app/assets/javascripts/modules/mobile-filters-modal.js b/app/assets/javascripts/modules/mobile-filters-modal.js index a725130523..b93a6f7e1b 100644 --- a/app/assets/javascripts/modules/mobile-filters-modal.js +++ b/app/assets/javascripts/modules/mobile-filters-modal.js @@ -90,7 +90,7 @@ window.GOVUK.Modules = window.GOVUK.Modules || {}; } MobileFiltersModal.prototype.addGa4Tracking = function () { - var indexSectionCount = document.querySelectorAll('[data-ga4-section]').length + var indexSectionCount = document.querySelectorAll('[data-ga4-filter-parent]').length this.triggerElement.setAttribute('data-ga4-event', JSON.stringify({ event_name: 'select_content', diff --git a/app/views/components/_date_filter.html.erb b/app/views/components/_date_filter.html.erb index 2e4074dd5d..3e8ac00e9f 100644 --- a/app/views/components/_date_filter.html.erb +++ b/app/views/components/_date_filter.html.erb @@ -19,7 +19,10 @@ value: from_value, controls: aria_controls_id, hint: "For example, 2005 or 21/11/2014", - error_message: date_errors_from + error_message: date_errors_from, + data: { + ga4_section: name + " after" + } } %> <%= render "govuk_publishing_components/components/input", { @@ -31,7 +34,10 @@ value: to_value, controls: aria_controls_id, hint: "For example, 2005 or 21/11/2014", - error_message: date_errors_to + error_message: date_errors_to, + data: { + ga4_section: name + " before" + } } %> <% end %> diff --git a/app/views/components/_expander.html.erb b/app/views/components/_expander.html.erb index 509df4fe21..a41e402490 100644 --- a/app/views/components/_expander.html.erb +++ b/app/views/components/_expander.html.erb @@ -11,7 +11,7 @@ css_classes << (shared_helper.get_margin_bottom) unless margin_bottom == 0 %> <% if title %> - <%= tag.div class: css_classes, data: { module: "expander", 'open-on-load': open_on_load, 'ga4-section': title } do %> + <%= tag.div class: css_classes, data: { module: "expander", 'open-on-load': open_on_load, 'ga4-filter-parent': '' } do %>