Skip to content
This repository has been archived by the owner on Sep 27, 2024. It is now read-only.

Commit

Permalink
Merge pull request #73 from MozillaSocial/fix/analytics-capture-neste…
Browse files Browse the repository at this point in the history
…d-clicks

fix(analytics): capture engagement events on children of <a>'s
  • Loading branch information
jpezninjo authored Nov 6, 2023
2 parents db2fbf6 + d21f1ea commit 7500fc2
Showing 1 changed file with 23 additions and 6 deletions.
29 changes: 23 additions & 6 deletions modules/glean/runtime/glean-plugin.client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,24 +33,41 @@ export default defineNuxtPlugin((nuxtApp) => {
}
}

function recordEngagement(element: Element) {
if (!element)
return

if (!element.hasAttribute('data-glean'))
return

const data = element.getAttribute('data-glean') || ''
const value = element.getAttribute('data-glean-value') || ''
engagement.record({ ui_identifier: data, engagement_value: value, ...engagementDetails[data] })
}

function handleButtonClick(ev: MouseEvent) {
const eventTarget = ev?.target as Element
const closestButton = eventTarget.closest('button')

if (!closestButton)
return

if (closestButton?.hasAttribute('href'))
linkClick.record({ target_url: closestButton.getAttribute('href') || '' })

const data = eventTarget?.getAttribute('data-glean') || ''
const value = eventTarget?.getAttribute('data-glean-value') || ''
if (eventTarget.hasAttribute('data-glean'))
engagement.record({ ui_identifier: data, engagement_value: value, ...engagementDetails[data] })
recordEngagement(eventTarget)
}

function handleLinkClick(ev: MouseEvent) {
const eventTarget = ev?.target as Element
const closestLink = eventTarget.closest('a')
if (closestLink)
linkClick.record({ target_url: closestLink.getAttribute('href') || '' })

if (!closestLink)
return

linkClick.record({ target_url: closestLink.getAttribute('href') || '' })

recordEngagement(closestLink)
}

window.addEventListener('click', eventListener)
Expand Down

0 comments on commit 7500fc2

Please sign in to comment.