From 188ed01e0bcb960117833aaa68af1dbbe0b83124 Mon Sep 17 00:00:00 2001 From: AshGDS <8880610+AshGDS@users.noreply.github.com> Date: Wed, 11 Dec 2024 15:45:30 +0000 Subject: [PATCH] Limit GA4 search term tracking to 500 characters --- .../analytics-ga4/ga4-core.js | 5 +++++ .../analytics-ga4/ga4-core.spec.js | 19 +++++++++++++++++++ 2 files changed, 24 insertions(+) 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 6265794f9d..f4e1ccc162 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 @@ -340,6 +340,11 @@ window.GOVUK.analyticsGa4 = window.GOVUK.analyticsGa4 || {}; if (isSearchResult) { var searchQuery = window.GOVUK.analyticsGa4.core.trackFunctions.standardiseSearchTerm(element.getAttribute('data-ga4-search-query')) + + // Limit tracked search term to 500 characters + if (searchQuery) { + searchQuery = searchQuery.substring(0, 500) + } var variant = element.getAttribute('data-ga4-ecommerce-variant') DEFAULT_LIST_TITLE = 'Site search 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 c89c40ec04..db31a98c5f 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 @@ -644,6 +644,25 @@ describe('GA4 core', function () { expect(builtEcommerceObject).toEqual(expectedEcommerceObject) }) + it('limits ecommerce search term tracking to 500 characters', function () { + var veryLongString = '' + + for (var i = 0; i < 600; i++) { + veryLongString = veryLongString + 'a' + } + + resultsParentEl.setAttribute('data-ga4-search-query', veryLongString) + + expectedEcommerceObject.search_results.term = veryLongString.substring(0, 500) + + 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')