From b2977881431742dc4362905f7fd7a7f96f679d89 Mon Sep 17 00:00:00 2001 From: Ryan Brown Date: Fri, 6 Dec 2024 16:35:28 +0000 Subject: [PATCH] Prevent navigation events from being tracked on tab controls It would be preferable to use the role ARIA attribute to do this, but it's not present yet when this module is initialised. This is because component modules are initialised after analytics and the tabs component is responsible for adding the role via javascript during its initialisation. Analytics modules are initialised before components by GOV.UK Publishing components. --- .../javascripts/admin/analytics-modules/ga4-link-setup.js | 5 ++++- .../admin/analytics-modules/ga4-link-setup.spec.js | 2 +- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/app/assets/javascripts/admin/analytics-modules/ga4-link-setup.js b/app/assets/javascripts/admin/analytics-modules/ga4-link-setup.js index 54b224be64e..c04b3a9d01f 100644 --- a/app/assets/javascripts/admin/analytics-modules/ga4-link-setup.js +++ b/app/assets/javascripts/admin/analytics-modules/ga4-link-setup.js @@ -13,7 +13,10 @@ window.GOVUK.analyticsGa4.analyticsModules = const links = moduleElement.querySelectorAll('a') links.forEach((link) => { // Exclude links that serve as tab controls as they have their own event tracking - if (link.role === 'tab') { + // It would be preferable to use the role ARIA attribute to do this, but it's not present yet + // when this module is initialised because the tabs component adds the role. + // Component modules are initialised after analytics modules by GOV.UK Publishing components + if (link.classList.contains('govuk-tabs__tab')) { return } const event = { diff --git a/spec/javascripts/admin/analytics-modules/ga4-link-setup.spec.js b/spec/javascripts/admin/analytics-modules/ga4-link-setup.spec.js index 9146fb628af..3a7faeec4e1 100644 --- a/spec/javascripts/admin/analytics-modules/ga4-link-setup.spec.js +++ b/spec/javascripts/admin/analytics-modules/ga4-link-setup.spec.js @@ -33,7 +33,7 @@ describe('GOVUK.analyticsGa4.analyticsModules.Ga4LinkSetup', function () { }) it('excludes links that serve as tab controls', function () { - link.role = 'tab' + link.classList.add('govuk-tabs__tab') const Ga4LinkSetup = GOVUK.analyticsGa4.analyticsModules.Ga4LinkSetup Ga4LinkSetup.init()