({ type, version = '1.0.0', ...props }: SendEventProps)
| 98 | } |
| 99 | |
| 100 | export function sendEvent({ type, version = '1.0.0', ...props }: SendEventProps) { |
| 101 | const body = { |
| 102 | type, |
| 103 | |
| 104 | context: { |
| 105 | // Primitives |
| 106 | event_id: uuidv4(), |
| 107 | user: getUserEventsId(), |
| 108 | version, |
| 109 | created: new Date().toISOString(), |
| 110 | page_event_id: pageEventId, |
| 111 | |
| 112 | // Content information |
| 113 | path: location.pathname, |
| 114 | hostname: location.hostname, |
| 115 | referrer: document.referrer, |
| 116 | search: location.search, |
| 117 | href: location.href, |
| 118 | path_language: getMetaContent('path-language'), |
| 119 | path_version: getMetaContent('path-version'), |
| 120 | path_product: getMetaContent('path-product'), |
| 121 | path_article: getMetaContent('path-article'), |
| 122 | page_document_type: getMetaContent('page-document-type'), |
| 123 | page_type: getMetaContent('page-type'), |
| 124 | status: Number(getMetaContent('status') || 0), |
| 125 | |
| 126 | // Device information |
| 127 | // os, os_version, browser, browser_version: |
| 128 | ...parseUserAgent(), |
| 129 | viewport_width: document.documentElement.clientWidth, |
| 130 | viewport_height: document.documentElement.clientHeight, |
| 131 | |
| 132 | // Location information |
| 133 | timezone: new Date().getTimezoneOffset() / -60, |
| 134 | user_language: navigator.language, |
| 135 | |
| 136 | // Preference information |
| 137 | application_preference: Cookies.get('toolPreferred'), |
| 138 | color_mode_preference: getColorModePreference(), |
| 139 | os_preference: Cookies.get('osPreferred'), |
| 140 | }, |
| 141 | |
| 142 | ...props, |
| 143 | } |
| 144 | |
| 145 | const blob = new Blob([JSON.stringify(body)], { type: 'application/json' }) |
| 146 | const endpoint = '/api/events' |
| 147 | try { |
| 148 | // Only send the beacon if the feature is not disabled in the user's browser |
| 149 | // Even if the function exists, it can still throw an error from the call being blocked |
| 150 | navigator?.sendBeacon(endpoint, blob) |
| 151 | } catch { |
| 152 | console.warn(`sendBeacon to '${endpoint}' failed.`) |
| 153 | } |
| 154 | |
| 155 | return body |
| 156 | } |
| 157 |
no test coverage detected