Skip to content

Commit

Permalink
Merge pull request #3405 from alphagov/fix-sort-ga4-bug
Browse files Browse the repository at this point in the history
Reintroduce GA4 pageview tracking code
  • Loading branch information
AshGDS authored Jul 10, 2024
2 parents ff7dac1 + 668db21 commit 5f1f54b
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 4 deletions.
14 changes: 10 additions & 4 deletions app/assets/javascripts/live_search.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@
}
}

LiveSearch.prototype.startEnhancedEcommerceTracking = function startEnhancedEcommerceTracking () {
LiveSearch.prototype.restartGa4EcommerceTracking = function restartGa4EcommerceTracking () {
if (document.readyState === 'complete') {
this.Ga4EcommerceTracking(this.previousSearchUrl)
} else {
Expand All @@ -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')
Expand All @@ -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 () {
Expand Down Expand Up @@ -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()
}
Expand All @@ -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()
}
}

Expand Down Expand Up @@ -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)
}
}
Expand Down
19 changes: 19 additions & 0 deletions spec/javascripts/live_search_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 () {
Expand Down

0 comments on commit 5f1f54b

Please sign in to comment.