@dotcms/analytics 1.1.1 → 1.2.0-next.10

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 (64) hide show
  1. package/README.md +475 -72
  2. package/lib/core/{dot-content-analytics.d.ts → dot-analytics.content.d.ts} +8 -1
  3. package/lib/core/dot-analytics.content.js +63 -0
  4. package/lib/core/plugin/click/dot-analytics.click-tracker.d.ts +108 -0
  5. package/lib/core/plugin/click/dot-analytics.click-tracker.js +144 -0
  6. package/lib/core/plugin/click/dot-analytics.click.plugin.d.ts +36 -0
  7. package/lib/core/plugin/click/dot-analytics.click.plugin.js +27 -0
  8. package/lib/core/plugin/click/dot-analytics.click.utils.d.ts +12 -0
  9. package/lib/core/plugin/click/dot-analytics.click.utils.js +55 -0
  10. package/lib/core/plugin/enricher/dot-analytics.enricher.plugin.d.ts +18 -30
  11. package/lib/core/plugin/enricher/dot-analytics.enricher.plugin.js +30 -20
  12. package/lib/core/plugin/identity/dot-analytics.identity.activity-tracker.d.ts +20 -0
  13. package/lib/core/{shared/dot-content-analytics.activity-tracker.js → plugin/identity/dot-analytics.identity.activity-tracker.js} +19 -38
  14. package/lib/core/plugin/identity/dot-analytics.identity.plugin.d.ts +13 -31
  15. package/lib/core/plugin/identity/dot-analytics.identity.plugin.js +14 -12
  16. package/lib/core/plugin/identity/dot-analytics.identity.utils.d.ts +7 -22
  17. package/lib/core/plugin/impression/dot-analytics.impression-tracker.d.ts +62 -0
  18. package/lib/core/plugin/impression/dot-analytics.impression-tracker.js +200 -0
  19. package/lib/core/plugin/impression/dot-analytics.impression.plugin.d.ts +40 -0
  20. package/lib/core/plugin/impression/dot-analytics.impression.plugin.js +27 -0
  21. package/lib/core/plugin/impression/dot-analytics.impression.utils.d.ts +26 -0
  22. package/lib/core/plugin/impression/dot-analytics.impression.utils.js +27 -0
  23. package/lib/core/plugin/impression/index.d.ts +2 -0
  24. package/lib/core/plugin/main/dot-analytics.plugin.d.ts +46 -0
  25. package/lib/core/plugin/main/dot-analytics.plugin.js +114 -0
  26. package/lib/core/shared/constants/dot-analytics.constants.d.ts +131 -0
  27. package/lib/core/shared/constants/dot-analytics.constants.js +52 -0
  28. package/lib/core/shared/constants/index.d.ts +4 -0
  29. package/lib/core/shared/dot-analytics.logger.d.ts +85 -0
  30. package/lib/core/shared/dot-analytics.logger.js +90 -0
  31. package/lib/core/shared/http/dot-analytics.http.d.ts +9 -0
  32. package/lib/core/shared/http/dot-analytics.http.js +34 -0
  33. package/lib/core/shared/models/data.model.d.ts +141 -0
  34. package/lib/core/shared/models/event.model.d.ts +135 -0
  35. package/lib/core/shared/models/index.d.ts +7 -0
  36. package/lib/core/shared/models/library.model.d.ts +243 -0
  37. package/lib/core/shared/models/request.model.d.ts +32 -0
  38. package/lib/core/shared/queue/dot-analytics.queue.utils.d.ts +28 -0
  39. package/lib/core/shared/queue/dot-analytics.queue.utils.js +80 -0
  40. package/lib/core/shared/queue/index.d.ts +1 -0
  41. package/lib/core/shared/utils/dot-analytics.utils.d.ts +260 -0
  42. package/lib/core/shared/utils/dot-analytics.utils.js +202 -0
  43. package/lib/react/components/DotContentAnalytics.d.ts +1 -1
  44. package/lib/react/hook/useContentAnalytics.d.ts +43 -15
  45. package/lib/react/hook/useContentAnalytics.js +18 -21
  46. package/lib/react/hook/useRouterTracker.d.ts +1 -1
  47. package/lib/react/hook/useRouterTracker.js +4 -4
  48. package/lib/react/internal/utils.d.ts +1 -1
  49. package/lib/react/internal/utils.js +3 -3
  50. package/lib/react/public-api.d.ts +1 -1
  51. package/lib/standalone.d.ts +2 -2
  52. package/package.json +7 -5
  53. package/uve/src/internal/constants.js +8 -3
  54. package/lib/core/dot-content-analytics.js +0 -43
  55. package/lib/core/plugin/dot-analytics.plugin.d.ts +0 -32
  56. package/lib/core/plugin/dot-analytics.plugin.js +0 -79
  57. package/lib/core/shared/dot-content-analytics.activity-tracker.d.ts +0 -28
  58. package/lib/core/shared/dot-content-analytics.constants.d.ts +0 -37
  59. package/lib/core/shared/dot-content-analytics.constants.js +0 -14
  60. package/lib/core/shared/dot-content-analytics.http.d.ts +0 -8
  61. package/lib/core/shared/dot-content-analytics.http.js +0 -29
  62. package/lib/core/shared/dot-content-analytics.model.d.ts +0 -351
  63. package/lib/core/shared/dot-content-analytics.utils.d.ts +0 -111
  64. package/lib/core/shared/dot-content-analytics.utils.js +0 -123
@@ -0,0 +1,141 @@
1
+ /**
2
+ * Data models for DotCMS Analytics
3
+ * Contains interfaces for data structures used in analytics events
4
+ */
5
+ /**
6
+ * Browser event data collected from the user's session in DotCMS.
7
+ * Contains comprehensive information about the user's browser environment,
8
+ * page context, and session details for analytics tracking.
9
+ *
10
+ * This is an internal type used by utility functions.
11
+ */
12
+ export interface DotCMSBrowserData {
13
+ /** UTC timestamp when the event occurred */
14
+ utc_time: string;
15
+ /** Local timezone offset in minutes */
16
+ local_tz_offset: number;
17
+ /** Screen resolution as a string (e.g., "1920x1080") */
18
+ screen_resolution: string;
19
+ /** Viewport size as a string (e.g., "1200x800") */
20
+ vp_size: string;
21
+ /** User's preferred language */
22
+ user_language: string;
23
+ /** Document encoding */
24
+ doc_encoding: string;
25
+ /** Document path */
26
+ doc_path: string;
27
+ /** Document host */
28
+ doc_host: string;
29
+ /** Document protocol (http/https) */
30
+ doc_protocol: string;
31
+ /** Document hash fragment */
32
+ doc_hash: string;
33
+ /** Document search parameters */
34
+ doc_search: string;
35
+ /** Referrer URL */
36
+ referrer: string;
37
+ /** Page title */
38
+ page_title: string;
39
+ /** Current page URL */
40
+ url: string;
41
+ /** UTM parameters for campaign tracking */
42
+ utm: DotCMSEventUtmData;
43
+ }
44
+ /**
45
+ * Analytics context shared across all events in DotCMS.
46
+ * Contains session and user identification data that provides
47
+ * continuity across multiple analytics events.
48
+ */
49
+ export interface DotCMSAnalyticsEventContext {
50
+ /** The site key for the DotCMS instance */
51
+ site_auth: string;
52
+ /** Unique session identifier */
53
+ session_id: string;
54
+ /** Unique user identifier */
55
+ user_id: string;
56
+ /** Device and browser information */
57
+ device: DotCMSEventDeviceData;
58
+ }
59
+ /**
60
+ * Device and browser information for DotCMS analytics tracking.
61
+ * Contains technical details about the user's device and browser environment.
62
+ */
63
+ export interface DotCMSEventDeviceData {
64
+ /** Screen resolution as a string (e.g., "1920x1080") */
65
+ screen_resolution: string;
66
+ /** User's preferred language */
67
+ language: string;
68
+ /** Viewport width in pixels */
69
+ viewport_width: string;
70
+ /** Viewport height in pixels */
71
+ viewport_height: string;
72
+ }
73
+ /**
74
+ * UTM (Urchin Tracking Module) parameters for DotCMS campaign tracking.
75
+ * Contains marketing campaign attribution data extracted from URL parameters.
76
+ */
77
+ export interface DotCMSEventUtmData {
78
+ /** The marketing medium (e.g., email, social, cpc) */
79
+ medium?: string;
80
+ /** The traffic source (e.g., google, newsletter) */
81
+ source?: string;
82
+ /** The campaign name */
83
+ campaign?: string;
84
+ /** The campaign term or keyword */
85
+ term?: string;
86
+ /** The campaign content or ad variation */
87
+ content?: string;
88
+ /** The campaign ID for tracking specific campaigns */
89
+ id?: string;
90
+ }
91
+ /**
92
+ * Page data structure for DotCMS analytics (used in pageview events).
93
+ * Contains comprehensive information about the current page and its context
94
+ * within the DotCMS environment.
95
+ */
96
+ export type DotCMSEventPageData = Pick<DotCMSBrowserData, 'url' | 'doc_path' | 'doc_hash' | 'doc_search' | 'doc_host' | 'doc_protocol' | 'doc_encoding'> & {
97
+ /** Page title */
98
+ title: string | undefined;
99
+ /** Language identifier */
100
+ language_id?: string;
101
+ /** Persona identifier */
102
+ persona?: string;
103
+ };
104
+ /**
105
+ * Minimal page data for content impression events.
106
+ * Contains only essential page information (title and url) to keep payload lightweight.
107
+ */
108
+ export type DotCMSContentImpressionPageData = Pick<DotCMSEventPageData, 'title' | 'url'>;
109
+ /**
110
+ * Data structure for content impression events.
111
+ * Tracks when a contentlet becomes visible in the viewport.
112
+ */
113
+ export interface DotCMSImpressionEventData {
114
+ /** Contentlet identification data extracted from data-dot-analytics-* attributes */
115
+ contentlet: {
116
+ /** Unique identifier of the contentlet */
117
+ identifier: string;
118
+ /** Inode of the contentlet */
119
+ inode: string;
120
+ /** Content type name */
121
+ contentType: string;
122
+ /** Title of the contentlet */
123
+ title: string;
124
+ /** Base type of the contentlet (e.g., CONTENT, WIDGET) */
125
+ baseType: string;
126
+ };
127
+ /** Viewport position and visibility metrics */
128
+ viewport: {
129
+ /** Percentage offset from top of viewport (0-100) */
130
+ offsetPercentage: number;
131
+ /** Percentage of element visible in viewport (0-1) */
132
+ visibilityRatio: number;
133
+ };
134
+ /** Timing information about the impression */
135
+ timing: {
136
+ /** Time in milliseconds the element was continuously visible */
137
+ dwellTime: number;
138
+ /** ISO 8601 timestamp when the impression was fired */
139
+ timestamp: string;
140
+ };
141
+ }
@@ -0,0 +1,135 @@
1
+ import { DotCMSContentImpressionPageData, DotCMSEventPageData, DotCMSEventUtmData } from './data.model';
2
+ import { DotCMSCustomEventType, DotCMSEventType, DotCMSPredefinedEventType } from '../constants/dot-analytics.constants';
3
+ /**
4
+ * JSON value type for analytics custom data.
5
+ * Represents any valid JSON value that can be serialized and sent to the analytics server.
6
+ */
7
+ export type JsonValue = string | number | boolean | null | undefined | JsonObject | JsonArray;
8
+ /**
9
+ * JSON object type for analytics custom data.
10
+ */
11
+ export type JsonObject = {
12
+ [key: string]: JsonValue;
13
+ };
14
+ /**
15
+ * JSON array type for analytics custom data.
16
+ */
17
+ export type JsonArray = JsonValue[];
18
+ /**
19
+ * Generic base event structure for DotCMS Analytics.
20
+ * All events share this base structure with customizable event type and data.
21
+ *
22
+ * @template TEventType - The type of the event (pageview, custom event name, etc.)
23
+ * @template TData - The data structure for the event
24
+ */
25
+ export interface DotCMSEventBase<TEventType extends DotCMSEventType, TData> {
26
+ /** The type of event being tracked */
27
+ event_type: TEventType;
28
+ /** Local timestamp when the event occurred */
29
+ local_time: string;
30
+ /** Event-specific data with structured format */
31
+ data: TData;
32
+ }
33
+ /**
34
+ * Data structure for pageview events.
35
+ * Contains page and optional UTM/custom data.
36
+ */
37
+ export type DotCMSPageViewEventData = {
38
+ /** Page data associated with the event */
39
+ page: DotCMSEventPageData;
40
+ /** UTM parameters for campaign tracking (optional) */
41
+ utm?: DotCMSEventUtmData;
42
+ /** Custom data associated with the event (any valid JSON) */
43
+ custom?: JsonObject;
44
+ };
45
+ /**
46
+ * Data structure for custom events.
47
+ * Contains user-defined custom data.
48
+ */
49
+ export type DotCMSCustomEventData = {
50
+ /** Custom data associated with the event (any valid JSON) */
51
+ custom: JsonObject;
52
+ };
53
+ /**
54
+ * Partial content impression data sent by producer plugins.
55
+ * Contains only impression-specific data (content and position).
56
+ * The enricher plugin will add page data automatically.
57
+ */
58
+ export type DotCMSContentImpressionPayload = {
59
+ /** Content information */
60
+ content: {
61
+ /** Content identifier */
62
+ identifier: string;
63
+ /** Content inode */
64
+ inode: string;
65
+ /** Content title */
66
+ title: string;
67
+ /** Content type name */
68
+ content_type: string;
69
+ };
70
+ /** Position information in the viewport and DOM */
71
+ position: {
72
+ /** Viewport offset percentage from top */
73
+ viewport_offset_pct: number;
74
+ /** DOM index position */
75
+ dom_index: number;
76
+ };
77
+ };
78
+ /**
79
+ * Partial content click data sent by producer plugins.
80
+ * Extends impression payload with element metadata.
81
+ */
82
+ export type DotCMSContentClickPayload = DotCMSContentImpressionPayload & {
83
+ /** Clicked element information */
84
+ element: {
85
+ /** Text content of the element */
86
+ text: string;
87
+ /** Type of element (anchor, button, etc.) */
88
+ type: string;
89
+ /** Element ID (required by backend, empty string if not present) */
90
+ id: string;
91
+ /** Element classes (required by backend, empty string if not present) */
92
+ class: string;
93
+ /** Link destination as written in HTML (relative path, only for <a> elements, empty string for buttons) */
94
+ href: string;
95
+ /** Additional element attributes in key:value format (e.g., ['data-category:val', 'data-campaign:val2']) */
96
+ attributes: string[];
97
+ };
98
+ };
99
+ /**
100
+ * Complete data structure for content impression events after enrichment.
101
+ * Includes minimal page data (title and url) added by the enricher plugin.
102
+ */
103
+ export type DotCMSContentImpressionEventData = DotCMSContentImpressionPayload & {
104
+ /** Minimal page data where the impression occurred (added by enricher) */
105
+ page: DotCMSContentImpressionPageData;
106
+ };
107
+ /**
108
+ * Complete data structure for content click events after enrichment.
109
+ * Includes minimal page data (title and url) added by the enricher plugin.
110
+ */
111
+ export type DotCMSContentClickEventData = DotCMSContentClickPayload & {
112
+ /** Minimal page data where the click occurred (added by enricher) */
113
+ page: DotCMSContentImpressionPageData;
114
+ };
115
+ /**
116
+ * Pageview event structure.
117
+ */
118
+ export type DotCMSPageViewEvent = DotCMSEventBase<typeof DotCMSPredefinedEventType.PAGEVIEW, DotCMSPageViewEventData>;
119
+ /**
120
+ * Content impression event structure.
121
+ */
122
+ export type DotCMSContentImpressionEvent = DotCMSEventBase<typeof DotCMSPredefinedEventType.CONTENT_IMPRESSION, DotCMSContentImpressionEventData>;
123
+ /**
124
+ * Content click event structure.
125
+ */
126
+ export type DotCMSContentClickEvent = DotCMSEventBase<typeof DotCMSPredefinedEventType.CONTENT_CLICK, DotCMSContentClickEventData>;
127
+ /**
128
+ * Custom event structure.
129
+ */
130
+ export type DotCMSCustomEvent = DotCMSEventBase<DotCMSCustomEventType, DotCMSCustomEventData>;
131
+ /**
132
+ * Union type for all possible analytics events.
133
+ * Used primarily for type documentation and validation.
134
+ */
135
+ export type DotCMSEvent = DotCMSPageViewEvent | DotCMSContentImpressionEvent | DotCMSContentClickEvent | DotCMSCustomEvent;
@@ -0,0 +1,7 @@
1
+ /**
2
+ * Central export point for all DotCMS Analytics models
3
+ */
4
+ export * from './data.model';
5
+ export * from './event.model';
6
+ export * from './request.model';
7
+ export * from './library.model';
@@ -0,0 +1,243 @@
1
+ import { DotCMSAnalyticsEventContext, DotCMSContentImpressionPageData, DotCMSEventPageData, DotCMSEventUtmData } from './data.model';
2
+ import { DotCMSContentImpressionPayload, JsonObject } from './event.model';
3
+ import { DotCMSAnalyticsRequestBody } from './request.model';
4
+ import { LogLevel } from '../dot-analytics.logger';
5
+ /**
6
+ * Configuration for event queue management.
7
+ * Controls how events are batched before sending to the server.
8
+ */
9
+ export interface QueueConfig {
10
+ /** Maximum events per batch - auto-sends when reached (default: 15) */
11
+ eventBatchSize?: number;
12
+ /** Time in milliseconds between flushes - sends pending events (default: 5000) */
13
+ flushInterval?: number;
14
+ }
15
+ /**
16
+ * Configuration for content impression tracking.
17
+ * Controls how content visibility is detected and tracked.
18
+ */
19
+ export interface ImpressionConfig {
20
+ /** Minimum percentage of element visible (0.0 to 1.0) - default: 0.5 */
21
+ visibilityThreshold?: number;
22
+ /** Minimum time in milliseconds element must be visible - default: 750 */
23
+ dwellMs?: number;
24
+ /** Maximum number of elements to track (performance limit) - default: 1000 */
25
+ maxNodes?: number;
26
+ /** Throttle time in milliseconds for intersection callbacks - default: 100 */
27
+ throttleMs?: number;
28
+ }
29
+ /**
30
+ * Interface for contentlet data extracted from DOM elements
31
+ */
32
+ export interface ContentletData {
33
+ identifier: string;
34
+ inode: string;
35
+ contentType: string;
36
+ title: string;
37
+ baseType: string;
38
+ }
39
+ /**
40
+ * Interface for viewport metrics
41
+ */
42
+ export interface ViewportMetrics {
43
+ offsetPercentage: number;
44
+ visibilityRatio: number;
45
+ }
46
+ /**
47
+ * Main interface for the DotCMS Analytics SDK.
48
+ * Provides the core methods for tracking page views and custom events.
49
+ */
50
+ export interface DotCMSAnalytics {
51
+ /**
52
+ * Track a page view event.
53
+ * @param payload - Optional custom data to include with the page view (any valid JSON object)
54
+ */
55
+ pageView: (payload?: JsonObject) => void;
56
+ /**
57
+ * Track a custom event.
58
+ * @param eventName - The name/type of the event to track
59
+ * @param payload - Custom data to include with the event (any valid JSON object)
60
+ */
61
+ track: (eventName: string, payload: JsonObject) => void;
62
+ }
63
+ /**
64
+ * Configuration interface for DotCMS Analytics SDK.
65
+ * Contains all necessary settings for initializing and configuring the analytics client.
66
+ */
67
+ export interface DotCMSAnalyticsConfig {
68
+ /**
69
+ * The URL of the Analytics server endpoint.
70
+ */
71
+ server: string;
72
+ /**
73
+ * Enable debug mode to get additional logging information.
74
+ */
75
+ debug: boolean;
76
+ /**
77
+ * Set the minimum log level for console output.
78
+ * - 'debug': Show all logs including detailed debugging information
79
+ * - 'info': Show informational messages, warnings, and errors
80
+ * - 'warn': Show only warnings and errors
81
+ * - 'error': Show only errors
82
+ *
83
+ * If not specified, falls back to debug flag (debug=true → 'debug', debug=false → 'warn')
84
+ */
85
+ logLevel?: LogLevel;
86
+ /**
87
+ * Automatically track page views when set to true.
88
+ */
89
+ autoPageView?: boolean;
90
+ /**
91
+ * The site auth for authenticating with the Analytics service.
92
+ */
93
+ siteAuth: string;
94
+ /**
95
+ * Queue configuration for event batching:
96
+ * - `false`: Disable queuing, send events immediately
97
+ * - `true` or `undefined` (default): Enable queuing with default settings
98
+ * - `QueueConfig`: Enable queuing with custom settings
99
+ */
100
+ queue?: QueueConfig | boolean;
101
+ /**
102
+ * Content impression tracking configuration (default: undefined - disabled):
103
+ * - `undefined` or `false`: Impression tracking disabled
104
+ * - `true`: Enable with default settings (threshold: 0.5, dwell: 750ms, maxNodes: 1000)
105
+ * - `ImpressionConfig`: Enable with custom settings
106
+ */
107
+ impressions?: ImpressionConfig | boolean;
108
+ /**
109
+ * Content click tracking configuration (default: undefined - disabled):
110
+ * - `undefined` or `false`: Click tracking disabled
111
+ * - `true`: Enable click tracking
112
+ */
113
+ clicks?: boolean;
114
+ }
115
+ /**
116
+ * Track event payload with context.
117
+ * This is the payload for track events after the identity plugin adds context.
118
+ * Used in the track:dot-analytics enricher plugin.
119
+ */
120
+ export interface AnalyticsTrackPayloadWithContext extends AnalyticsBasePayload {
121
+ /** The event name (can be predefined or custom) */
122
+ event: string;
123
+ /** Analytics context added by identity plugin */
124
+ context: DotCMSAnalyticsEventContext;
125
+ }
126
+ /**
127
+ * Parameters passed to DotCMS Analytics plugin methods (after enrichment).
128
+ * The payload is the complete request body ready to send to the server.
129
+ */
130
+ export interface DotCMSAnalyticsParams {
131
+ /** Configuration for the analytics client */
132
+ config: DotCMSAnalyticsConfig;
133
+ /** The complete request body */
134
+ payload: DotCMSAnalyticsRequestBody;
135
+ }
136
+ type AnalyticsBasePayloadType = 'page' | 'track';
137
+ /**
138
+ * Analytics.js hook parameter types for DotCMS.
139
+ * Represents the payload structure used by Analytics.js lifecycle hooks
140
+ * for intercepting and modifying analytics events.
141
+ *
142
+ * Properties are flexible (Record<string, unknown>) to support both:
143
+ * - Page events: with page-specific fields (title, url, path, etc.)
144
+ * - Track events: with any custom event data structure
145
+ */
146
+ export interface AnalyticsBasePayload {
147
+ /** The type of analytics event */
148
+ type: AnalyticsBasePayloadType;
149
+ /** Properties associated with the event (flexible structure) */
150
+ properties: Record<string, unknown>;
151
+ /** Configuration options for the event */
152
+ options: Record<string, unknown>;
153
+ /** User identifier */
154
+ userId: string;
155
+ /** Anonymous user identifier */
156
+ anonymousId: string;
157
+ /** Metadata about the event */
158
+ meta: {
159
+ /** Request identifier */
160
+ rid: string;
161
+ /** Timestamp */
162
+ ts: number;
163
+ /** Whether the event has a callback function */
164
+ hasCallback: boolean;
165
+ };
166
+ }
167
+ /**
168
+ * Analytics.js payload with context.
169
+ * This is the result of enriching the base Analytics.js payload
170
+ * with context data added by the identity plugin.
171
+ */
172
+ export interface AnalyticsBasePayloadWithContext extends AnalyticsBasePayload {
173
+ context: DotCMSAnalyticsEventContext;
174
+ }
175
+ /**
176
+ * Enriched analytics payload with DotCMS-specific data.
177
+ * This is the result of enriching the base Analytics.js payload with context (from identity plugin)
178
+ * and then adding page, UTM, and custom data (from enricher plugin).
179
+ */
180
+ export type EnrichedAnalyticsPayload = AnalyticsBasePayloadWithContext & {
181
+ /** Page data for the current page */
182
+ page: DotCMSEventPageData;
183
+ /** UTM parameters for campaign tracking */
184
+ utm?: DotCMSEventUtmData;
185
+ /** Custom data associated with the event (any valid JSON) */
186
+ custom?: JsonObject;
187
+ /** Local timestamp when the event occurred */
188
+ local_time: string;
189
+ };
190
+ /**
191
+ * Enriched track event payload with fields added to root based on event type.
192
+ * Used by the enricher plugin for track events.
193
+ */
194
+ export interface EnrichedTrackPayload extends AnalyticsTrackPayloadWithContext {
195
+ local_time: string;
196
+ page?: DotCMSContentImpressionPageData | DotCMSEventPageData;
197
+ content?: DotCMSContentImpressionPayload['content'];
198
+ position?: DotCMSContentImpressionPayload['position'];
199
+ utm?: DotCMSEventUtmData;
200
+ }
201
+ /**
202
+ * Analytics.js instance structure for DotCMS.
203
+ * Represents the internal structure of an Analytics.js instance,
204
+ * providing access to plugins, storage, and event configuration.
205
+ */
206
+ export interface DotCMSAnalyticsInstance {
207
+ /** Available plugins and their configurations */
208
+ plugins: Record<string, unknown>;
209
+ /** Storage mechanisms for analytics data */
210
+ storage: Record<string, unknown>;
211
+ /** Event configuration */
212
+ events: {
213
+ /** Core event types */
214
+ core: string[];
215
+ /** Plugin-specific event types */
216
+ plugins: string[];
217
+ };
218
+ }
219
+ /**
220
+ * Base parameters structure passed by Analytics.js to plugin hooks.
221
+ * Contains all the context and data needed for Analytics.js lifecycle hooks
222
+ * to process and modify analytics events.
223
+ */
224
+ export interface AnalyticsBaseParams {
225
+ /** The event payload data */
226
+ payload: AnalyticsBasePayload;
227
+ /** The analytics instance */
228
+ instance: DotCMSAnalyticsInstance;
229
+ /** Global configuration settings */
230
+ config: Record<string, unknown>;
231
+ /** Available plugins and their status */
232
+ plugins: Record<string, {
233
+ /** Whether the plugin is enabled */
234
+ enabled: boolean;
235
+ /** Whether the plugin is initialized */
236
+ initialized: boolean;
237
+ /** Whether the plugin is loaded */
238
+ loaded: boolean;
239
+ /** Plugin-specific configuration */
240
+ config: Record<string, unknown>;
241
+ }>;
242
+ }
243
+ export {};
@@ -0,0 +1,32 @@
1
+ import { DotCMSAnalyticsEventContext } from './data.model';
2
+ import { DotCMSEvent } from './event.model';
3
+ /**
4
+ * Analytics request body for DotCMS Analytics.
5
+ * Generic structure sent to the DotCMS analytics server.
6
+ *
7
+ * This structure contains properly typed events that match the DotCMS event specifications.
8
+ * Events can be pageviews, content impressions, or custom events, each with their own data structure.
9
+ */
10
+ export interface DotCMSRequestBody {
11
+ /** Context information shared across all events */
12
+ context: DotCMSAnalyticsEventContext;
13
+ /** Array of analytics events to be tracked */
14
+ events: DotCMSEvent[];
15
+ }
16
+ /**
17
+ * Main type for analytics request bodies.
18
+ * Flexible enough to accept any event type (pageview, content_impression, custom, etc.)
19
+ */
20
+ export type DotCMSAnalyticsRequestBody = DotCMSRequestBody;
21
+ /**
22
+ * Specific request body type for PageView events (for type documentation)
23
+ */
24
+ export type DotCMSPageViewRequestBody = DotCMSRequestBody;
25
+ /**
26
+ * Specific request body type for ContentImpression events (for type documentation)
27
+ */
28
+ export type DotCMSContentImpressionRequestBody = DotCMSRequestBody;
29
+ /**
30
+ * Specific request body type for Custom events (for type documentation)
31
+ */
32
+ export type DotCMSCustomEventRequestBody = DotCMSRequestBody;
@@ -0,0 +1,28 @@
1
+ import { DotCMSAnalyticsConfig, DotCMSAnalyticsEventContext, DotCMSEvent } from '../models';
2
+ /**
3
+ * Creates a queue manager for batching analytics events.
4
+ * Uses factory function pattern consistent with the plugin architecture.
5
+ */
6
+ export declare const createAnalyticsQueue: (config: DotCMSAnalyticsConfig) => {
7
+ /**
8
+ * Initialize the queue with smart batching
9
+ */
10
+ initialize: () => void;
11
+ /**
12
+ * Add event to queue
13
+ * smartQueue handles all batching logic automatically:
14
+ * - Sends immediately when eventBatchSize reached (with throttle: false)
15
+ * - Sends pending events every flushInterval
16
+ */
17
+ enqueue: (event: DotCMSEvent, context: DotCMSAnalyticsEventContext) => void;
18
+ /**
19
+ * Get queue size for debugging
20
+ * Returns the number of events in smartQueue
21
+ */
22
+ size: () => number;
23
+ /**
24
+ * Clean up queue resources
25
+ * Flushes remaining events and cleans up listeners
26
+ */
27
+ cleanup: () => void;
28
+ };
@@ -0,0 +1,80 @@
1
+ import m from "@analytics/queue-utils";
2
+ import v from "@analytics/router-utils";
3
+ import { DEFAULT_QUEUE_CONFIG as y } from "../constants/dot-analytics.constants.js";
4
+ import { sendAnalyticsEvent as b } from "../http/dot-analytics.http.js";
5
+ import { createPluginLogger as w } from "../utils/dot-analytics.utils.js";
6
+ const L = (o) => {
7
+ const i = w("Queue", o);
8
+ let e = null, n = null, u = !1, d = !1, f = typeof window < "u" ? window.location.pathname : "";
9
+ const a = {
10
+ ...y,
11
+ ...typeof o.queue == "object" ? o.queue : {}
12
+ }, g = (t, s) => {
13
+ if (!n) return;
14
+ i.debug(`Sending batch of ${t.length} event(s)`, {
15
+ events: t,
16
+ keepalive: u
17
+ }), b({ context: n, events: t }, o, u);
18
+ }, l = () => {
19
+ !e || e.size() === 0 || !n || (i.info(`Flushing ${e.size()} events (page hidden/unload)`), u = !0, e.flush(!0));
20
+ }, c = () => {
21
+ if (i.debug("handleVisibilityChange", document.visibilityState), document.visibilityState === "hidden") {
22
+ if (d) {
23
+ i.debug("Skipping flush (SPA navigation detected)");
24
+ return;
25
+ }
26
+ l();
27
+ } else document.visibilityState === "visible" && (d = !1);
28
+ };
29
+ return {
30
+ /**
31
+ * Initialize the queue with smart batching
32
+ */
33
+ initialize: () => {
34
+ e = m(
35
+ (t, s) => {
36
+ g(t);
37
+ },
38
+ {
39
+ max: a.eventBatchSize,
40
+ interval: a.flushInterval,
41
+ throttle: !1
42
+ // Always false - enables both batch size and interval triggers
43
+ }
44
+ ), typeof window < "u" && typeof document < "u" && (document.addEventListener("visibilitychange", c), window.addEventListener("pagehide", l), v((t) => {
45
+ d = !0, f = t, i.debug(`SPA navigation detected (${f})`), setTimeout(() => {
46
+ d = !1;
47
+ }, 100);
48
+ }));
49
+ },
50
+ /**
51
+ * Add event to queue
52
+ * smartQueue handles all batching logic automatically:
53
+ * - Sends immediately when eventBatchSize reached (with throttle: false)
54
+ * - Sends pending events every flushInterval
55
+ */
56
+ enqueue: (t, s) => {
57
+ if (n = s, !e) return;
58
+ const r = e.size() + 1, p = a.eventBatchSize, h = r >= p;
59
+ i.debug(
60
+ `Event added. Queue size: ${r}/${p}${h ? " (full, sending...)" : ""}`,
61
+ { eventType: t.event_type, event: t }
62
+ ), e.push(t);
63
+ },
64
+ /**
65
+ * Get queue size for debugging
66
+ * Returns the number of events in smartQueue
67
+ */
68
+ size: () => (e == null ? void 0 : e.size()) ?? 0,
69
+ /**
70
+ * Clean up queue resources
71
+ * Flushes remaining events and cleans up listeners
72
+ */
73
+ cleanup: () => {
74
+ l(), typeof window < "u" && typeof document < "u" && (document.removeEventListener("visibilitychange", c), window.removeEventListener("pagehide", l)), e = null, n = null, u = !1;
75
+ }
76
+ };
77
+ };
78
+ export {
79
+ L as createAnalyticsQueue
80
+ };
@@ -0,0 +1 @@
1
+ export { createAnalyticsQueue } from './dot-analytics.queue.utils';