Skip to content

Commit

Permalink
Merge pull request #4422 from alphagov/clear-tool-name
Browse files Browse the repository at this point in the history
Fix "sticky" GA4 search tracker `tool_name`
  • Loading branch information
csutter authored Nov 21, 2024
2 parents 5e50b30 + 59e9a82 commit 7d48fe7
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 7d48fe7

Please sign in to comment.