Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Limit GA4 search term tracking to 500 characters #4496

Merged
merged 1 commit into from
Dec 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading