@dotcms/analytics 1.1.1 → 1.2.0

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.
Files changed (40) hide show
  1. package/README.md +284 -71
  2. package/lib/core/dot-content-analytics.d.ts +8 -1
  3. package/lib/core/dot-content-analytics.js +26 -23
  4. package/lib/core/plugin/dot-analytics.plugin.d.ts +8 -7
  5. package/lib/core/plugin/dot-analytics.plugin.js +25 -62
  6. package/lib/core/plugin/enricher/dot-analytics.enricher.plugin.d.ts +12 -28
  7. package/lib/core/plugin/enricher/dot-analytics.enricher.plugin.js +37 -15
  8. package/lib/core/plugin/identity/dot-analytics.identity.plugin.d.ts +11 -11
  9. package/lib/core/plugin/identity/dot-analytics.identity.plugin.js +13 -11
  10. package/lib/core/plugin/identity/dot-analytics.identity.utils.d.ts +7 -6
  11. package/lib/core/shared/constants/dot-content-analytics.constants.d.ts +70 -0
  12. package/lib/core/shared/constants/dot-content-analytics.constants.js +34 -0
  13. package/lib/core/shared/constants/index.d.ts +4 -0
  14. package/lib/core/shared/dot-content-analytics.activity-tracker.d.ts +9 -2
  15. package/lib/core/shared/dot-content-analytics.activity-tracker.js +11 -10
  16. package/lib/core/shared/dot-content-analytics.http.d.ts +13 -4
  17. package/lib/core/shared/dot-content-analytics.http.js +25 -13
  18. package/lib/core/shared/dot-content-analytics.utils.d.ts +105 -44
  19. package/lib/core/shared/dot-content-analytics.utils.js +92 -68
  20. package/lib/core/shared/models/data.model.d.ts +103 -0
  21. package/lib/core/shared/models/event.model.d.ts +64 -0
  22. package/lib/core/shared/models/index.d.ts +7 -0
  23. package/lib/core/shared/models/library.model.d.ts +191 -0
  24. package/lib/core/shared/models/request.model.d.ts +24 -0
  25. package/lib/core/shared/queue/dot-analytics.queue.utils.d.ts +28 -0
  26. package/lib/core/shared/queue/dot-analytics.queue.utils.js +73 -0
  27. package/lib/core/shared/queue/index.d.ts +1 -0
  28. package/lib/react/components/DotContentAnalytics.d.ts +1 -1
  29. package/lib/react/hook/useContentAnalytics.d.ts +43 -15
  30. package/lib/react/hook/useContentAnalytics.js +18 -21
  31. package/lib/react/hook/useRouterTracker.d.ts +1 -1
  32. package/lib/react/internal/utils.d.ts +1 -1
  33. package/lib/react/internal/utils.js +2 -2
  34. package/lib/react/public-api.d.ts +1 -1
  35. package/lib/standalone.d.ts +2 -2
  36. package/package.json +2 -1
  37. package/uve/src/internal/constants.js +8 -3
  38. package/lib/core/shared/dot-content-analytics.constants.d.ts +0 -37
  39. package/lib/core/shared/dot-content-analytics.constants.js +0 -14
  40. package/lib/core/shared/dot-content-analytics.model.d.ts +0 -351
