UNPKG

overcentric

Version:

A lightweight, privacy-focused toolkit for modern SaaS web applications

30 lines (29 loc) 1.04 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.sendEvent = sendEvent; function sendEvent(event, projectId, endpoint = 'http://localhost:4000/api/events') { return new Promise((resolve, reject) => { const xhr = new XMLHttpRequest(); xhr.open('POST', endpoint, true); xhr.setRequestHeader('Content-Type', 'application/json'); xhr.onload = () => { if (xhr.status >= 200 && xhr.status < 300) { resolve(); } else { reject(new Error(`HTTP Error: ${xhr.status} ${xhr.statusText}`)); } }; xhr.onerror = () => reject(new Error('Network Error')); const payload = { event: { name: event.eventName, properties: event.properties, project_id: projectId, url: window.location.href, referrer: document.referrer, } }; xhr.send(JSON.stringify(payload)); }); }