Skip to content

Commit

Permalink
Fix failing tests
Browse files Browse the repository at this point in the history
- rewrites the ga4-finder-tracker slightly to use the schema code and specifically use the mergeProperties function of the schema to ensure that all undefined attributes from the schema are included in the data sent to the dataLayer
  • Loading branch information
andysellick committed Jul 25, 2023
1 parent dfd1465 commit d9c53d9
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 35 deletions.
47 changes: 24 additions & 23 deletions app/assets/javascripts/analytics-ga4/ga4-finder-tracker.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,17 +37,18 @@
return
}

var schema = new GOVUK.analyticsGa4.Schemas().eventSchema()
schema.event = 'event_data'
schema.event_data.type = 'finder'
schema.event_data.event_name = 'select_content'
var data = {}
data.type = 'finder'
data.event_name = 'select_content'

var elementInfo = this.getElementInfo(eventTarget, elementType, section)
var elementValue = elementInfo.elementValue
schema.event_data.text = elementValue

data.text = elementValue
var wasFilterRemoved = elementInfo.wasFilterRemoved
schema = this.setSchemaBasedOnChangeType(schema, elementValue, elementType, wasFilterRemoved, changeType, section)
data = this.setSchemaBasedOnChangeType(data, elementValue, elementType, wasFilterRemoved, changeType, section)

var schemas = new window.GOVUK.analyticsGa4.Schemas()
var schema = schemas.mergeProperties(data, 'event_data')