@@ -1,79 +1,42 @@
1
- import { EVENT_TYPES as y } from "../shared/dot-content-analytics.constants.js";
2
- import { sendAnalyticsEventToServer as c } from "../shared/dot-content-analytics.http.js";
3
- const p = (v) => {
4
- let r = !1;
1
+ import { sendAnalyticsEvent as u } from "../shared/dot-content-analytics.http.js";
2
+ import { createAnalyticsQueue as d } from "../shared/queue/dot-analytics.queue.utils.js";
3
+ const v = (t) => {
4
+ let n = !1;
5
+ const i = t.queue !== !1;
6
+ let e = null;
7
+ const o = (r) => {
8
+ const { config: c, payload: l } = r;
9
+ if (!n)
10
+ throw new Error("DotCMS Analytics: Plugin not initialized");
11
+ const a = l.events[0], s = l.context;
12
+ i && e ? e.enqueue(a, s) : u({
13
+ context: s,
14
+ events: [a]
15
+ }, c);
16
+ };
5
17
  return {
6
18
  name: "dot-analytics",
7
- config: v,
19
+ config: t,
8
20
  /**
9
- * Initialize the plugin
21
+ * Initialize the plugin with optional queue management
10
22
  */
11
- initialize: () => (r = !0, Promise.resolve()),
23
+ initialize: () => (n = !0, i && (e = d(t), e.initialize()), Promise.resolve()),
12
24
  /**
13
25
  * Track a page view event
14
- * Takes enriched data from properties and creates final structured event
26
+ * The enricher plugin has already built the complete request body
15
27
  */
16
- page: (a) => {
17
- const { config: t, payload: e } = a, { context: n, page: o, device: i, utm: s, local_time: l } = e;
18
- if (!r)
19
- throw new Error("DotAnalytics: Plugin not initialized");
20
- if (!n || !o || !i || !l)
21
- throw new Error("DotAnalytics: Missing required payload data for pageview event");
22
- const d = {
23
- context: n,
24
- events: [
25
- {
26
- event_type: y.PAGEVIEW,
27
- local_time: l,
28
- data: {
29
- page: o,
30
- device: i,
31
- ...s && { utm: s }
32
- }
33
- }
34
- ]
35
- };
36
- return t.debug && console.warn("DotAnalytics: Pageview event to send:", d), c(d, t);
37
- },
38
- // TODO: Fix this when we haver the final design for the track event
28
+ page: o,
39
29
  /**
40
30
  * Track a custom event
41
- * Takes enriched data and sends it to the analytics server
31
+ * The enricher plugin has already built the complete request body
42
32
  */
43
- track: (a) => {
44
- const { config: t, payload: e } = a;
45
- if (!r)
46
- throw new Error("DotAnalytics: Plugin not initialized");
47
- if ("events" in e && Array.isArray(e.events)) {
48
- const o = e, i = {
49
- context: o.context,
50
- events: o.events
51
- };
52
- return t.debug && console.warn("DotAnalytics: Track event to send:", i), c(i, t);
53
- }
54
- if (!e.context || !e.local_time)
55
- throw new Error("DotAnalytics: Missing required payload data for track event");
56
- const n = {
57
- context: e.context,
58
- events: [
59
- {
60
- event_type: y.TRACK,
61
- local_time: e.local_time,
62
- data: {
63
- event: e.event,
64
- ...e.properties
65
- }
66
- }
67
- ]
68
- };
69
- return t.debug && console.warn("DotAnalytics: Track event to send (fallback):", n), c(n, t);
70
- },
33
+ track: o,
71
34
  /**
72
35
  * Check if the plugin is loaded
73
36
  */
74
- loaded: () => r
37
+ loaded: () => n
75
38
  };
76
39
  };
77
40
  export {
78
- p as dotAnalytics
41
+ v as dotAnalytics
79
42
  };
@@ -1,44 +1,28 @@
1
- import { DotCMSAnalyticsPayload } from '../../shared/dot-content-analytics.model';
1
+ import { AnalyticsBasePayloadWithContext, AnalyticsTrackPayloadWithContext, DotCMSAnalyticsRequestBody } from '../../shared/models';
2
2
  /**
3
3
  * Plugin that enriches the analytics payload data based on the event type.
4
4
  * Uses Analytics.js lifecycle events to inject context before processing.
5
- * The identity plugin runs FIRST to inject context: { session_id, site_key, user_id }
6
- * This enricher plugin runs SECOND to add page/device/utm data.
5
+ * The identity plugin runs FIRST to inject context: { session_id, site_auth, user_id, device }
6
+ * This enricher plugin runs SECOND to add page/utm/custom data.
7
7
  *
8
- * OPTIMIZED: Uses existing payload.properties data to avoid duplication
8
+ * Returns the final request body structure ready to send to the server.
9
9
  */
10
10
  export declare const dotAnalyticsEnricherPlugin: () => {
11
11
  name: string;
12
12
  /**
13
13
  * PAGE VIEW ENRICHMENT - Runs after identity context injection
14
- * Uses optimized enrichment that leverages analytics.js payload data
14
+ * Returns the complete request body for pageview events
15
+ * @returns {DotCMSAnalyticsRequestBody} Complete request body ready to send
15
16
  */
16
17
  'page:dot-analytics': ({ payload }: {
17
- payload: DotCMSAnalyticsPayload;
18
- }) => {
19
- local_time: string;
20
- utm?: import('../../shared/dot-content-analytics.model').DotCMSUtmData;
21
- page: import('../../shared/dot-content-analytics.model').DotCMSPageData;
22
- device: import('../../shared/dot-content-analytics.model').DotCMSDeviceData;
23
- event: string;
24
- properties: Record<string, unknown>;
25
- options: Record<string, unknown>;
26
- context?: import('../../shared/dot-content-analytics.model').DotCMSAnalyticsContext;
27
- };
18
+ payload: AnalyticsBasePayloadWithContext;
19
+ }) => DotCMSAnalyticsRequestBody;
28
20
  /**
29
21
  * TRACK EVENT ENRICHMENT - Runs after identity context injection
30
- * Creates structured track events with pre-injected context
22
+ * Returns the complete request body for custom events
23
+ * @returns {DotCMSAnalyticsRequestBody} Complete request body ready to send
31
24
  */
