@dotcms/analytics
Version:
Official JavaScript library for Content Analytics with DotCMS.
35 lines (34 loc) • 1.05 kB
JavaScript
import { ANALYTICS_ENDPOINT as f } from "../constants/dot-analytics.constants.js";
import { createPluginLogger as T } from "../utils/dot-analytics.utils.js";
const h = async (n, a, i = !1) => {
const r = T("HTTP", a), c = `${a.server}${f}`, g = JSON.stringify(n);
r.info(`Sending ${n.events.length} event(s)${i ? " (keepalive)" : ""}`, {
payload: n
});
try {
const e = {
method: "POST",
headers: { "Content-Type": "application/json" },
body: g
};
if (i) {
e.keepalive = !0, e.credentials = "omit", fetch(c, e);
return;
}
const t = await fetch(c, e);
if (!t.ok) {
const p = t.statusText || "Unknown Error", o = `HTTP ${t.status}: ${p}`;
try {
const s = await t.json();
s.message ? r.warn(`${s.message} (${o})`) : r.warn(`${o} - No error message in response`);
} catch (s) {
r.warn(`${o} - Failed to parse error response:`, s);
}
}
} catch (e) {
r.error("Error sending event:", e);
}
};
export {
h as sendAnalyticsEvent
};