From 36e483bf0f3b105e816d19e1b71d4e2e73e8f55a Mon Sep 17 00:00:00 2001 From: AshGDS <8880610+AshGDS@users.noreply.github.com> Date: Tue, 9 Jul 2024 17:45:52 +0100 Subject: [PATCH 1/3] Reintroduce GA4 pageview tracking code This was accidentally removed during UA removal. --- app/assets/javascripts/live_search.js | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/app/assets/javascripts/live_search.js b/app/assets/javascripts/live_search.js index 1114c7727..aca385266 100644 --- a/app/assets/javascripts/live_search.js +++ b/app/assets/javascripts/live_search.js @@ -116,11 +116,16 @@ } } + LiveSearch.prototype.trackingInit = function trackingInit () { + GOVUK.modules.start(this.$resultsWrapper) + this.startEnhancedEcommerceTracking() + } + LiveSearch.prototype.Ga4EcommerceTracking = function (referrer) { if (GOVUK.analyticsGa4 && GOVUK.analyticsGa4.Ga4EcommerceTracker) { var consentCookie = GOVUK.getConsentCookie() - if (consentCookie && consentCookie.settings) { + if (consentCookie && consentCookie.usage) { if (this.$resultsWrapper) { this.$resultsWrapper.setAttribute('data-ga4-search-query', this.currentKeywords()) var sortedBy = this.$resultsWrapper.querySelector('.js-order-results') @@ -129,7 +134,6 @@ this.$resultsWrapper.setAttribute('data-ga4-ecommerce-variant', sortedBy.options[sortedBy.selectedIndex].text) } } - GOVUK.analyticsGa4.Ga4EcommerceTracker.init(referrer) } else { window.addEventListener('cookie-consent', function () { @@ -355,6 +359,7 @@ liveSearch.cache(liveSearch.serializeState(liveSearch.state), response) liveSearch.ga4TrackFormChange(formChangeEvent) // must be before displayResults changes the DOM, otherwise will break formChangeEvent.target.closest liveSearch.displayResults(response, searchState) + liveSearch.trackingInit() } else { liveSearch.showErrorIndicator() } @@ -368,6 +373,7 @@ this.updateUrl() this.ga4TrackFormChange(formChangeEvent) // must be before displayResults changes the DOM, otherwise will break formChangeEvent.target.closest this.displayResults(cachedResultData, searchState) + this.trackingInit() } } @@ -541,7 +547,7 @@ if (GOVUK.analyticsGa4 && GOVUK.analyticsGa4.Ga4FinderTracker) { var consentCookie = GOVUK.getConsentCookie() - if (consentCookie && consentCookie.settings) { + if (consentCookie && consentCookie.usage) { GOVUK.analyticsGa4.Ga4FinderTracker.trackChangeEvent(event.target, ga4ChangeCategory) } } From 3217cd6f85bf8594797975c42a6d339d6aabde47 Mon Sep 17 00:00:00 2001 From: AshGDS <8880610+AshGDS@users.noreply.github.com> Date: Tue, 9 Jul 2024 17:57:43 +0100 Subject: [PATCH 2/3] Namespace functions with GA4 --- app/assets/javascripts/live_search.js | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/app/assets/javascripts/live_search.js b/app/assets/javascripts/live_search.js index aca385266..ebdecf379 100644 --- a/app/assets/javascripts/live_search.js +++ b/app/assets/javascripts/live_search.js @@ -106,7 +106,7 @@ } } - LiveSearch.prototype.startEnhancedEcommerceTracking = function startEnhancedEcommerceTracking () { + LiveSearch.prototype.restartGa4EcommerceTracking = function restartGa4EcommerceTracking () { if (document.readyState === 'complete') { this.Ga4EcommerceTracking(this.previousSearchUrl) } else { @@ -116,9 +116,9 @@ } } - LiveSearch.prototype.trackingInit = function trackingInit () { + LiveSearch.prototype.reinitialiseGa4Tracking = function reinitialiseGa4Tracking () { GOVUK.modules.start(this.$resultsWrapper) - this.startEnhancedEcommerceTracking() + this.restartGa4EcommerceTracking() } LiveSearch.prototype.Ga4EcommerceTracking = function (referrer) { @@ -359,7 +359,7 @@ liveSearch.cache(liveSearch.serializeState(liveSearch.state), response) liveSearch.ga4TrackFormChange(formChangeEvent) // must be before displayResults changes the DOM, otherwise will break formChangeEvent.target.closest liveSearch.displayResults(response, searchState) - liveSearch.trackingInit() + liveSearch.reinitialiseGa4Tracking() } else { liveSearch.showErrorIndicator() } @@ -373,7 +373,7 @@ this.updateUrl() this.ga4TrackFormChange(formChangeEvent) // must be before displayResults changes the DOM, otherwise will break formChangeEvent.target.closest this.displayResults(cachedResultData, searchState) - this.trackingInit() + this.reinitialiseGa4Tracking() } } From 668db213f8d4fa45b379e198a5d36dcd15af84c5 Mon Sep 17 00:00:00 2001 From: AshGDS <8880610+AshGDS@users.noreply.github.com> Date: Tue, 9 Jul 2024 18:27:14 +0100 Subject: [PATCH 3/3] Add test to ensure GA4 is reinitialised when search updates --- spec/javascripts/live_search_spec.js | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/spec/javascripts/live_search_spec.js b/spec/javascripts/live_search_spec.js index 199bf5ff9..51a25594d 100644 --- a/spec/javascripts/live_search_spec.js +++ b/spec/javascripts/live_search_spec.js @@ -793,6 +793,25 @@ describe('liveSearch', function () { expect(window.GOVUK.analyticsGa4.Ga4FinderTracker.trackChangeEvent).not.toHaveBeenCalled() }) + + it('reinitialises tracking when search is updated', function () { + spyOn(liveSearch, 'reinitialiseGa4Tracking') + + expect(liveSearch.reinitialiseGa4Tracking).not.toHaveBeenCalled() + + var $input = $form.find('input[name="field"]') + $input.attr('data-ga4-change-category', 'update-sort select') + + liveSearch.state = [] + + liveSearch.formChange({ target: $input[0] }) + jasmine.Ajax.requests.mostRecent().respondWith({ + status: 200, + response: '{"total":81,"display_total":"81 reports","facet_tags":"","search_results":"","display_selected_facets_count":"","sort_options_markup":"","next_and_prev_links":"","suggestions":"","errors":{}}' + }) + + expect(liveSearch.reinitialiseGa4Tracking).toHaveBeenCalled() + }) }) describe('meta tag updating', function () {