32
25
  'track:dot-analytics': ({ payload }: {
33
- payload: DotCMSAnalyticsPayload;
34
- }) => {
35
- events: {
36
- event_type: "track";
37
- local_time: string;
38
- data: {
39
- src: string;
40
- event: string;
41
- };
42
- }[];
43
- };
26
+ payload: AnalyticsTrackPayloadWithContext;
27
+ }) => DotCMSAnalyticsRequestBody;
44
28
  };
@@ -1,28 +1,50 @@
1
- import { ANALYTICS_SOURCE_TYPE as n, EVENT_TYPES as r } from "../../shared/dot-content-analytics.constants.js";
2
- import { getLocalTime as a, enrichPagePayloadOptimized as i } from "../../shared/dot-content-analytics.utils.js";
3
- const l = () => ({
1
+ import { DotCMSPredefinedEventType as i } from "../../shared/constants/dot-content-analytics.constants.js";
2
+ import { getLocalTime as c, enrichPagePayloadOptimized as s } from "../../shared/dot-content-analytics.utils.js";
3
+ const m = () => ({
4
4
  name: "enrich-dot-analytics",
5
5
  /**
6
6
  * PAGE VIEW ENRICHMENT - Runs after identity context injection
7
- * Uses optimized enrichment that leverages analytics.js payload data
7
+ * Returns the complete request body for pageview events
8
+ * @returns {DotCMSAnalyticsRequestBody} Complete request body ready to send
8
9
  */
9
- "page:dot-analytics": ({ payload: e }) => i(e),
10
- // TODO: Fix this when we haver the final design for the track event
10
+ "page:dot-analytics": ({
11
+ payload: o
12
+ }) => {
13
+ const { context: a, page: t, utm: e, custom: n, local_time: r } = s(o);
14
+ if (!t)
15
+ throw new Error("DotCMS Analytics: Missing required page data");
16
+ return {
17
+ context: a,
18
+ events: [
19
+ {
20
+ event_type: i.PAGEVIEW,
21
+ local_time: r,
22
+ data: {
23
+ page: t,
24
+ ...e && { utm: e },
25
+ ...n && { custom: n }
26
+ }
27
+ }
28
+ ]
29
+ };
30
+ },
11
31
  /**
12
32
  * TRACK EVENT ENRICHMENT - Runs after identity context injection
13
- * Creates structured track events with pre-injected context
33
+ * Returns the complete request body for custom events
34
+ * @returns {DotCMSAnalyticsRequestBody} Complete request body ready to send
14
35
  */
15
- "track:dot-analytics": ({ payload: e }) => {
16
- const t = a();
36
+ "track:dot-analytics": ({
37
+ payload: o
38
+ }) => {
39
+ const { event: a, properties: t, context: e } = o, n = c();
17
40
  return {
41
+ context: e,
18
42
  events: [
19
43
  {
20
- event_type: r.TRACK,
21
- local_time: t,
44
+ event_type: a,
45
+ local_time: n,
22
46
  data: {
23
- event: e.event,
24
- ...e.properties,
25
- src: n
47
+ custom: t
26
48
  }
27
49
  }
28
50
  ]
@@ -30,5 +52,5 @@ const l = () => ({
30
52
  }
31
53
  });
32
54
  export {
33
- l as dotAnalyticsEnricherPlugin
55
+ m as dotAnalyticsEnricherPlugin
34
56
  };
@@ -1,4 +1,4 @@
1
- import { DotCMSAnalyticsConfig, DotCMSAnalyticsHookParams } from '../../shared/dot-content-analytics.model';
1
+ import { AnalyticsBaseParams, DotCMSAnalyticsConfig } from '../../shared/models';
2
2
  /**
3
3
  * Identity Plugin for DotAnalytics
4
4
  * Handles user ID generation, session management, and activity tracking.
@@ -20,12 +20,12 @@ export declare const dotAnalyticsIdentityPlugin: (config: DotCMSAnalyticsConfig)
20
20
  */
21
21
  initialize: () => Promise<void>;
22
22
  /**
23
- * Inject identity context into page events
23
+ * Inject identity context into page events and updates session activity for session management
24
24
  * This runs BEFORE the enricher plugin
25
25
  */
26
- pageStart: ({ payload }: DotCMSAnalyticsHookParams) => {
27
- context: import('../../shared/dot-content-analytics.model').DotCMSAnalyticsContext;
28
- type: string;
26
+ pageStart: ({ payload }: AnalyticsBaseParams) => {
27
+ context: import('../../shared/models').DotCMSAnalyticsEventContext;
28
+ type: "page" | "track";
29
29
  properties: {
30
30
  title: string;
31
31
  url: string;
@@ -37,7 +37,7 @@ export declare const dotAnalyticsIdentityPlugin: (config: DotCMSAnalyticsConfig)
37
37
  referrer?: string;
38
38
  };
39
39
  options: Record<string, unknown>;
40
- userId: string | null;
40
+ userId: string;
41
41
  anonymousId: string;
42
42
  meta: {
43
43
  rid: string;
@@ -46,12 +46,12 @@ export declare const dotAnalyticsIdentityPlugin: (config: DotCMSAnalyticsConfig)
46
46
  };
47
47
  };
48
48
  /**
49
- * Inject identity context into track events
49
+ * Inject identity context into track events and updates session activity for session management
50
50
  * This runs BEFORE the enricher plugin
51
51
  */
52
- trackStart: ({ payload }: DotCMSAnalyticsHookParams) => {
53
- context: import('../../shared/dot-content-analytics.model').DotCMSAnalyticsContext;
54
- type: string;
52
+ trackStart: ({ payload }: AnalyticsBaseParams) => {
53
+ context: import('../../shared/models').DotCMSAnalyticsEventContext;
54
+ type: "page" | "track";
55
55
  properties: {
56
56
  title: string;
57
57
  url: string;
@@ -63,7 +63,7 @@ export declare const dotAnalyticsIdentityPlugin: (config: DotCMSAnalyticsConfig)
63
63
  referrer?: string;
64
64
  };
65
65
  options: Record<string, unknown>;
66
- userId: string | null;
66
+ userId: string;
67
67
  anonymousId: string;
68
68
  meta: {
69
69
  rid: string;
@@ -1,32 +1,34 @@
1
- import { getAnalyticsContext as i } from "../../shared/dot-content-analytics.utils.js";
2
- import { cleanupActivityTracking as r, initializeActivityTracking as o } from "../../shared/dot-content-analytics.activity-tracker.js";
3
- const c = (t) => ({
1
+ import { getAnalyticsContext as n } from "../../shared/dot-content-analytics.utils.js";
2
+ import { cleanupActivityTracking as r, updateSessionActivity as o, initializeActivityTracking as a } from "../../shared/dot-content-analytics.activity-tracker.js";
3
+ const s = (t) => ({
4
4
  name: "dot-analytics-identity",
5
5
  /**
6
6
  * Initialize the identity plugin
7
7
  * Sets up activity tracking for session management
8
8
  */
9
- initialize: () => (o(t), Promise.resolve()),
9
+ initialize: () => (a(t), Promise.resolve()),
10
10
  /**
11
- * Inject identity context into page events
11
+ * Inject identity context into page events and updates session activity for session management
12
12
  * This runs BEFORE the enricher plugin
13
13
  */
14
14
  pageStart: ({ payload: e }) => {
15
- const n = i(t);
15
+ o();
16
+ const i = n(t);
16
17
  return {
17
18
  ...e,
18
- context: n
19
+ context: i
19
20
  };
20
21
  },
21
22
  /**
22
- * Inject identity context into track events
23
+ * Inject identity context into track events and updates session activity for session management
23
24
  * This runs BEFORE the enricher plugin
24
25
  */
25
26
  trackStart: ({ payload: e }) => {
26
- const n = i(t);
27
+ o();
28
+ const i = n(t);
27
29
  return {
28
30
  ...e,
29
- context: n
31
+ context: i
30
32
  };
31
33
  },
32
34
  /**
@@ -36,5 +38,5 @@ const c = (t) => ({
36
38
  loaded: () => (typeof window < "u" && (window.addEventListener("beforeunload", r), window.addEventListener("pagehide", r)), !0)
37
39
  });
38
40
  export {
39
- c as dotAnalyticsIdentityPlugin
41
+ s as dotAnalyticsIdentityPlugin
40
42
  };
@@ -1,3 +1,4 @@
1
+ import { DotCMSEventUtmData } from '../../shared/models';
1
2
  /**
2
3
  * Updates activity timestamp
3
4
  */
@@ -15,10 +16,10 @@ export declare const hasPassedMidnight: (sessionStartTime: number) => boolean;
15
16
  */
16
17
  export declare const getLastActivityTime: () => number;
17
18
  /**
18
- * Extracts UTM parameters from current location
19
+ * Compares UTM parameters to detect campaign changes.
20
+ * Only checks significant parameters: source, medium, and campaign.
21
+ * @internal This function is for internal use only.
22
+ * @param currentUTM - Current UTM parameters in DotCMS format
23
+ * @returns True if UTM parameters have changed, false otherwise
19
24
  */
20
- export declare const extractUTMParameters: () => Record<string, string>;
21
- /**
22
- * Compares UTM parameters to detect campaign changes
23
- */
24
- export declare const hasUTMChanged: (currentUTM: Record<string, string>) => boolean;
25
+ export declare const hasUTMChanged: (currentUTM: DotCMSEventUtmData) => boolean;
@@ -0,0 +1,70 @@
1
+ export declare const ANALYTICS_WINDOWS_KEY = "dotAnalytics";
2
+ export declare const ANALYTICS_SOURCE_TYPE = "dotAnalytics";
3
+ export declare const ANALYTICS_ENDPOINT = "/api/v1/analytics/content/event";
4
+ /**
5
+ * Structured event types - events with predefined data shapes
6
+ * These events have specific data structures and validation
7
+ */
8
+ export declare const DotCMSPredefinedEventType: {
9
+ readonly PAGEVIEW: "pageview";
10
+ };
11
+ /**
12
+ * Type for structured events
13
+ */
14
+ export type DotCMSPredefinedEventType = (typeof DotCMSPredefinedEventType)[keyof typeof DotCMSPredefinedEventType];
15
+ /**
16
+ * Custom event type - any string except predefined event types
17
+ * These events have flexible data structures defined by the user
18
+ */
19
+ export type DotCMSCustomEventType = Exclude<string, DotCMSPredefinedEventType>;
20
+ /**
21
+ * Union type for all possible event types
22
+ */
23
+ export type DotCMSEventType = DotCMSPredefinedEventType | DotCMSCustomEventType;
24
+ /**
25
+ * Expected UTM parameter keys for campaign tracking
26
+ */
27
+ export declare const EXPECTED_UTM_KEYS: readonly ["utm_source", "utm_medium", "utm_campaign", "utm_term", "utm_content"];
28
+ /**
29
+ * Session configuration constants
30
+ */
31
+ export declare const DEFAULT_SESSION_TIMEOUT_MINUTES = 30;
32
+ /**
33
+ * Session storage key for session ID
34
+ */
35
+ export declare const SESSION_STORAGE_KEY = "dot_analytics_session_id";
36
+ /**
37
+ * Session storage key for session start time
38
+ */
39
+ export declare const SESSION_START_KEY = "dot_analytics_session_start";
40
+ /**
41
+ * Session storage key for session UTM data
42
+ */
43
+ export declare const SESSION_UTM_KEY = "dot_analytics_session_utm";
44
+ /**
45
+ * User ID configuration constants
46
+ */
47
+ export declare const USER_ID_KEY = "dot_analytics_user_id";
48
+ /**
49
+ * Activity tracking configuration
50
+ * Events used to detect user activity for session management
51
+ * - click: Detects real user interaction with minimal performance impact
52
+ * - visibilitychange: Handled separately to detect tab changes
53
+ */
54
+ export declare const ACTIVITY_EVENTS: readonly ["click"];
55
+ /**
56
+ * Default queue configuration for event batching
57
+ */
58
+ export declare const DEFAULT_QUEUE_CONFIG: {
59
+ readonly eventBatchSize: 15;
60
+ readonly flushInterval: 5000;
61
+ };
62
+ /**
63
+ * The name of the analytics minified script.
64
+ */
65
+ export declare const ANALYTICS_MINIFIED_SCRIPT_NAME = "ca.min.js";
66
+ /**
67
+ * Default properties that Analytics.js adds automatically
68
+ * These should be filtered out to only keep user-provided properties
69
+ */
70
+ export declare const ANALYTICS_JS_DEFAULT_PROPERTIES: readonly ["title", "url", "path", "hash", "search", "width", "height", "referrer"];
@@ -0,0 +1,34 @@
1
+ const E = "/api/v1/analytics/content/event", e = {
2
+ PAGEVIEW: "pageview"
3
+ }, n = [
4
+ "utm_source",
5
+ "utm_medium",
6
+ "utm_campaign",
7
+ "utm_term",
8
+ "utm_content"
9
+ ], c = 30, s = "dot_analytics_session_id", T = "dot_analytics_user_id", t = 15, _ = 5e3, o = ["click"], S = {
10
+ eventBatchSize: t,
11
+ // Max events per batch - auto-sends when reached
12
+ flushInterval: _
13
+ // Time between flushes - sends whatever is queued
14
+ }, I = [
15
+ "title",
16
+ "url",
17
+ "path",
18
+ "hash",
19
+ "search",
20
+ "width",
21
+ "height",
22
+ "referrer"
23
+ ];
24
+ export {
25
+ o as ACTIVITY_EVENTS,
26
+ E as ANALYTICS_ENDPOINT,
27
+ I as ANALYTICS_JS_DEFAULT_PROPERTIES,
28
+ S as DEFAULT_QUEUE_CONFIG,
29
+ c as DEFAULT_SESSION_TIMEOUT_MINUTES,
30
+ e as DotCMSPredefinedEventType,
31
+ n as EXPECTED_UTM_KEYS,
32
+ s as SESSION_STORAGE_KEY,
33
+ T as USER_ID_KEY
34
+ };
@@ -0,0 +1,4 @@
1
+ /**
2
+ * Central export point for all DotCMS Analytics constants
3
+ */
4
+ export * from './dot-content-analytics.constants';
@@ -1,4 +1,11 @@
1
- import { DotCMSAnalyticsConfig } from './dot-content-analytics.model';
1
+ import { ANALYTICS_WINDOWS_ACTIVE_KEY, ANALYTICS_WINDOWS_CLEANUP_KEY } from '../../../../../uve/src/internal.ts';
2
+ import { DotCMSAnalyticsConfig } from './models';
3
+ declare global {
4
+ interface Window {
5
+ [ANALYTICS_WINDOWS_CLEANUP_KEY]?: () => void;
6
+ [ANALYTICS_WINDOWS_ACTIVE_KEY]?: boolean;
7
+ }
8
+ }
2
9
  /**
3
10
  * Updates session activity with throttling
4
11
  */
@@ -15,7 +22,7 @@ export declare const getSessionInfo: () => {
15
22
  */
16
23
  export declare const initializeActivityTracking: (config: DotCMSAnalyticsConfig) => void;
17
24
  /**
18
- * Cleans up activity tracking listeners
25
+ * Cleans up activity tracking listeners and resets analytics state
19
26
  */
20
27
  export declare const cleanupActivityTracking: () => void;
21
28
  /**
@@ -1,5 +1,6 @@
1
- import { DEFAULT_SESSION_TIMEOUT_MINUTES as c, ACTIVITY_EVENTS as r } from "./dot-content-analytics.constants.js";
2
- class o {
1
+ import { ANALYTICS_WINDOWS_ACTIVE_KEY as o, ANALYTICS_WINDOWS_CLEANUP_KEY as r } from "../../../uve/src/internal/constants.js";
2
+ import { DEFAULT_SESSION_TIMEOUT_MINUTES as c, ACTIVITY_EVENTS as v } from "./constants/dot-content-analytics.constants.js";
3
+ class l {
3
4
  constructor() {
4
5
  this.activityListeners = [], this.lastActivityTime = Date.now(), this.inactivityTimer = null, this.isThrottled = !1, this.config = null, this.ACTIVITY_THROTTLE_MS = 1e3;
5
6
  }
@@ -44,7 +45,7 @@ class o {
44
45
  if (this.cleanup(), this.config = i, typeof window > "u")
45
46
  return;
46
47
  const s = () => this.updateSessionActivity();
47
- r.forEach((a) => {
48
+ v.forEach((a) => {
48
49
  window.addEventListener(a, s, { passive: !0 }), this.activityListeners.push(
49
50
  () => window.removeEventListener(a, s)
50
51
  );
@@ -72,15 +73,15 @@ class o {
72
73
  };
73
74
  }
74
75
  }
75
- const t = new o(), h = () => {
76
+ const t = new l(), u = () => {
76
77
  t.updateSessionActivity();
77
- }, l = (e) => {
78
+ }, d = (e) => {
78
79
  t.initialize(e);
79
- }, T = () => {
80
- t.cleanup();
80
+ }, y = () => {
81
+ t.cleanup(), typeof window < "u" && (window[o] = !1, window[r] = void 0, window.dispatchEvent(new CustomEvent("dotcms:analytics:cleanup")));
81
82
  };
82
83
  export {
83
- T as cleanupActivityTracking,
84
- l as initializeActivityTracking,
85
- h as updateSessionActivity
84
+ y as cleanupActivityTracking,
85
+ d as initializeActivityTracking,
86
+ u as updateSessionActivity
86
87
  };
@@ -1,8 +1,17 @@
1
- import { DotCMSAnalyticsConfig, DotCMSAnalyticsRequestBody } from './dot-content-analytics.model';
1
+ import { DotCMSAnalyticsConfig, DotCMSEvent, DotCMSRequestBody } from './models';
2
2
  /**
3
- * Send an analytics event to the server
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
4
12
  * @param payload - The event payload data
5
13
  * @param config - The analytics configuration
6
- * @returns A promise that resolves when the request is complete
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)
7
16
  */
8
- export declare const sendAnalyticsEventToServer: (payload: DotCMSAnalyticsRequestBody, config: DotCMSAnalyticsConfig) => Promise<void>;
17
+ export declare const sendAnalyticsEvent: (payload: DotCMSRequestBody<DotCMSEvent>, config: DotCMSAnalyticsConfig, transportType?: TransportType) => Promise<void>;
@@ -1,29 +1,41 @@
1
- import { ANALYTICS_ENDPOINT as a } from "./dot-content-analytics.constants.js";
2
- const i = async (o, t) => {
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
+ }
3
15
  try {
4
- const e = await fetch(`${t.server}${a}`, {
16
+ const e = await fetch(a, {
5
17
  method: "POST",
6
18
  headers: { "Content-Type": "application/json" },
7
- body: JSON.stringify(o)
19
+ body: c
8
20
  });
9
21
  if (!e.ok) {
10
- const n = e.statusText || "Unknown Error", s = `HTTP ${e.status}: ${n}`;
22
+ const i = e.statusText || "Unknown Error", r = `HTTP ${e.status}: ${i}`;
11
23
  try {
12
- const r = await e.json();
13
- r.message ? console.warn(`DotAnalytics: ${r.message} (${s})`) : console.warn(
14
- `DotAnalytics: ${s} - No error message in response`
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`
15
27
  );
16
- } catch (r) {
28
+ } catch (t) {
17
29
  console.warn(
18
- `DotAnalytics: ${s} - Failed to parse error response:`,
19
- r
30
+ `DotCMS Analytics: ${r} - Failed to parse error response:`,
31
+ t
20
32
  );
21
33
  }
22
34
  }
23
35
  } catch (e) {
24
- console.error("DotAnalytics: Error sending event:", e);
36
+ console.error("DotCMS Analytics: Error sending event:", e);
25
37
  }
26
38
  };
27
39
  export {
28
- i as sendAnalyticsEventToServer
40
+ d as sendAnalyticsEvent
29
41
  };