Skip to content

Commit

Permalink
Merge pull request #9484 from alphagov/update-link-and-button-tracking
Browse files Browse the repository at this point in the history
Updates to link and button tracking
  • Loading branch information
dnkrj authored Oct 9, 2024
2 parents d4b4080 + bfbe25c commit d105610
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"}'
)
})

Expand All @@ -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"}'
)
})

Expand All @@ -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"}'
)
})

Expand All @@ -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"}'
)
})
})
13 changes: 12 additions & 1 deletion spec/javascripts/admin/analytics-modules/ga4-link-setup.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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"}'
)
})
})

0 comments on commit d105610

Please sign in to comment.