From bfbe25cf7a542e7e8a9d860b375302ad36268980 Mon Sep 17 00:00:00 2001 From: Laura Ghiorghisor Date: Mon, 30 Sep 2024 17:40:17 +0100 Subject: [PATCH] wip --- .../admin/analytics-modules/ga4-button-setup.js | 8 ++++---- .../admin/analytics-modules/ga4-link-setup.js | 5 +++-- .../analytics-modules/ga4-button-setup.spec.js | 8 ++++---- .../admin/analytics-modules/ga4-link-setup.spec.js | 13 ++++++++++++- 4 files changed, 23 insertions(+), 11 deletions(-) diff --git a/app/assets/javascripts/admin/analytics-modules/ga4-button-setup.js b/app/assets/javascripts/admin/analytics-modules/ga4-button-setup.js index 8ed8b22c6b6..29ea2cd4ee9 100644 --- a/app/assets/javascripts/admin/analytics-modules/ga4-button-setup.js +++ b/app/assets/javascripts/admin/analytics-modules/ga4-button-setup.js @@ -15,12 +15,12 @@ window.GOVUK.analyticsGa4.analyticsModules = ) buttons.forEach((button) => { const event = { - event_name: - button.type === 'submit' ? 'form_response' : 'navigation', - type: 'generic_link', + event_name: 'navigation', + type: 'button', text: button.textContent, section: document.title.split(' - ')[0].replace('Error: ', ''), - action: button.textContent + action: button.textContent, + method: 'primary_click' } if (button.dataset.ga4Event) { Object.assign(event, JSON.parse(button.dataset.ga4Event)) 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 09abb0675fe..42f2fe4dd34 100644 --- a/app/assets/javascripts/admin/analytics-modules/ga4-link-setup.js +++ b/app/assets/javascripts/admin/analytics-modules/ga4-link-setup.js @@ -14,8 +14,9 @@ window.GOVUK.analyticsGa4.analyticsModules = links.forEach((link) => { const event = { event_name: 'navigation', - type: 'generic_link', - section: document.title.split(' - ')[0].replace('Error: ', '') + type: link.role === 'button' ? 'button' : 'generic_link', + section: document.title.split(' - ')[0].replace('Error: ', ''), + method: 'primary_click' } if (link.dataset.ga4Event) { Object.assign(event, JSON.parse(link.dataset.ga4Event)) diff --git a/spec/javascripts/admin/analytics-modules/ga4-button-setup.spec.js b/spec/javascripts/admin/analytics-modules/ga4-button-setup.spec.js index cf9dfce4377..fb8af06682c 100644 --- a/spec/javascripts/admin/analytics-modules/ga4-button-setup.spec.js +++ b/spec/javascripts/admin/analytics-modules/ga4-button-setup.spec.js @@ -18,7 +18,7 @@ describe('GOVUK.analyticsGa4.analyticsModules', function () { ga4ButtonSetup.init() expect(button.dataset.ga4Event).toEqual( - '{"event_name":"navigation","type":"generic_link","text":"Button","section":"Title","action":"Button"}' + '{"event_name":"navigation","type":"button","text":"Button","section":"Title","action":"Button","method":"primary_click"}' ) }) @@ -29,7 +29,7 @@ describe('GOVUK.analyticsGa4.analyticsModules', function () { ga4ButtonSetup.init() expect(button.dataset.ga4Event).toEqual( - '{"event_name":"form_response","type":"generic_link","text":"Button","section":"Title","action":"Button"}' + '{"event_name":"navigation","type":"button","text":"Button","section":"Title","action":"Button","method":"primary_click"}' ) }) @@ -42,7 +42,7 @@ describe('GOVUK.analyticsGa4.analyticsModules', function () { ga4ButtonSetup.init() expect(button.dataset.ga4Event).toEqual( - '{"event_name":"custom_event_name","type":"generic_link","text":"Button","section":"Title","action":"Button"}' + '{"event_name":"custom_event_name","type":"button","text":"Button","section":"Title","action":"Button","method":"primary_click"}' ) }) @@ -56,7 +56,7 @@ describe('GOVUK.analyticsGa4.analyticsModules', function () { ga4ButtonSetup.init() expect(link.dataset.ga4Event).toEqual( - '{"event_name":"navigation","type":"generic_link","text":"Link","section":"Title","action":"Link"}' + '{"event_name":"navigation","type":"button","text":"Link","section":"Title","action":"Link","method":"primary_click"}' ) }) }) 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 f9391cc209d..72337dd38d7 100644 --- a/spec/javascripts/admin/analytics-modules/ga4-link-setup.spec.js +++ b/spec/javascripts/admin/analytics-modules/ga4-link-setup.spec.js @@ -17,7 +17,18 @@ describe('GOVUK.analyticsGa4.analyticsModules.Ga4LinkSetup', function () { Ga4LinkSetup.init() expect(link.dataset.ga4Event).toEqual( - '{"event_name":"navigation","type":"generic_link","section":"Title"}' + '{"event_name":"navigation","type":"generic_link","section":"Title","method":"primary_click"}' + ) + }) + + it('sends type:"button" for links with role="button"', function () { + link.role = 'button' + + const Ga4LinkSetup = GOVUK.analyticsGa4.analyticsModules.Ga4LinkSetup + Ga4LinkSetup.init() + + expect(link.dataset.ga4Event).toEqual( + '{"event_name":"navigation","type":"button","section":"Title","method":"primary_click"}' ) }) })