From 58276d377b039fbc5fbae365198249eaed4f5d79 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 | 23 ++++++++++++- .../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, 93 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 501ea04b96..6797e30961 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,6 +13,7 @@ * Add the keyboard shim for link buttons ([PR #3027](https://github.com/alphagov/govuk_publishing_components/pull/3027)) * Remove unused axe-core option from options parameter ([PR #3094](https://github.com/alphagov/govuk_publishing_components/pull/3094)) * Minor ecommerce tracking refactor ([PR #3098](https://github.com/alphagov/govuk_publishing_components/pull/3098)) +* Add step by step nav GA4 tracking ([PR #3052](https://github.com/alphagov/govuk_publishing_components/pull/3052)) ## 33.0.0 @@ -25,7 +26,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..e8e830ccb8 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,12 @@ 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 showAllAttributesGa4 = { event_name: 'select_content', type: 'step by step', index: 0, index_total: this.$module.totalSteps } + this.$module.showOrHideAllButton.setAttribute('data-ga4-event', JSON.stringify(showAllAttributesGa4)) + } } Gemstepnav.prototype.addShowHideToggle = function () { @@ -169,12 +178,24 @@ window.GOVUK.Modules = window.GOVUK.Modules || {}; var title = thisel.querySelectorAll('.js-step-title')[0] var contentId = thisel.querySelectorAll('.js-panel')[0].getAttribute('id') var titleText = title.textContent || title.innerText // IE8 fallback + var ga4Data = '' + + if (this.$module.isGa4Enabled) { + var ga4JSON = { + event_name: 'select_content', + type: 'step by step', + text: titleText.trim(), + index: i + 1, + index_total: this.$module.totalSteps + } + ga4Data = "data-ga4-event='" + JSON.stringify(ga4JSON) + "'" // Construct GA4 data-attributes for ga4-event-tracker. + } title.outerHTML = '' + '