window.GOVUK.analyticsGa4.core.sendData(schema)
},
Expand Down Expand Up @@ -112,36 +113,36 @@
switch (changeType) {
case 'update-filter':
if (section) {
schema.event_data.section = section.getAttribute('data-ga4-section')
schema.section = section.getAttribute('data-ga4-section')
}

if (wasFilterRemoved) {
schema.event_data.action = 'remove'
schema.event_data.text = elementType === 'text' ? undefined : elementValue
schema.action = 'remove'
schema.text = elementType === 'text' ? undefined : elementValue
} else {
schema.event_data.action = elementType === 'text' ? 'search' : 'select'
schema.event_data.index = this.getSectionIndex(section)
schema.action = elementType === 'text' ? 'search' : 'select'
schema.index = this.getSectionIndex(section)
}
break

case 'update-keyword':
schema.event_data.event_name = 'search'
schema.event_data.url = window.location.pathname
schema.event_data.section = 'Search'
schema.event_data.action = 'search'
schema.event_data.text = PIIRemover.stripPIIWithOverride(schema.event_data.text, true, true)
schema.event_data.text = GOVUK.analyticsGa4.core.trackFunctions.removeLinesAndExtraSpaces(schema.event_data.text)
schema.event_data.text = schema.event_data.text.toLowerCase()
schema.event_name = 'search'
schema.url = window.location.pathname
schema.section = 'Search'
schema.action = 'search'
schema.text = PIIRemover.stripPIIWithOverride(schema.text, true, true)
schema.text = GOVUK.analyticsGa4.core.trackFunctions.removeLinesAndExtraSpaces(schema.text)
schema.text = schema.text.toLowerCase()
break

case 'clear-all-filters':
schema.event_data.action = 'remove'
schema.event_data.text = 'Clear all filters'
schema.action = 'remove'
schema.text = 'Clear all filters'
break

case 'update-sort':
schema.event_data.action = 'sort'
schema.event_data.section = 'Sort by'
schema.action = 'sort'
schema.section = 'Sort by'
break
}
return schema
Expand Down
24 changes: 12 additions & 12 deletions spec/javascripts/analytics-ga4/ga4-finder-tracker.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ describe('GA4 finder change tracker', function () {
inputParent = document.createElement('div')
inputParent.setAttribute('data-ga4-change-category', 'update-filter checkbox')
inputParent.setAttribute('data-ga4-section', 'Your favourite chocolate')
var index = { index_section: 1, index_section_count: 1, index_link: undefined }
var index = { index_section: 1, index_section_count: 1 }
inputParent.setAttribute('data-ga4-index', JSON.stringify(index))

input = document.createElement('input')
Expand All @@ -119,15 +119,15 @@ describe('GA4 finder change tracker', function () {
expected.event_data.section = 'Your favourite chocolate'
expected.event_data.text = 'All types of chocolate'
expected.event_data.action = 'select'
expected.event_data.index = index
expected.event_data.index = { index_section: 1, index_section_count: 1, index_link: undefined }

expect(window.dataLayer[0]).toEqual(expected)

input.checked = false
window.GOVUK.triggerEvent(input, 'change')

expected.event_data.action = 'remove'
expected.event_data.index = undefined
expected.event_data.index = { index_section: undefined, index_section_count: undefined, index_link: undefined }

expect(window.dataLayer[1]).toEqual(expected)
})
Expand All @@ -136,7 +136,7 @@ describe('GA4 finder change tracker', function () {
inputParent = document.createElement('div')
inputParent.setAttribute('data-ga4-change-category', 'update-filter radio')
inputParent.setAttribute('data-ga4-section', 'Your favourite chocolate')
var index = { index_section: 1, index_section_count: 1, index_link: undefined }
var index = { index_section: 1, index_section_count: 1 }
inputParent.setAttribute('data-ga4-index', JSON.stringify(index))

option1 = document.createElement('input')
Expand Down Expand Up @@ -170,7 +170,7 @@ describe('GA4 finder change tracker', function () {
expected.event_data.section = 'Your favourite chocolate'
expected.event_data.text = 'Belgian chocolate'
expected.event_data.action = 'select'
expected.event_data.index = index
expected.event_data.index = { index_section: 1, index_section_count: 1, index_link: undefined }

expect(window.dataLayer[0]).toEqual(expected)

Expand All @@ -179,7 +179,7 @@ describe('GA4 finder change tracker', function () {
window.GOVUK.triggerEvent(option1, 'change')

expected.event_data.action = 'remove'
expected.event_data.index = undefined
expected.event_data.index = { index_section: undefined, index_section_count: undefined, index_link: undefined }
expected.event_data.text = 'All types of chocolate (default)'

expect(window.dataLayer[1]).toEqual(expected)
Expand All @@ -189,7 +189,7 @@ describe('GA4 finder change tracker', function () {
inputParent = document.createElement('div')
inputParent.setAttribute('data-ga4-change-category', 'update-filter select')
inputParent.setAttribute('data-ga4-section', 'Your favourite chocolate')
var index = { index_section: 5, index_section_count: 15, index_link: undefined }
var index = { index_section: 5, index_section_count: 15 }
inputParent.setAttribute('data-ga4-index', JSON.stringify(index))

input = document.createElement('select')
Expand Down Expand Up @@ -218,15 +218,15 @@ describe('GA4 finder change tracker', function () {
expected.event_data.section = 'Your favourite chocolate'
expected.event_data.text = 'Belgian chocolate'
expected.event_data.action = 'select'
expected.event_data.index = index
expected.event_data.index = { index_section: 5, index_section_count: 15, index_link: undefined }

expect(window.dataLayer[0]).toEqual(expected)

input.value = 'all-types'
window.GOVUK.triggerEvent(input, 'change')

expected.event_data.action = 'remove'
expected.event_data.index = undefined
expected.event_data.index = { index_link: undefined, index_section: undefined, index_section_count: undefined }
expected.event_data.text = 'All types of chocolate (default)'

expect(window.dataLayer[1]).toEqual(expected)
Expand All @@ -236,7 +236,7 @@ describe('GA4 finder change tracker', function () {
inputParent = document.createElement('div')
inputParent.setAttribute('data-ga4-change-category', 'update-filter text')
inputParent.setAttribute('data-ga4-section', 'Your favourite chocolate')
var index = { index_section: 2, index_section_count: 2, index_link: undefined }
var index = { index_section: 2, index_section_count: 2 }
inputParent.setAttribute('data-ga4-index', JSON.stringify(index))

input = document.createElement('input')
Expand All @@ -252,15 +252,15 @@ describe('GA4 finder change tracker', function () {
expected.event_data.section = 'Your favourite chocolate'
expected.event_data.text = 'Here is an email that should not get redacted [email protected]'
expected.event_data.action = 'search'
expected.event_data.index = index
expected.event_data.index = { index_section: 2, index_section_count: 2, index_link: undefined }

expect(window.dataLayer[0]).toEqual(expected)

input.value = ''
window.GOVUK.triggerEvent(input, 'change')

expected.event_data.action = 'remove'
expected.event_data.index = undefined
expected.event_data.index = { index_section: undefined, index_section_count: undefined, index_link: undefined }
expected.event_data.text = undefined

expect(window.dataLayer[1]).toEqual(expected)
Expand Down

0 comments on commit d9c53d9

Please sign in to comment.