Upload beautify
This commit is contained in:
24
resources/js/lib/uploadAnalytics.js
Normal file
24
resources/js/lib/uploadAnalytics.js
Normal file
@@ -0,0 +1,24 @@
|
||||
export function emitUploadEvent(eventName, payload = {}) {
|
||||
try {
|
||||
if (typeof window !== 'undefined') {
|
||||
window.dispatchEvent(
|
||||
new CustomEvent('skinbase:upload-analytics', {
|
||||
detail: {
|
||||
event: eventName,
|
||||
payload,
|
||||
timestamp: Date.now(),
|
||||
},
|
||||
})
|
||||
)
|
||||
}
|
||||
|
||||
const endpoint = typeof window !== 'undefined' ? window?.SKINBASE_UPLOAD_ANALYTICS_URL : null
|
||||
if (endpoint && typeof navigator !== 'undefined' && typeof navigator.sendBeacon === 'function') {
|
||||
const body = JSON.stringify({ event: eventName, payload, ts: Date.now() })
|
||||
const blob = new Blob([body], { type: 'application/json' })
|
||||
navigator.sendBeacon(endpoint, blob)
|
||||
}
|
||||
} catch {
|
||||
// analytics must remain non-blocking
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user