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 4a58dea4b5..29c1d2d07f 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 @@ -66,7 +66,6 @@ window.GOVUK.Modules = window.GOVUK.Modules || {}; Gemstepnav.prototype.addShowHideAllButton = function () { var showAll = document.createElement('div') var steps = this.$module.querySelectorAll('.gem-c-step-nav__steps')[0] - var showAllGa4Data = this.$module.getAttribute('data-ga4-show-all-attributes') showAll.className = 'gem-c-step-nav__controls govuk-!-display-none-print' showAll.innerHTML = @@ -80,10 +79,14 @@ window.GOVUK.Modules = window.GOVUK.Modules || {}; this.$module.insertBefore(showAll, steps) this.$module.showOrHideAllButton = this.$module.querySelectorAll('.js-step-controls-button')[0] - if (showAllGa4Data) { - this.$module.showOrHideAllButton.setAttribute('data-ga4-event', showAllGa4Data) - this.$module.showOrHideAllButton.setAttribute('data-ga4-expandable', '') // Enables tracking of aria-expanded in ga4-event-tracker. - this.$module.removeAttribute('data-ga4-show-all-attributes') // Remove it to prevent repetition. The JSON lives there temporarily until the HTML it is needed on is rendered by this JS. + // if GA4 is enabled, set attributes on 'show all sections' for tracking using ga4-event-tracker + var dataModule = this.$module.getAttribute('data-module') + var isGa4Enabled = dataModule ? dataModule.indexOf('ga4-event-tracker') !== -1 : false + if (isGa4Enabled) { + var indexTotal = this.$module.querySelectorAll('li.gem-c-step-nav__step').length + 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)) } } diff --git a/app/views/govuk_publishing_components/components/_step_by_step_nav.html.erb b/app/views/govuk_publishing_components/components/_step_by_step_nav.html.erb index 472cb245fb..2997b46d31 100644 --- a/app/views/govuk_publishing_components/components/_step_by_step_nav.html.erb +++ b/app/views/govuk_publishing_components/components/_step_by_step_nav.html.erb @@ -16,20 +16,12 @@ step_nav_helper = GovukPublishingComponents::Presenters::StepByStepNavHelper.new - ga4_tracking ||= nil - ga4_show_all_json ||= nil - if ga4_tracking - ga4_show_all_json = { - event_name: 'select_content', - type: 'step by step', - index: 0, - index_total: steps.length - }.to_json - end + ga4_tracking ||= false %> <% if steps %>