@@ -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,44 @@ 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+
183+ // Navigate event
184+ Array . from (
185+ document . querySelectorAll ( '.sidebar-products details' )
186+ ) . forEach ( details => details . addEventListener (
187+ 'toggle' ,
188+ evt => sendEvent ( {
189+ type : 'navigate' ,
190+ navigate_label : `details ${ evt . target . open ? 'open' : 'close' } : ${ evt . target . querySelector ( 'summary' ) . innerText } `
191+ } )
192+ ) )
193+
194+ document . querySelector ( '.sidebar-products' ) . addEventListener ( 'click' , evt => {
195+ const link = evt . target . closest ( 'a' )
196+ if ( ! link ) return
197+ sendEvent ( {
198+ type : 'navigate' ,
199+ navigate_label : `link: ${ link . href } `
200+ } )
201+ } )
202+
173203 // Exit event
174204 pageEventId = pageEvent ?. context ?. event_id
175205 window . addEventListener ( 'scroll' , trackScroll )
0 commit comments