|
| 1 | +/* eslint-disable camelcase */ |
| 2 | +import { v4 as uuidv4 } from 'uuid' |
| 3 | +import Cookies from 'js-cookie' |
| 4 | +import getCsrf from './get-csrf' |
| 5 | + |
| 6 | +const COOKIE_NAME = '_docs-events' |
| 7 | + |
| 8 | +let cookieValue |
| 9 | + |
| 10 | +export function getUserEventsId () { |
| 11 | + if (cookieValue) return cookieValue |
| 12 | + cookieValue = Cookies.get(COOKIE_NAME) |
| 13 | + if (cookieValue) return cookieValue |
| 14 | + cookieValue = uuidv4() |
| 15 | + Cookies.set(COOKIE_NAME, cookieValue, { |
| 16 | + secure: true, |
| 17 | + sameSite: 'strict', |
| 18 | + expires: 365 |
| 19 | + }) |
| 20 | + return cookieValue |
| 21 | +} |
| 22 | + |
| 23 | +export async function sendEvent ({ |
| 24 | + type, |
| 25 | + version = '1.0.0', |
| 26 | + page_render_duration, |
| 27 | + exit_page_id, |
| 28 | + exit_first_paint, |
| 29 | + exit_dom_interactive, |
| 30 | + exit_dom_complete, |
| 31 | + exit_visit_duration, |
| 32 | + exit_scroll_length, |
| 33 | + link_url, |
| 34 | + search_query, |
| 35 | + search_context, |
| 36 | + navigate_label, |
| 37 | + survey_vote, |
| 38 | + survey_comment, |
| 39 | + survey_email, |
| 40 | + experiment_name, |
| 41 | + experiment_variation, |
| 42 | + experiment_success |
| 43 | +}) { |
| 44 | + const response = await fetch('/events', { |
| 45 | + method: 'POST', |
| 46 | + headers: { |
| 47 | + 'Content-Type': 'application/json', |
| 48 | + 'CSRF-Token': getCsrf() |
| 49 | + }, |
| 50 | + body: JSON.stringify({ |
| 51 | + type, // One of page, exit, link, search, navigate, survey, experiment |
| 52 | + |
| 53 | + context: { |
| 54 | + // Primitives |
| 55 | + event_id: uuidv4(), |
| 56 | + user: getUserEventsId(), |
| 57 | + version, |
| 58 | + created: new Date().toISOString(), |
| 59 | + |
| 60 | + // Content information |
| 61 | + path: location.pathname, |
| 62 | + referrer: document.referrer, |
| 63 | + search: location.search, |
| 64 | + href: location.href, |
| 65 | + site_language: location.pathname.split('/')[1], |
| 66 | + |
| 67 | + // Device information |
| 68 | + // os: |
| 69 | + // os_version: |
| 70 | + // browser: |
| 71 | + // browser_version: |
| 72 | + viewport_width: document.documentElement.clientWidth, |
| 73 | + viewport_height: document.documentElement.clientHeight, |
| 74 | + |
| 75 | + // Location information |
| 76 | + timezone: new Date().getTimezoneOffset() / -60, |
| 77 | + user_language: navigator.language |
| 78 | + }, |
| 79 | + |
| 80 | + // Page event |
| 81 | + page_render_duration, |
| 82 | + |
| 83 | + // Exit event |
| 84 | + exit_page_id, |
| 85 | + exit_first_paint, |
| 86 | + exit_dom_interactive, |
| 87 | + exit_dom_complete, |
| 88 | + exit_visit_duration, |
| 89 | + exit_scroll_length, |
| 90 | + |
| 91 | + // Link event |
| 92 | + link_url, |
| 93 | + |
| 94 | + // Search event |
| 95 | + search_query, |
| 96 | + search_context, |
| 97 | + |
| 98 | + // Navigate event |
| 99 | + navigate_label, |
| 100 | + |
| 101 | + // Survey event |
| 102 | + survey_vote, |
| 103 | + survey_comment, |
| 104 | + survey_email, |
| 105 | + |
| 106 | + // Experiment event |
| 107 | + experiment_name, |
| 108 | + experiment_variation, |
| 109 | + experiment_success |
| 110 | + }) |
| 111 | + }) |
| 112 | + const data = response.ok ? await response.json() : {} |
| 113 | + return data |
| 114 | +} |
| 115 | + |
| 116 | +export default async function initializeEvents () { |
| 117 | + await sendEvent({ type: 'page' }) |
| 118 | +} |
0 commit comments