diff --git a/app/assets/javascripts/live_search.js b/app/assets/javascripts/live_search.js index 1114c7727..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,11 +116,16 @@ } } + LiveSearch.prototype.reinitialiseGa4Tracking = function reinitialiseGa4Tracking () { + GOVUK.modules.start(this.$resultsWrapper) + this.restartGa4EcommerceTracking() + } + 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.reinitialiseGa4Tracking() } 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.reinitialiseGa4Tracking() } } @@ -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) } } 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 () {