Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

repo sync #729

Merged
merged 2 commits into from
Oct 22, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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