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

GA4: Fix ecommerce tracking of searches without query #4317

Merged
merged 1 commit into from
Oct 18, 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

* 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))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 = []
Expand Down
Loading