Skip to content

Commit

Permalink
repo sync
Browse files Browse the repository at this point in the history
  • Loading branch information
Octomerger authored Oct 22, 2020
2 parents 9977f31 + c3bff7f commit cfb498e
Showing 1 changed file with 34 additions and 4 deletions.
38 changes: 34 additions & 4 deletions javascripts/events.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export function getUserEventsId () {
return cookieValue
}

export async function sendEvent ({
export function sendEvent ({
type,
version = '1.0.0',
page_render_duration,
Expand Down Expand Up @@ -141,7 +141,7 @@ function trackScroll () {
if (scrollPosition > maxScrollY) maxScrollY = scrollPosition
}

async function sendExit () {
function sendExit () {
if (sentExit) return
if (document.visibilityState !== 'hidden') return
if (!pageEventId) return
Expand All @@ -162,14 +162,44 @@ async function sendExit () {
})
}

export default async function initializeEvents () {
export default function initializeEvents () {
// Page event
const { render } = getPerformance()
const pageEvent = await sendEvent({
const pageEvent = sendEvent({
type: 'page',
page_render_duration: render
})

// Link event
document.documentElement.addEventListener('click', evt => {
const link = evt.target.closest('a[href^="http"]')
if (!link) return
sendEvent({
type: 'link',
link_url: link.href
})
})

// Navigate event
Array.from(
document.querySelectorAll('.sidebar-products details')
).forEach(details => details.addEventListener(
'toggle',
evt => sendEvent({
type: 'navigate',
navigate_label: `details ${evt.target.open ? 'open' : 'close'}: ${evt.target.querySelector('summary').innerText}`
})
))

document.querySelector('.sidebar-products').addEventListener('click', evt => {
const link = evt.target.closest('a')
if (!link) return
sendEvent({
type: 'navigate',
navigate_label: `link: ${link.href}`
})
})

// Exit event
pageEventId = pageEvent?.context?.event_id
window.addEventListener('scroll', trackScroll)
Expand Down

0 comments on commit cfb498e

Please sign in to comment.