Skip to content

Commit c3bff7f

Browse files
authored
Trigger event when user clicks external link (#16156)
* Trigger event when user clicks external link * Use event delegation for link events
1 parent c4cf788 commit c3bff7f

File tree

1 file changed

+14
-4
lines changed

1 file changed

+14
-4
lines changed

javascripts/events.js

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ export function getUserEventsId () {
2727
return cookieValue
2828
}
2929

30-
export async function sendEvent ({
30+
export function sendEvent ({
3131
type,
3232
version = '1.0.0',
3333
page_render_duration,
@@ -141,7 +141,7 @@ function trackScroll () {
141141
if (scrollPosition > maxScrollY) maxScrollY = scrollPosition
142142
}
143143

144-
async function sendExit () {
144+
function sendExit () {
145145
if (sentExit) return
146146
if (document.visibilityState !== 'hidden') return
147147
if (!pageEventId) return
@@ -162,14 +162,24 @@ async function sendExit () {
162162
})
163163
}
164164

165-
export default async function initializeEvents () {
165+
export default function initializeEvents () {
166166
// Page event
167167
const { render } = getPerformance()
168-
const pageEvent = await sendEvent({
168+
const pageEvent = sendEvent({
169169
type: 'page',
170170
page_render_duration: render
171171
})
172172

173+
// Link event
174+
document.documentElement.addEventListener('click', evt => {
175+
const link = evt.target.closest('a[href^="http"]')
176+
if (!link) return
177+
sendEvent({
178+
type: 'link',
179+
link_url: link.href
180+
})
181+
})
182+
173183
// Navigate event
174184
Array.from(
175185
document.querySelectorAll('.sidebar-products details')

0 commit comments

Comments
 (0)