Skip to content

Commit

Permalink
Fix "sticky" GA4 search tracker tool_name
Browse files Browse the repository at this point in the history
The GA4 search tracker was only setting the `tool_name` on its `search`
event in case the user had accepted an autocomplete suggestion. Now that
we persist the `tool_name` in the page's analytics state, this meant
that if the user had accepted a suggestion on a previous search, the
`tool_name` would continue to be set for subsequent searches regardless
of whether they've actually _used_ autocomplete.
  • Loading branch information
csutter committed Nov 20, 2024
1 parent 5e50b30 commit 59e9a82
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 2 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

* Fix "sticky" GA4 search tracker `tool_name` ([PR #4422](https://github.com/alphagov/govuk_publishing_components/pull/4422))

## 45.6.0

* Fix yellow focus colour overspill ([PR #4418](https://github.com/alphagov/govuk_publishing_components/pull/4418))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,11 @@ window.GOVUK.Modules = window.GOVUK.Modules || {};
// part of the `page_view` event on the subsequent page load
window.sessionStorage &&
window.sessionStorage.setItem('searchAutocompleteAccepted', 'true')
} else {
// Explicitly set `tool_name` to `null` if the user has not accepted a suggestion, as
// `undefined` will not overwrite the current value in the analytics state (meaning a
// previously set value would be used if there was one)
data.tool_name = null
}

data.length = Number(this.$searchInput.dataset.autocompleteSuggestionsCount)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -145,15 +145,15 @@ describe('Google Analytics search tracking', () => {
expect(setItemSpy).toHaveBeenCalledWith('searchAutocompleteAccepted', 'true')
})

it('does not set tool_name if the user has not accepted a suggestion', () => {
it('sets tool_name to null if the user has not accepted a suggestion', () => {
input.dataset.autocompleteTriggerInput = 'i want to'
input.dataset.autocompleteSuggestions = 'i want to fish|i want to dance|i want to sleep'
input.dataset.autocompleteSuggestionsCount = '3'

input.value = 'i want to fish'
GOVUK.triggerEvent(form, 'submit')

expect(sendSpy.calls.mostRecent().args[0].tool_name).toBeUndefined()
expect(sendSpy.calls.mostRecent().args[0].tool_name).toBeNull()
})
})

Expand Down

0 comments on commit 59e9a82

Please sign in to comment.