Skip to content

Instantly share code, notes, and snippets.

@simonexmachina
Last active May 13, 2020 12:57
Show Gist options
  • Save simonexmachina/c534e193dc5824b5965e365efc9e8df2 to your computer and use it in GitHub Desktop.
Save simonexmachina/c534e193dc5824b5965e365efc9e8df2 to your computer and use it in GitHub Desktop.
Improving Elements Analytics - Proof of Concept
// Run addDataLayerTracking for each form to be tracked
// When the user enters invalid information call trackValidationError
export function addFormTracking(form) {
form.addEventListener('change', ev => {
let option = null
if (input.type == 'checkbox') {
option = input.checked ? 'Checked' : null
}
else if (input.type == 'radio') {
option = input.dataset.trackingLabel
}
// we might to set and unset in the calls to dataLayer.push
dataLayer.push({
event: 'formChange',
formId: form.dataset.trackingId,
inputId: input.dataset.trackingId,
option,
})
})
form.addEventListener('submit', ev => {
dataLayer.push({
event: 'formSubmit',
formId: form.dataset.trackingId,
})
})
}
export function trackValidationError(form, input, message) {
dataLayer.push({
event: 'formInvalid',
formId: form.dataset.trackingId,
inputId: input.dataset.trackingId,
message,
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment