From a5e958deb7f2c87dfb2ee9096f6f75c371aa457c Mon Sep 17 00:00:00 2001 From: Christian Sutter Date: Fri, 18 Oct 2024 15:19:18 +0000 Subject: [PATCH] GA4: Fix ecommerce tracking of searches without query The GA4 ecommerce tracking for search results doesn't currently work if the user is searching for an empty query, as the check for whether the `ga4-search-query` data attribute is present uses loose Javascript boolean logic (i.e. returning false for an empty string). This amends it to strictly check for `null` instead (data attribute not present at all), and adds some tests for the search ecommerce data being properly set with the query present and absent. --- CHANGELOG.md | 4 +++ .../analytics-ga4/ga4-core.js | 2 +- .../analytics-ga4/ga4-core.spec.js | 30 +++++++++++++++++++ 3 files changed, 35 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 33174acb8c..1d3934d015 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,10 @@ useful summary for people upgrading their application, not a replication of the commit log. +## Unreleased + +* Fix ecommerce tracking of searches without query ([PR #4317](https://github.com/alphagov/govuk_publishing_components/pull/4317)) + ## 44.4.1 * Add chartkick path to gemspec ([PR #4312](https://github.com/alphagov/govuk_publishing_components/pull/4312)) diff --git a/app/assets/javascripts/govuk_publishing_components/analytics-ga4/ga4-core.js b/app/assets/javascripts/govuk_publishing_components/analytics-ga4/ga4-core.js index f92e83b507..6265794f9d 100644 --- a/app/assets/javascripts/govuk_publishing_components/analytics-ga4/ga4-core.js +++ b/app/assets/javascripts/govuk_publishing_components/analytics-ga4/ga4-core.js @@ -333,7 +333,7 @@ window.GOVUK.analyticsGa4 = window.GOVUK.analyticsGa4 || {}; var element = data.element var resultsId = data.resultsId var isClickEvent = data.event !== undefined - var isSearchResult = element.getAttribute('data-ga4-search-query') + var isSearchResult = element.getAttribute('data-ga4-search-query') !== null var ecommerceSchema = new window.GOVUK.analyticsGa4.Schemas().ecommerceSchema() var DEFAULT_LIST_TITLE = 'Smart answer results' diff --git a/spec/javascripts/govuk_publishing_components/analytics-ga4/ga4-core.spec.js b/spec/javascripts/govuk_publishing_components/analytics-ga4/ga4-core.spec.js index e4eabd609a..c89c40ec04 100644 --- a/spec/javascripts/govuk_publishing_components/analytics-ga4/ga4-core.spec.js +++ b/spec/javascripts/govuk_publishing_components/analytics-ga4/ga4-core.spec.js @@ -629,6 +629,36 @@ describe('GA4 core', function () { expect(builtEcommerceObject).toEqual(expectedEcommerceObject) }) + it('tracks variant and term for search results', function () { + resultsParentEl.setAttribute('data-ga4-search-query', 'search term') + resultsParentEl.setAttribute('data-ga4-ecommerce-variant', 'upside-down') + + expectedEcommerceObject.search_results.term = 'search term' + expectedEcommerceObject.search_results.sort = 'upside-down' + + var builtEcommerceObject = GOVUK.analyticsGa4.core.ecommerceHelperFunctions.populateEcommerceSchema({ + element: resultsParentEl, + resultsId: 'result-count' + }) + + expect(builtEcommerceObject).toEqual(expectedEcommerceObject) + }) + + it('tracks variant and term for search results even when query is blank', function () { + resultsParentEl.setAttribute('data-ga4-search-query', '') + resultsParentEl.setAttribute('data-ga4-ecommerce-variant', 'upside-down') + + expectedEcommerceObject.search_results.term = undefined + expectedEcommerceObject.search_results.sort = 'upside-down' + + var builtEcommerceObject = GOVUK.analyticsGa4.core.ecommerceHelperFunctions.populateEcommerceSchema({ + element: resultsParentEl, + resultsId: 'result-count' + }) + + expect(builtEcommerceObject).toEqual(expectedEcommerceObject) + }) + it('the ecommerce items array is limited to a maximum of 15,000 UTF-16 code units', function () { var innerHTML = '' var ecommerceItems = []