@dotcms/analytics 1.2.0-next.2 → 1.2.0-next.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,17 +1,9 @@
1
1
  import { DotCMSAnalyticsConfig, DotCMSEvent, DotCMSRequestBody } from './models';
2
2
  /**
3
- * Available transport methods for sending analytics events
4
- */
5
- export declare const TRANSPORT_TYPES: {
6
- readonly FETCH: "fetch";
7
- readonly BEACON: "beacon";
8
- };
9
- export type TransportType = (typeof TRANSPORT_TYPES)[keyof typeof TRANSPORT_TYPES];
10
- /**
11
- * Send analytics events to the server
3
+ * Send analytics events to the server using fetch API
12
4
  * @param payload - The event payload data
13
5
  * @param config - The analytics configuration
14
- * @param transportType - Transport method: 'fetch' (default) or 'beacon' (for page unload)
15
- * @returns A promise that resolves when the request is complete (fetch only)
6
+ * @param keepalive - Use keepalive mode for page unload scenarios (default: false)
7
+ * @returns A promise that resolves when the request is complete
16
8
  */
17
- export declare const sendAnalyticsEvent: (payload: DotCMSRequestBody<DotCMSEvent>, config: DotCMSAnalyticsConfig, transportType?: TransportType) => Promise<void>;
9
+ export declare const sendAnalyticsEvent: (payload: DotCMSRequestBody<DotCMSEvent>, config: DotCMSAnalyticsConfig, keepalive?: boolean) => Promise<void>;
@@ -1,33 +1,28 @@
1
- import { ANALYTICS_ENDPOINT as l } from "./constants/dot-content-analytics.constants.js";
2
- const d = async (s, n, o = "fetch") => {
3
- const a = `${n.server}${l}`, c = JSON.stringify(s);
4
- if (n.debug && console.warn(
5
- `DotCMS Analytics: Sending ${s.events.length} event(s) via ${o}`,
6
- o === "fetch" ? { payload: s } : void 0
7
- ), o === "beacon") {
8
- if (navigator.sendBeacon) {
9
- const e = new Blob([c], { type: "application/json" });
10
- !navigator.sendBeacon(a, e) && n.debug && console.warn("DotCMS Analytics: sendBeacon failed (queue might be full)");
11
- } else
12
- return n.debug && console.warn("DotCMS Analytics: sendBeacon not available, using fetch fallback"), d(s, n, "fetch");
13
- return;
14
- }
1
+ import { ANALYTICS_ENDPOINT as p } from "./constants/dot-content-analytics.constants.js";
2
+ const $ = async (n, r, a = !1) => {
3
+ const c = `${r.server}${p}`, i = JSON.stringify(n);
4
+ r.debug && console.warn(
5
+ `DotCMS Analytics: Sending ${n.events.length} event(s)${a ? " (keepalive)" : ""}`,
6
+ { payload: n }
7
+ );
15
8
  try {
16
- const e = await fetch(a, {
9
+ const e = {
17
10
  method: "POST",
18
11
  headers: { "Content-Type": "application/json" },
19
- body: c
20
- });
21
- if (!e.ok) {
22
- const i = e.statusText || "Unknown Error", r = `HTTP ${e.status}: ${i}`;
12
+ body: i
13
+ };
14
+ a && (e.keepalive = !0, e.credentials = "omit");
15
+ const s = await fetch(c, e);
16
+ if (!s.ok) {
17
+ const l = s.statusText || "Unknown Error", o = `HTTP ${s.status}: ${l}`;
23
18
  try {
24
- const t = await e.json();
25
- t.message ? console.warn(`DotCMS Analytics: ${t.message} (${r})`) : console.warn(
26
- `DotCMS Analytics: ${r} - No error message in response`
19
+ const t = await s.json();
20
+ t.message ? console.warn(`DotCMS Analytics: ${t.message} (${o})`) : console.warn(
21
+ `DotCMS Analytics: ${o} - No error message in response`
27
22
  );
28
23
  } catch (t) {
29
24
  console.warn(
30
- `DotCMS Analytics: ${r} - Failed to parse error response:`,
25
+ `DotCMS Analytics: ${o} - Failed to parse error response:`,
31
26
  t
32
27
  );
33
28
  }
@@ -37,5 +32,5 @@ const d = async (s, n, o = "fetch") => {
37
32
  }
38
33
  };
39
34
  export {
40
- d as sendAnalyticsEvent
35
+ $ as sendAnalyticsEvent
41
36
  };
@@ -1,21 +1,21 @@
1
1
  import p from "@analytics/queue-utils";
2
- import { DEFAULT_QUEUE_CONFIG as c } from "../constants/dot-content-analytics.constants.js";
2
+ import { DEFAULT_QUEUE_CONFIG as h } from "../constants/dot-content-analytics.constants.js";
3
3
  import { sendAnalyticsEvent as y } from "../dot-content-analytics.http.js";
4
4
  const z = (n) => {
5
- let e = null, i = null, o = "fetch";
6
- const l = {
7
- ...c,
5
+ let e = null, i = null, l = !1;
6
+ const o = {
7
+ ...h,
8
8
  ...typeof n.queue == "object" ? n.queue : {}
9
- }, f = (t) => {
9
+ }, c = (t) => {
10
10
  if (!i) return;
11
11
  n.debug && console.log(`DotCMS Analytics Queue: Sending batch of ${t.length} event(s)`, {
12
12
  events: t,
13
- transport: o
14
- }), y({ context: i, events: t }, n, o);
13
+ keepalive: l
14
+ }), y({ context: i, events: t }, n, l);
15
15
  }, u = () => {
16
16
  !e || e.size() === 0 || !i || (n.debug && console.warn(
17
17
  `DotCMS Analytics: Flushing ${e.size()} events (page hidden/unload)`
18
- ), o = "beacon", e.flush(!0));
18
+ ), l = !0, e.flush(!0));
19
19
  }, d = () => {
20
20
  document.visibilityState === "hidden" && u();
21
21
  };
@@ -26,11 +26,11 @@ const z = (n) => {
26
26
  initialize: () => {
27
27
  e = p(
28
28
  (t) => {
29
- f(t);
29
+ c(t);
30
30
  },
31
31
  {
32
- max: l.eventBatchSize,
33
- interval: l.flushInterval,
32
+ max: o.eventBatchSize,
33
+ interval: o.flushInterval,
34
34
  throttle: !1
35
35
  // Always false - enables both batch size and interval triggers
36
36
  }
@@ -45,9 +45,9 @@ const z = (n) => {
45
45
  enqueue: (t, s) => {
46
46
  if (i = s, !!e) {
47
47
  if (n.debug) {
48
- const a = e.size() + 1, r = l.eventBatchSize ?? c.eventBatchSize, h = a >= r;
48
+ const a = e.size() + 1, r = o.eventBatchSize, f = a >= r;
49
49
  console.log(
50
- `DotCMS Analytics Queue: Event added. Queue size: ${a}/${r}${h ? " (full, sending...)" : ""}`,
50
+ `DotCMS Analytics Queue: Event added. Queue size: ${a}/${r}${f ? " (full, sending...)" : ""}`,
51
51
  { eventType: t.event_type, event: t }
52
52
  );
53
53
  }
@@ -64,7 +64,7 @@ const z = (n) => {
64
64
  * Flushes remaining events and cleans up listeners
65
65
  */
66
66
  cleanup: () => {
67
- u(), typeof window < "u" && typeof document < "u" && (document.removeEventListener("visibilitychange", d), window.removeEventListener("pagehide", u)), e = null, i = null, o = "fetch";
67
+ u(), typeof window < "u" && typeof document < "u" && (document.removeEventListener("visibilitychange", d), window.removeEventListener("pagehide", u)), e = null, i = null, l = !1;
68
68
  }
69
69
  };
70
70
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dotcms/analytics",
3
- "version": "1.2.0-next.2",
3
+ "version": "1.2.0-next.4",
4
4
  "description": "Official JavaScript library for Content Analytics with DotCMS.",
5
5
  "repository": {
6
6
  "type": "git",