From 283d512854064acfd440ba703eb3af726092ea7f Mon Sep 17 00:00:00 2001 From: Ryan Brown Date: Fri, 6 Dec 2024 13:46:34 +0000 Subject: [PATCH] Omit links with tab aria role from link tracking Tabs have their own tracking built in to the component from the govuk_publishing_components gem. No need for us to duplicate it. --- .../admin/analytics-modules/ga4-link-setup.js | 4 ++++ .../admin/analytics-modules/ga4-link-setup.spec.js | 9 +++++++++ 2 files changed, 13 insertions(+) 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 5c6923c2c24..54b224be64e 100644 --- a/app/assets/javascripts/admin/analytics-modules/ga4-link-setup.js +++ b/app/assets/javascripts/admin/analytics-modules/ga4-link-setup.js @@ -12,6 +12,10 @@ window.GOVUK.analyticsGa4.analyticsModules = moduleElements.forEach(function (moduleElement) { 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') { + return + } const event = { event_name: 'navigation', type: link.role === 'button' ? 'button' : 'generic_link' 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 10fd0377777..9146fb628af 100644 --- a/spec/javascripts/admin/analytics-modules/ga4-link-setup.spec.js +++ b/spec/javascripts/admin/analytics-modules/ga4-link-setup.spec.js @@ -31,4 +31,13 @@ describe('GOVUK.analyticsGa4.analyticsModules.Ga4LinkSetup', function () { '{"event_name":"navigation","type":"button"}' ) }) + + it('excludes links that serve as tab controls', function () { + link.role = 'tab' + + const Ga4LinkSetup = GOVUK.analyticsGa4.analyticsModules.Ga4LinkSetup + Ga4LinkSetup.init() + + expect(link.dataset.ga4Link).toBeUndefined() + }) })