Skip to content

Commit

Permalink
Merge pull request #4496 from alphagov/ecommerce-500-limit-ga4
Browse files Browse the repository at this point in the history
Limit GA4 search term tracking to 500 characters
  • Loading branch information
AshGDS authored Dec 13, 2024
2 parents 171e814 + ec49362 commit 28fe968
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 0 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@
useful summary for people upgrading their application, not a replication
of the commit log.

## Unreleased

* Limit GA4 search term tracking to 500 characters ([PR #4496](https://github.com/alphagov/govuk_publishing_components/pull/4496))

## 46.3.1

* Add search-with-autocomplete to stylesheets served by static ([PR #4495](https://github.com/alphagov/govuk_publishing_components/pull/4495))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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'
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down

0 comments on commit 28fe968

Please sign in to comment.