From 7eb07b1dfb29554af604b423cead45debd16e800 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 --- CHANGELOG.md | 1 + .../analytics-ga4/ga4-core.js | 5 +++++ .../analytics-ga4/ga4-core.spec.js | 19 +++++++++++++++++++ 3 files changed, 25 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index a4722d9064..4dde252ebb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,7 @@ ## Unreleased * Add /media/ path to GA4 download link tracking ([PR #4491](https://github.com/alphagov/govuk_publishing_components/pull/4491/)) +* Limit GA4 search term tracking to 500 characters ([PR #4496](https://github.com/alphagov/govuk_publishing_components/pull/4496)) ## 46.3.0 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')