From 4edff50cba6e3b1f57428e3aad0e5243d1272908 Mon Sep 17 00:00:00 2001 From: AshGDS Date: Thu, 3 Nov 2022 15:53:20 +0000 Subject: [PATCH] Add Step by Step nav GA4 tracking Enables GA4 tracking with the presence of 'ga4_tracking: true' on the component. ga4-event-tracker and data-ga4-expandable are then added to the template. We are then using JS to add all the GA4 JSONS. There is no use putting them on the HTML template, as the HTML gets reconstructed by JavaScript anyway in order to create the buttons. --- CHANGELOG.md | 2 +- .../components/step-by-step-nav.js | 22 +++++++++++- .../components/_step_by_step_nav.html.erb | 5 ++- .../components/docs/step_by_step_nav.yml | 34 +++++++++++++++++++ spec/components/step_by_step_nav_spec.rb | 7 ++++ .../components/step-by-step-nav-spec.js | 26 +++++++++++++- 6 files changed, 92 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7c3ba74d95..2edf15ade9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,7 @@ ## Unreleased * Add the keyboard shim for link buttons ([PR #3027](https://github.com/alphagov/govuk_publishing_components/pull/3027)) +* Add step by step nav GA4 tracking ([PR #3052](https://github.com/alphagov/govuk_publishing_components/pull/3052)) ## 33.0.0 @@ -22,7 +23,6 @@ * Simplify the way ga4 tracking is added to accordions ([PR #3082](https://github.com/alphagov/govuk_publishing_components/pull/3082)) * Rename section and themes property names ([PR #3092](https://github.com/alphagov/govuk_publishing_components/pull/3092)) - ## 32.1.0 * Calculate viewport width correctly for navbar in Chrome and Firefox when Mac scrollbars are enabled ([PR #3016](https://github.com/alphagov/govuk_publishing_components/pull/3016)) diff --git a/app/assets/javascripts/govuk_publishing_components/components/step-by-step-nav.js b/app/assets/javascripts/govuk_publishing_components/components/step-by-step-nav.js index 6e93f9a312..1b6270f7bf 100644 --- a/app/assets/javascripts/govuk_publishing_components/components/step-by-step-nav.js +++ b/app/assets/javascripts/govuk_publishing_components/components/step-by-step-nav.js @@ -36,6 +36,9 @@ window.GOVUK.Modules = window.GOVUK.Modules || {}; this.$module.uniqueId = this.$module.getAttribute('data-id') || false + this.$module.dataModule = this.$module.getAttribute('data-module') + this.$module.isGa4Enabled = this.$module.dataModule ? this.$module.dataModule.indexOf('ga4-event-tracker') !== -1 : false + if (this.$module.uniqueId) { this.$module.sessionStoreLink = this.$module.sessionStoreLink + '_' + this.$module.uniqueId } @@ -78,6 +81,14 @@ window.GOVUK.Modules = window.GOVUK.Modules || {}; this.$module.insertBefore(showAll, steps) this.$module.showOrHideAllButton = this.$module.querySelectorAll('.js-step-controls-button')[0] + + // if GA4 is enabled, set attributes on 'show all sections' for tracking using ga4-event-tracker + if (this.$module.isGa4Enabled) { + var indexTotal = this.$module.totalSteps + var showAllAttributesGa4 = { event_name: 'select_content', type: 'step by step', index: 0, index_total: indexTotal } + + this.$module.showOrHideAllButton.setAttribute('data-ga4-event', JSON.stringify(showAllAttributesGa4)) + } } Gemstepnav.prototype.addShowHideToggle = function () { @@ -170,11 +181,20 @@ window.GOVUK.Modules = window.GOVUK.Modules || {}; var contentId = thisel.querySelectorAll('.js-panel')[0].getAttribute('id') var titleText = title.textContent || title.innerText // IE8 fallback + if (this.$module.isGa4Enabled) { + var indexTotal = this.$module.totalSteps + var trimmedTitle = titleText.trim() + var ga4Index = i + 1 + var ga4JSON = { event_name: 'select_content', type: 'step by step', text: trimmedTitle, index: ga4Index, index_total: indexTotal } + } + + var ga4Data = this.$module.isGa4Enabled ? "data-ga4-expandable data-ga4-event='" + JSON.stringify(ga4JSON) + "'" : '' // Construct GA4 data-attributes for ga4-event-tracker. + title.outerHTML = '' + '