Skip to content

Commit

Permalink
Merge pull request #9651 from alphagov/track-all-links-and-buttons
Browse files Browse the repository at this point in the history
Track all links and buttons in Whitehall as per agreed event schema
  • Loading branch information
ryanb-gds authored Dec 6, 2024
2 parents e612754 + 283d512 commit 54f6ad0
Show file tree
Hide file tree
Showing 10 changed files with 34 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,13 @@ window.GOVUK.analyticsGa4.analyticsModules =
"[data-module~='ga4-button-setup']"
)
moduleElements.forEach(function (moduleElement) {
const buttons = moduleElement.querySelectorAll(
'button, [role="button"]'
)
const buttons = moduleElement.querySelectorAll('button')
buttons.forEach((button) => {
const event = {
event_name: 'navigation',
event_name:
button.type === 'submit' ? 'navigation' : 'select_content',
type: 'button',
text: button.textContent,
method: 'primary_click'
text: button.textContent
}
if (button.dataset.ga4Event) {
Object.assign(event, JSON.parse(button.dataset.ga4Event))
Expand Down
13 changes: 8 additions & 5 deletions app/assets/javascripts/admin/analytics-modules/ga4-link-setup.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,18 @@ 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',
method: 'primary_click'
type: link.role === 'button' ? 'button' : 'generic_link'
}
if (link.dataset.ga4Event) {
Object.assign(event, JSON.parse(link.dataset.ga4Event))
if (link.dataset.ga4Link) {
Object.assign(event, JSON.parse(link.dataset.ga4Link))
}
link.dataset.ga4Event = JSON.stringify(event)
link.dataset.ga4Link = JSON.stringify(event)
})
})
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<div class="app-view-summary__sidebar-actions" data-module="ga4-button-setup">
<div class="app-view-summary__sidebar-actions">
<%= render "govuk_publishing_components/components/list", {
extra_spacing: true,
items: actions,
Expand Down
2 changes: 1 addition & 1 deletion app/helpers/admin/editions_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ def lead_organisation_id_at_index(edition, index)
end

def standard_edition_form(edition)
form_for form_url_for_edition(edition), as: :edition, html: { class: edition_form_classes(edition), multipart: true }, data: { module: "EditionForm LocaleSwitcher ga4-button-setup ga4-visual-editor-event-handlers", "rtl-locales": Locale.right_to_left.collect(&:to_param) } do |form|
form_for form_url_for_edition(edition), as: :edition, html: { class: edition_form_classes(edition), multipart: true }, data: { module: "EditionForm LocaleSwitcher ga4-visual-editor-event-handlers", "rtl-locales": Locale.right_to_left.collect(&:to_param) } do |form|
concat render("standard_fields", form:, edition:)
yield(form)
concat render("settings_fields", form:, edition:)
Expand Down
2 changes: 1 addition & 1 deletion app/views/admin/attachments/_form.html.erb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<%= form_for attachment, url: [:admin, typecast_for_attachable_routing(attachable), attachment.becomes(Attachment)], as: :attachment, html: { :class => "app-view-attachments__form", data: { module: "LocaleSwitcher ga4-button-setup ga4-visual-editor-event-handlers", "rtl-locales": Locale.right_to_left.collect(&:to_param) }}, multipart: true do |form| %>
<%= form_for attachment, url: [:admin, typecast_for_attachable_routing(attachable), attachment.becomes(Attachment)], as: :attachment, html: { :class => "app-view-attachments__form", data: { module: "LocaleSwitcher ga4-visual-editor-event-handlers", "rtl-locales": Locale.right_to_left.collect(&:to_param) }}, multipart: true do |form| %>
<div class="govuk-!-margin-bottom-8 app-view-attachments__form-title js-locale-switcher-field">
<%= render "govuk_publishing_components/components/input", {
label: {
Expand Down
2 changes: 1 addition & 1 deletion app/views/admin/new_document/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<div class="govuk-grid-row">
<div class="govuk-grid-column-two-thirds">
<%= form_with url: admin_new_document_options_path, data: {
module: "ga4-form-setup ga4-form-tracker ga4-link-setup ga4-link-tracker",
module: "ga4-form-setup ga4-form-tracker",
} do %>
<%= render "govuk_publishing_components/components/radio", {
heading: "New document",
Expand Down
2 changes: 1 addition & 1 deletion app/views/layouts/design_system.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

<%= render partial: "shared/header" %>

<div class="govuk-width-container" data-module="ga4-event-tracker ga4-paste-tracker">
<div class="govuk-width-container" data-module="ga4-event-tracker ga4-paste-tracker ga4-link-setup ga4-link-tracker ga4-button-setup">
<%= render "shared/phase_banner", {
show_feedback_banner: t("admin.feedback.show_banner"),
} %>
Expand Down
2 changes: 1 addition & 1 deletion app/views/shared/_header.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
],
} %>

<div class="govuk-width-container">
<div class="govuk-width-container" data-module="ga4-link-setup ga4-link-tracker">
<%= render "components/sub_navigation", {
items: [
sub_nav_item("New document", admin_new_document_path),
Expand Down
20 changes: 3 additions & 17 deletions spec/javascripts/admin/analytics-modules/ga4-button-setup.spec.js
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":"button","text":"Button","method":"primary_click"}'
'{"event_name":"select_content","type":"button","text":"Button"}'
)
})

Expand All @@ -29,7 +29,7 @@ describe('GOVUK.analyticsGa4.analyticsModules', function () {
ga4ButtonSetup.init()

expect(button.dataset.ga4Event).toEqual(
'{"event_name":"navigation","type":"button","text":"Button","method":"primary_click"}'
'{"event_name":"navigation","type":"button","text":"Button"}'
)
})

Expand All @@ -42,21 +42,7 @@ describe('GOVUK.analyticsGa4.analyticsModules', function () {
ga4ButtonSetup.init()

expect(button.dataset.ga4Event).toEqual(
'{"event_name":"custom_event_name","type":"button","text":"Button","method":"primary_click"}'
)
})

it('adds ga4 event data to nodes with the button role', function () {
const link = document.createElement('a')
link.textContent = 'Link'
link.role = 'button'
button.replaceWith(link)

const ga4ButtonSetup = GOVUK.analyticsGa4.analyticsModules.Ga4ButtonSetup
ga4ButtonSetup.init()

expect(link.dataset.ga4Event).toEqual(
'{"event_name":"navigation","type":"button","text":"Link","method":"primary_click"}'
'{"event_name":"custom_event_name","type":"button","text":"Button"}'
)
})
})
17 changes: 13 additions & 4 deletions spec/javascripts/admin/analytics-modules/ga4-link-setup.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ describe('GOVUK.analyticsGa4.analyticsModules.Ga4LinkSetup', function () {
const Ga4LinkSetup = GOVUK.analyticsGa4.analyticsModules.Ga4LinkSetup
Ga4LinkSetup.init()

expect(link.dataset.ga4Event).toEqual(
'{"event_name":"navigation","type":"generic_link","method":"primary_click"}'
expect(link.dataset.ga4Link).toEqual(
'{"event_name":"navigation","type":"generic_link"}'
)
})

Expand All @@ -27,8 +27,17 @@ describe('GOVUK.analyticsGa4.analyticsModules.Ga4LinkSetup', function () {
const Ga4LinkSetup = GOVUK.analyticsGa4.analyticsModules.Ga4LinkSetup
Ga4LinkSetup.init()

expect(link.dataset.ga4Event).toEqual(
'{"event_name":"navigation","type":"button","method":"primary_click"}'
expect(link.dataset.ga4Link).toEqual(
'{"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()
})
})

0 comments on commit 54f6ad0

Please sign in to comment.