Skip to content

Commit

Permalink
Merge pull request #3545 from alphagov/new-search-event
Browse files Browse the repository at this point in the history
Use new Publishing Components GA4 search tracker
  • Loading branch information
csutter authored Nov 1, 2024
2 parents 8d7d842 + 7e5491a commit 1a8ac7e
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 84 deletions.
2 changes: 1 addition & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ GEM
govuk_personalisation (1.0.0)
plek (>= 1.9.0)
rails (>= 6, < 8)
govuk_publishing_components (44.9.1)
govuk_publishing_components (44.10.0)
chartkick
govuk_app_config
govuk_personalisation (>= 0.7.0)
Expand Down
27 changes: 0 additions & 27 deletions app/assets/javascripts/modules/all-content-finder.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,36 +57,9 @@ window.GOVUK.Modules = window.GOVUK.Modules || {};
})
}

// Sends the canonical GOV.UK `search` analytics event (the legacy UI does this on change using
// the regular GA4 finder tracker, but we want this to only happen on submission if the keywords
// have actually changed since the least search (i.e. page load/module initialisation), so
// cannot leverage that)
setupFormSubmissionAnalyticsEvent () {
this.$form.addEventListener('submit', () => {
if (this.$keywordInput.value === this.initialKeywords) return

const schemas = new window.GOVUK.analyticsGa4.Schemas()
const data = schemas.mergeProperties({
type: 'finder',
event_name: 'search',
section: 'Search',
action: 'search',
// standardiseSearchTerm returns undefined for empty strings, which we do _not_ want in
// this scenario as it would lead to the analytics tracking not picking up on the change
text: GOVUK.analyticsGa4.core.trackFunctions.standardiseSearchTerm(
this.$keywordInput.value
) || '',
url: window.location.pathname
}, 'event_data')

GOVUK.analyticsGa4.core.sendData(data)
})
}

setupAnalyticsTracking () {
GOVUK.analyticsGa4.Ga4EcommerceTracker.init()

this.setupFormSubmissionAnalyticsEvent()
this.$form.addEventListener('change', (event) => {
const $closestCategoryWrapper = event.target.closest('[data-ga4-change-category]')

Expand Down
6 changes: 6 additions & 0 deletions app/views/finders/show_all_content_finder.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,12 @@
action: content_item.base_path,
id: "all-content-finder-form",
class: "js-all-content-finder-form",
data: {
module: "ga4-search-tracker",
ga4_search_type: "finder",
ga4_search_url: "/search/all",
ga4_search_section: "Search",
},
) do %>
<%= hidden_field_tag :parent, @parent if @parent.present? %>
<%= hidden_field_tag :enable_new_all_content_finder_ui, params[:enable_new_all_content_finder_ui] if params[:enable_new_all_content_finder_ui].present? %>
Expand Down
56 changes: 0 additions & 56 deletions spec/javascripts/modules/all-content-finder.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -179,21 +179,6 @@ describe('AllContentFinder module', () => {
expect(GOVUK.analyticsGa4.Ga4FinderTracker.trackChangeEvent).not.toHaveBeenCalled()
})

it('does not fire a `search` event on form submit if the keyword has not changed', () => {
const form = fixture.querySelector('.js-all-content-finder-form')
form.dispatchEvent(new Event('submit', { bubbles: true }))

expect(GOVUK.analyticsGa4.core.sendData).not.toHaveBeenCalled()
})

it('does not fire a `search` event on form submit even if the keyword has changed', () => {
const form = fixture.querySelector('.js-all-content-finder-form')
form.querySelector('input[type="search"]').value = 'new keyword'
form.dispatchEvent(new Event('submit', { bubbles: true }))

expect(GOVUK.analyticsGa4.core.sendData).not.toHaveBeenCalled()
})

it('does not set up ecommerce tracking', () => {
expect(GOVUK.analyticsGa4.Ga4EcommerceTracker.init).not.toHaveBeenCalled()
})
Expand All @@ -213,47 +198,6 @@ describe('AllContentFinder module', () => {
expect(GOVUK.analyticsGa4.Ga4FinderTracker.trackChangeEvent).toHaveBeenCalledWith(input, 'FooCategory')
})

it('does not fire a `search` event on form submit if the keyword has not changed', () => {
const form = fixture.querySelector('.js-all-content-finder-form')
form.dispatchEvent(new Event('submit', { bubbles: true }))

expect(GOVUK.analyticsGa4.core.sendData).not.toHaveBeenCalled()
})

it('fires a `search` event on form submit if the keyword has changed', () => {
const form = fixture.querySelector('.js-all-content-finder-form')
form.querySelector('input[type="search"]').value = 'new keyword'
form.dispatchEvent(new Event('submit', { bubbles: true }))

expect(GOVUK.analyticsGa4.core.sendData).toHaveBeenCalledWith(jasmine.objectContaining({
event: 'event_data',
event_data: jasmine.objectContaining({
event_name: 'search',
type: 'finder',
text: 'new keyword',
action: 'search',
section: 'Search'
})
}))
})

it('fires a `search` event with the right text if the keyword has been removed', () => {
const form = fixture.querySelector('.js-all-content-finder-form')
form.querySelector('input[type="search"]').value = ''
form.dispatchEvent(new Event('submit', { bubbles: true }))

expect(GOVUK.analyticsGa4.core.sendData).toHaveBeenCalledWith(jasmine.objectContaining({
event: 'event_data',
event_data: jasmine.objectContaining({
event_name: 'search',
type: 'finder',
text: '',
action: 'search',
section: 'Search'
})
}))
})

it('sets up ecommerce tracking', () => {
expect(GOVUK.analyticsGa4.Ga4EcommerceTracker.init).toHaveBeenCalled()
})
Expand Down

0 comments on commit 1a8ac7e

Please sign in to comment.