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 = '' + '