From b3a4d6451678b122fc7d6c5459754282008ead69 Mon Sep 17 00:00:00 2001 From: Andy Sellick Date: Wed, 18 Oct 2023 08:26:47 +0100 Subject: [PATCH 1/2] Finder tracker flatten GA4 attributes - we want to flatten the structure of GA4 data on elements that will be read by the GA4 trackers, because that data is then fitted automatically into the right place in the schema - in this situation `setFilterIndexes` creates a JSON object to attach to an element, containing the index parameters - but we don't want to flatten that, because it then gets read by `addFilterButtonTracking` in the same file, which then generates the data to attach to an element that gets read by a tracker - instead we flatten the output of `addFilterButtonTracking` --- app/assets/javascripts/analytics-ga4/ga4-finder-tracker.js | 6 +++++- spec/javascripts/components/expander-spec.js | 6 ++---- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/app/assets/javascripts/analytics-ga4/ga4-finder-tracker.js b/app/assets/javascripts/analytics-ga4/ga4-finder-tracker.js index f0f0b6c98..e4ef216d6 100644 --- a/app/assets/javascripts/analytics-ga4/ga4-finder-tracker.js +++ b/app/assets/javascripts/analytics-ga4/ga4-finder-tracker.js @@ -167,7 +167,11 @@ try { var index = button.closest('[data-ga4-index]') || undefined if (index) { - ga4JSON.index = JSON.parse(index.getAttribute('data-ga4-index')) + var indexData = JSON.parse(index.getAttribute('data-ga4-index')) + // flatten the attributes in index into the main data + for (var prop in indexData) { + ga4JSON[prop] = indexData[prop] + } } else { console.warn('No data-ga4-index found on the following element or its parents:') console.warn(button) diff --git a/spec/javascripts/components/expander-spec.js b/spec/javascripts/components/expander-spec.js index 70dc665c3..6c31840c8 100644 --- a/spec/javascripts/components/expander-spec.js +++ b/spec/javascripts/components/expander-spec.js @@ -145,10 +145,8 @@ describe('An expander module', function () { event_name: 'select_content', type: 'finder', section: 'Organisation', - index: { - index_section: 1, - index_section_count: 3 - } + index_section: 1, + index_section_count: 3 }) expect($button.attr('data-ga4-event')).toEqual(expected) From 729fcc619ef8be49dde32182cc73fad17e747c25 Mon Sep 17 00:00:00 2001 From: Andy Sellick Date: Wed, 18 Oct 2023 09:03:03 +0100 Subject: [PATCH 2/2] Mobile filters flatten GA4 attributes --- app/assets/javascripts/modules/mobile-filters-modal.js | 6 ++---- spec/javascripts/modules/mobile-filters-modal.spec.js | 6 ++---- 2 files changed, 4 insertions(+), 8 deletions(-) diff --git a/app/assets/javascripts/modules/mobile-filters-modal.js b/app/assets/javascripts/modules/mobile-filters-modal.js index b93a6f7e1..1eea20775 100644 --- a/app/assets/javascripts/modules/mobile-filters-modal.js +++ b/app/assets/javascripts/modules/mobile-filters-modal.js @@ -97,10 +97,8 @@ window.GOVUK.Modules = window.GOVUK.Modules || {}; type: 'finder', text: 'Filter', section: 'Filter', - index: { - index_section: 0, - index_section_count: indexSectionCount - } + index_section: 0, + index_section_count: indexSectionCount })) } diff --git a/spec/javascripts/modules/mobile-filters-modal.spec.js b/spec/javascripts/modules/mobile-filters-modal.spec.js index 42da78329..72fa767aa 100644 --- a/spec/javascripts/modules/mobile-filters-modal.spec.js +++ b/spec/javascripts/modules/mobile-filters-modal.spec.js @@ -142,10 +142,8 @@ describe('Mobile filters', function () { type: 'finder', text: 'Filter', section: 'Filter', - index: { - index_section: 0, - index_section_count: 5 - } + index_section: 0, + index_section_count: 5 } expect(button.getAttribute('data-ga4-event')).toEqual(JSON.stringify(expected)) })