@dotcms/analytics 0.0.1-beta.41 → 0.0.1-beta.43

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 (28) hide show
  1. package/lib/dotAnalytics/dot-content-analytics.d.ts +5 -5
  2. package/lib/dotAnalytics/dot-content-analytics.js +28 -9
  3. package/lib/dotAnalytics/plugin/dot-analytics.plugin.d.ts +9 -7
  4. package/lib/dotAnalytics/plugin/dot-analytics.plugin.js +30 -31
  5. package/lib/dotAnalytics/plugin/enricher/dot-analytics.enricher.plugin.d.ts +46 -0
  6. package/lib/dotAnalytics/plugin/enricher/dot-analytics.enricher.plugin.js +33 -0
  7. package/lib/dotAnalytics/plugin/identity/dot-analytics.identity.plugin.d.ts +80 -0
  8. package/lib/dotAnalytics/plugin/identity/dot-analytics.identity.plugin.js +40 -0
  9. package/lib/dotAnalytics/plugin/identity/dot-analytics.identity.utils.d.ts +24 -0
  10. package/lib/dotAnalytics/shared/dot-content-analytics.activity-tracker.d.ts +29 -0
  11. package/lib/dotAnalytics/shared/dot-content-analytics.activity-tracker.js +86 -0
  12. package/lib/dotAnalytics/shared/dot-content-analytics.constants.d.ts +16 -7
  13. package/lib/dotAnalytics/shared/dot-content-analytics.constants.js +8 -8
  14. package/lib/dotAnalytics/shared/dot-content-analytics.http.d.ts +2 -2
  15. package/lib/dotAnalytics/shared/dot-content-analytics.http.js +2 -8
  16. package/lib/dotAnalytics/shared/dot-content-analytics.model.d.ts +263 -89
  17. package/lib/dotAnalytics/shared/dot-content-analytics.utils.d.ts +91 -28
  18. package/lib/dotAnalytics/shared/dot-content-analytics.utils.js +125 -40
  19. package/lib/react/components/DotContentAnalyticsProvider.d.ts +4 -4
  20. package/lib/react/components/DotContentAnalyticsProvider.js +5 -2
  21. package/lib/react/contexts/DotContentAnalyticsContext.d.ts +3 -3
  22. package/lib/react/hook/useContentAnalytics.d.ts +36 -6
  23. package/lib/react/hook/useContentAnalytics.js +25 -25
  24. package/lib/react/hook/useRouterTracker.d.ts +3 -3
  25. package/lib/standalone.d.ts +2 -2
  26. package/package.json +4 -4
  27. package/lib/dotAnalytics/plugin/dot-analytics.enricher.plugin.d.ts +0 -31
  28. package/lib/dotAnalytics/plugin/dot-analytics.enricher.plugin.js +0 -28
@@ -1,11 +1,13 @@
1
- import { ANALYTICS_PAGEVIEW_EVENT, EventType, EXPECTED_UTM_KEYS } from './dot-content-analytics.constants';
2
-
1
+ declare global {
2
+ interface Window {
3
+ __dotAnalyticsCleanup?: () => void;
4
+ }
5
+ }
3
6
  /**
4
- * Configuration interface for DotAnalytics SDK.
5
- *
6
- * @interface DotAnalyticsConfig
7
+ * Configuration interface for DotCMS Analytics SDK.
8
+ * Contains all necessary settings for initializing and configuring the analytics client.
7
9
  */
8
- export interface DotContentAnalyticsConfig {
10
+ export interface DotCMSAnalyticsConfig {
9
11
  /**
10
12
  * The URL of the Analytics server endpoint.
11
13
  */
@@ -19,125 +21,297 @@ export interface DotContentAnalyticsConfig {
19
21
  */
20
22
  autoPageView?: boolean;
21
23
  /**
22
- * The API key for authenticating with the Analytics service.
24
+ * The site key for authenticating with the Analytics service.
23
25
  */
24
- apiKey: string;
26
+ siteKey: string;
25
27
  /**
26
28
  * Custom redirect function handler.
27
29
  * When provided, this function will be called instead of the default browser redirect
28
30
  * for handling URL redirections.
29
- *
30
- * @param {string} url - The URL to redirect to
31
31
  */
32
32
  redirectFn?: (url: string) => void;
33
33
  }
34
- type UTMParams = {
35
- [key in (typeof EXPECTED_UTM_KEYS)[number] as key extends `utm_${infer U}` ? U : never]?: string;
36
- };
37
- interface BaseEventData {
38
- anonymousId?: string;
39
- src: string;
40
- utc_time: string;
41
- local_tz_offset: number;
42
- doc_path: string;
43
- doc_host: string;
34
+ /**
35
+ * Individual analytics event structure for DotCMS.
36
+ * Represents a single event within an analytics request sent to the DotCMS analytics server.
37
+ */
38
+ export interface DotCMSAnalyticsEvent {
39
+ /** The type of event being tracked */
40
+ event_type: 'pageview' | 'track';
41
+ /** Page data associated with the event */
42
+ page: DotCMSPageData;
43
+ /** Device and browser information */
44
+ device: DotCMSDeviceData;
45
+ /** UTM parameters for campaign tracking */
46
+ utm?: DotCMSUtmData;
47
+ /** Local timestamp when the event occurred */
48
+ local_time: string;
49
+ }
50
+ /**
51
+ * Analytics request body for page view events in DotCMS.
52
+ * Structure sent to the DotCMS analytics server for page tracking.
53
+ */
54
+ export interface DotCMSPageViewRequestBody {
55
+ /** Context information shared across all events */
56
+ context: DotCMSAnalyticsContext;
57
+ /** Array of analytics events to be tracked */
58
+ events: DotCMSAnalyticsEvent[];
59
+ }
60
+ /**
61
+ * Analytics request body for track events in DotCMS.
62
+ * Structure sent to the DotCMS analytics server for custom event tracking.
63
+ */
64
+ export interface DotCMSTrackRequestBody {
65
+ /** Context information shared across all events */
66
+ context: DotCMSAnalyticsContext;
67
+ /** Array of analytics events to be tracked */
68
+ events?: DotCMSAnalyticsEvent[];
44
69
  }
45
- export interface BrowserEventData {
70
+ /**
71
+ * Browser event data collected from the user's session in DotCMS.
72
+ * Contains comprehensive information about the user's browser environment,
73
+ * page context, and session details for analytics tracking.
74
+ */
75
+ export interface DotCMSBrowserEventData {
76
+ /** UTC timestamp when the event occurred */
46
77
  utc_time: string;
78
+ /** Local timezone offset in minutes */
47
79
  local_tz_offset: number;
48
- screen_resolution: string;
49
- vp_size: string;
50
- userAgent: string;
51
- user_language: string;
52
- doc_encoding: string;
53
- doc_path: string;
54
- doc_host: string;
55
- doc_protocol: string;
80
+ /** Screen resolution as a string (e.g., "1920x1080") */
81
+ screen_resolution: string | undefined;
82
+ /** Viewport size as a string (e.g., "1200x800") */
83
+ vp_size: string | undefined;
84
+ /** User's preferred language */
85
+ user_language: string | undefined;
86
+ /** Document encoding */
87
+ doc_encoding: string | undefined;
88
+ /** Document path */
89
+ doc_path: string | undefined;
90
+ /** Document host */
91
+ doc_host: string | undefined;
92
+ /** Document protocol (http/https) */
93
+ doc_protocol: string | undefined;
94
+ /** Document hash fragment */
56
95
  doc_hash: string;
96
+ /** Document search parameters */
57
97
  doc_search: string;
58
- referrer: string;
59
- page_title: string;
60
- utm: UTMParams;
98
+ /** Referrer URL */
99
+ referrer: string | undefined;
100
+ /** Page title */
101
+ page_title: string | undefined;
102
+ /** Current page URL */
103
+ url: string | undefined;
104
+ /** UTM parameters for campaign tracking */
105
+ utm: Record<string, string>;
61
106
  }
62
- export interface PageViewEvent extends BaseEventData, BrowserEventData {
63
- event_type: typeof ANALYTICS_PAGEVIEW_EVENT;
107
+ /**
108
+ * The payload structure for DotCMS analytics events.
109
+ * This interface represents the complete data structure that flows through
110
+ * the analytics pipeline, including original event data and enriched context.
111
+ */
112
+ export interface DotCMSAnalyticsPayload {
113
+ /** The type of analytics event */
114
+ type: string;
115
+ /** Additional properties associated with the event */
116
+ properties: Record<string, unknown>;
117
+ /** The event name or identifier */
118
+ event: string;
119
+ /** Configuration options for the event */
120
+ options: Record<string, unknown>;
121
+ /** Analytics context shared across events */
122
+ context: DotCMSAnalyticsContext;
123
+ /** Page data for the current page */
124
+ page: DotCMSPageData;
125
+ /** Device and browser information */
126
+ device: DotCMSDeviceData;
127
+ /** UTM parameters for campaign tracking */
128
+ utm?: DotCMSUtmData;
129
+ /** Local timestamp when the event occurred */
130
+ local_time: string;
64
131
  }
65
- export interface TrackEvent {
66
- event_type: 'track';
67
- custom_event: string;
68
- [key: string]: unknown;
132
+ /**
133
+ * Parameters passed to DotCMS Analytics plugin methods.
134
+ * Contains the configuration and payload data needed for processing analytics events.
135
+ */
136
+ export interface DotCMSAnalyticsParams {
137
+ /** Configuration for the analytics client */
138
+ config: DotCMSAnalyticsConfig;
139
+ /** The event payload to be processed */
140
+ payload: DotCMSAnalyticsPayload;
69
141
  }
70
- export interface TrackPayload extends DotAnalyticsPayload {
71
- type: EventType.Track;
72
- properties: Record<string, unknown> & {
73
- title: string;
74
- url: string;
75
- path: string;
76
- hash: string;
77
- search: string;
78
- width: number;
79
- height: number;
80
- source: string;
81
- };
142
+ /**
143
+ * Main interface for the DotCMS Analytics SDK.
144
+ * Provides the core methods for tracking page views and custom events.
145
+ */
146
+ export interface DotCMSAnalytics {
147
+ /**
148
+ * Track a page view event.
149
+ * @param payload - Optional additional data to include with the page view
150
+ */
151
+ pageView: (payload?: Record<string, unknown>) => void;
152
+ /**
153
+ * Track a custom event.
154
+ * @param eventName - The name/type of the event to track
155
+ * @param payload - Optional additional data to include with the event
156
+ */
157
+ track: (eventName: string, payload?: Record<string, unknown>) => void;
82
158
  }
83
- export interface PageViewPayload extends DotAnalyticsPayload {
84
- type: EventType.PageView;
85
- properties: {
86
- title: string;
87
- url: string;
88
- path: string;
89
- hash: string;
90
- search: string;
91
- width: number;
92
- height: number;
93
- source: string;
94
- };
159
+ /**
160
+ * Analytics context shared across all events in DotCMS.
161
+ * Contains session and user identification data that provides
162
+ * continuity across multiple analytics events.
163
+ */
164
+ export interface DotCMSAnalyticsContext {
165
+ /** The site key for the DotCMS instance */
166
+ site_key: string;
167
+ /** Unique session identifier */
168
+ session_id: string;
169
+ /** Unique user identifier */
170
+ user_id: string;
171
+ }
172
+ /**
173
+ * Device and browser information for DotCMS analytics tracking.
174
+ * Contains technical details about the user's device and browser environment.
175
+ */
176
+ export interface DotCMSDeviceData {
177
+ /** Screen resolution as a string (e.g., "1920x1080") */
178
+ screen_resolution: string | undefined;
179
+ /** User's preferred language */
180
+ language: string | undefined;
181
+ /** Viewport width in pixels */
182
+ viewport_width: string | undefined;
183
+ /** Viewport height in pixels */
184
+ viewport_height: string | undefined;
185
+ }
186
+ /**
187
+ * UTM (Urchin Tracking Module) parameters for DotCMS campaign tracking.
188
+ * Contains marketing campaign attribution data extracted from URL parameters.
189
+ */
190
+ export interface DotCMSUtmData {
191
+ /** The marketing medium (e.g., email, social, cpc) */
192
+ medium?: string;
193
+ /** The traffic source (e.g., google, newsletter) */
194
+ source?: string;
195
+ /** The campaign name */
196
+ campaign?: string;
197
+ /** The campaign term or keyword */
198
+ term?: string;
199
+ /** The campaign content or ad variation */
200
+ content?: string;
201
+ /** The campaign ID for tracking specific campaigns */
202
+ id?: string;
95
203
  }
96
204
  /**
97
- * The payload for a track event.
205
+ * Page data structure for DotCMS analytics.
206
+ * Contains comprehensive information about the current page and its context
207
+ * within the DotCMS environment.
98
208
  */
99
- export interface DotAnalyticsPayload {
209
+ export interface DotCMSPageData {
210
+ /** The current page URL */
211
+ url: string | undefined;
212
+ /** Document encoding */
213
+ doc_encoding: string | undefined;
214
+ /** Document hash fragment */
215
+ doc_hash: string;
216
+ /** Document protocol (http/https) */
217
+ doc_protocol: string | undefined;
218
+ /** Document search parameters */
219
+ doc_search: string;
220
+ /** DotCMS host domain */
221
+ dot_host: string | undefined;
222
+ /** DotCMS page path */
223
+ dot_path: string | undefined;
224
+ /** Page title */
225
+ title: string | undefined;
226
+ /** User agent string */
227
+ user_agent?: string;
228
+ /** Language identifier */
229
+ language_id?: string;
230
+ /** Persona identifier */
231
+ persona?: string;
232
+ }
233
+ /**
234
+ * Analytics.js hook parameter types for DotCMS.
235
+ * Represents the payload structure used by Analytics.js lifecycle hooks
236
+ * for intercepting and modifying analytics events.
237
+ */
238
+ export interface DotCMSAnalyticsHookPayload {
239
+ /** The type of analytics event */
100
240
  type: string;
241
+ /** Properties associated with the event */
101
242
  properties: {
243
+ /** Page title */
102
244
  title: string;
245
+ /** Page URL */
103
246
  url: string;
247
+ /** Page path */
104
248
  path: string;
249
+ /** URL hash fragment */
105
250
  hash: string;
251
+ /** URL search parameters */
106
252
  search: string;
253
+ /** Viewport width */
107
254
  width: number;
255
+ /** Viewport height */
108
256
  height: number;
109
- source: string;
257
+ /** Referrer URL */
258
+ referrer?: string;
110
259
  };
111
- event: string;
260
+ /** Configuration options for the event */
112
261
  options: Record<string, unknown>;
262
+ /** User identifier */
113
263
  userId: string | null;
114
- anonymousId: string | null;
115
- }
116
- /**
117
- * Add ServerEvent type for HTTP layer
118
- */
119
- export interface ServerEvent extends Record<string, unknown> {
120
- timestamp: string;
121
- key: string;
122
- }
123
- /**
124
- * Interface for the AnalyticsTracker.
125
- */
126
- export interface DotContentAnalyticsCustomHook {
127
- track: (eventName: string, payload?: Record<string, unknown>) => void;
264
+ /** Anonymous user identifier */
265
+ anonymousId: string;
266
+ /** Metadata about the event */
267
+ meta: {
268
+ /** Request identifier */
269
+ rid: string;
270
+ /** Timestamp */
271
+ ts: number;
272
+ /** Whether the event has a callback function */
273
+ hasCallback: boolean;
274
+ };
128
275
  }
129
276
  /**
130
- * Params for the DotAnalytics plugin
277
+ * Analytics.js instance structure for DotCMS.
278
+ * Represents the internal structure of an Analytics.js instance,
279
+ * providing access to plugins, storage, and event configuration.
131
280
  */
132
- export interface DotAnalyticsParams {
133
- config: DotContentAnalyticsConfig;
134
- payload: DotAnalyticsPayload;
281
+ export interface DotCMSAnalyticsInstance {
282
+ /** Available plugins and their configurations */
283
+ plugins: Record<string, unknown>;
284
+ /** Storage mechanisms for analytics data */
285
+ storage: Record<string, unknown>;
286
+ /** Event configuration */
287
+ events: {
288
+ /** Core event types */
289
+ core: string[];
290
+ /** Plugin-specific event types */
291
+ plugins: string[];
292
+ };
135
293
  }
136
294
  /**
137
- * Shared interface for the DotAnalytics plugin
295
+ * Parameters passed to Analytics.js hook functions in DotCMS.
296
+ * Contains all the context and data needed for Analytics.js lifecycle hooks
297
+ * to process and modify analytics events.
138
298
  */
139
- export interface DotAnalytics {
140
- pageView: (payload?: Record<string, unknown>) => void;
141
- track: (eventName: string, payload?: Record<string, unknown>) => void;
299
+ export interface DotCMSAnalyticsHookParams {
300
+ /** The event payload data */
301
+ payload: DotCMSAnalyticsHookPayload;
302
+ /** The analytics instance */
303
+ instance: DotCMSAnalyticsInstance;
304
+ /** Global configuration settings */
305
+ config: Record<string, unknown>;
306
+ /** Available plugins and their status */
307
+ plugins: Record<string, {
308
+ /** Whether the plugin is enabled */
309
+ enabled: boolean;
310
+ /** Whether the plugin is initialized */
311
+ initialized: boolean;
312
+ /** Whether the plugin is loaded */
313
+ loaded: boolean;
314
+ /** Plugin-specific configuration */
315
+ config: Record<string, unknown>;
316
+ }>;
142
317
  }
143
- export {};
@@ -1,48 +1,111 @@
1
- import { BrowserEventData, DotContentAnalyticsConfig } from './dot-content-analytics.model';
1
+ import { DotCMSAnalyticsConfig, DotCMSAnalyticsContext, DotCMSAnalyticsPayload, DotCMSBrowserEventData, DotCMSDeviceData, DotCMSUtmData } from './dot-content-analytics.model';
2
+ import { PageData } from 'analytics';
2
3
 
4
+ export { cleanupActivityTracking, getLastActivity, getSessionInfo, initializeActivityTracking, isUserInactive, updateSessionActivity } from './dot-content-analytics.activity-tracker';
3
5
  /**
4
- * Retrieves analytics attributes from a given script element.
5
- *
6
- * @return {DotAnalyticsConfig | null} - The analytics attributes or null if there are no valid attributes present.
6
+ * Generates a cryptographically secure random ID
7
+ */
8
+ export declare const generateSecureId: (prefix: string) => string;
9
+ /**
10
+ * Safe sessionStorage wrapper with error handling
7
11
  */
8
- export declare const getDataAnalyticsAttributes: (location: Location) => DotContentAnalyticsConfig;
12
+ export declare const safeSessionStorage: {
13
+ getItem: (key: string) => string | null;
14
+ setItem: (key: string, value: string) => void;
15
+ };
9
16
  /**
10
- * Retrieves the analytics script tag from the document.
17
+ * Gets or generates a user ID
18
+ */
19
+ export declare const getUserId: () => string;
20
+ /**
21
+ * Gets session ID with comprehensive lifecycle management
22
+ * Returns existing valid session ID or creates a new one if needed
11
23
  *
12
- * @returns {HTMLScriptElement} - The analytics script tag.
24
+ * Session validation criteria:
25
+ * 1. User is still active (< 30 min inactivity)
26
+ * 2. Session hasn't passed midnight (UTC)
27
+ * 3. UTM parameters haven't changed
28
+ */
29
+ export declare const getSessionId: () => string;
30
+ /**
31
+ * Gets analytics context with user and session identification
32
+ */
33
+ export declare const getAnalyticsContext: (config: DotCMSAnalyticsConfig) => DotCMSAnalyticsContext;
34
+ /**
35
+ * Retrieves analytics attributes from a given script element.
36
+ */
37
+ export declare const getDataAnalyticsAttributes: () => DotCMSAnalyticsConfig;
38
+ /**
39
+ * Gets the analytics script tag from the DOM
13
40
  */
14
41
  export declare const getAnalyticsScriptTag: () => HTMLScriptElement;
15
42
  /**
16
- * Retrieves the browser event data.
17
- *
18
- * @param {Location} location - The location object.
19
- * @returns {BrowserEventData} - The browser event data.
43
+ * Retrieves the browser event data - optimized but accurate.
20
44
  */
21
- export declare const getBrowserEventData: (location: Location) => BrowserEventData;
45
+ export declare const getBrowserEventData: (location: Location) => DotCMSBrowserEventData;
22
46
  /**
23
- * Extracts UTM parameters from a given URL location.
24
- *
25
- * @param {Location} location - The location object containing the URL.
26
- * @returns {Record<string, string>} - An object containing the extracted UTM parameters.
47
+ * Extracts UTM parameters from the URL - cached for performance
27
48
  */
28
49
  export declare const extractUTMParameters: (location: Location) => Record<string, string>;
29
50
  /**
30
- * A function to redirect the user to a new URL.
31
- *
32
- * @param {string} href - The URL to redirect to.
33
- * @returns {void}
51
+ * Default redirect function
34
52
  */
35
53
  export declare const defaultRedirectFn: (href: string) => string;
36
54
  /**
37
- * Checks if the current environment is inside the dotCMS editor.
38
- *
39
- * @returns {boolean} - True if inside the editor, false otherwise.
55
+ * Check if we're inside the DotCMS editor
40
56
  */
41
57
  export declare const isInsideEditor: () => boolean;
42
58
  /**
43
- * Creates an analytics instance.
44
- *
45
- * @param {DotContentAnalyticsConfig} config - The configuration object for the analytics instance.
46
- * @returns {Analytics | null} - The analytics instance or null if there is an error.
59
+ * Gets local time in ISO format
60
+ */
61
+ export declare const getLocalTime: () => string;
62
+ /**
63
+ * Gets page data from browser event data and payload
64
+ */
65
+ export declare const getPageData: (browserData: DotCMSBrowserEventData, payload: DotCMSAnalyticsPayload) => PageData;
66
+ /**
67
+ * Gets device data from browser event data
68
+ */
69
+ export declare const getDeviceData: (browserData: DotCMSBrowserEventData) => DotCMSDeviceData;
70
+ /**
71
+ * Gets UTM data from browser event data
72
+ */
73
+ export declare const getUtmData: (browserData: DotCMSBrowserEventData) => DotCMSUtmData;
74
+ /**
75
+ * Enriches payload with UTM data
76
+ */
77
+ export declare const enrichWithUtmData: (payload: DotCMSAnalyticsPayload) => DotCMSAnalyticsPayload;
78
+ /**
79
+ * Optimized payload enrichment using existing analytics.js data
80
+ * Reuses payload.properties data instead of recalculating from DOM when available
81
+ * Maintains the same output structure as the original function
82
+ */
83
+ export declare const enrichPagePayloadOptimized: (payload: DotCMSAnalyticsPayload, location?: Location) => {
84
+ local_time: string;
85
+ utm?: DotCMSUtmData | undefined;
86
+ page: PageData;
87
+ device: DotCMSDeviceData;
88
+ type: string;
89
+ properties: Record<string, unknown>;
90
+ event: string;
91
+ options: Record<string, unknown>;
92
+ context: DotCMSAnalyticsContext;
93
+ };
94
+ /**
95
+ * @deprecated Use enrichPagePayloadOptimized instead to avoid data duplication
96
+ * Legacy function that enriches page payload with all data in one call
97
+ * This function duplicates data already available in analytics.js payload
47
98
  */
48
- export declare const createAnalyticsInstance: (config: DotContentAnalyticsConfig) => import('analytics').AnalyticsInstance | null;
99
+ export declare const enrichPagePayload: (payload: DotCMSAnalyticsPayload, location?: Location) => {
100
+ payload: {
101
+ local_time: string;
102
+ utm?: DotCMSUtmData | undefined;
103
+ page: PageData;
104
+ device: DotCMSDeviceData;
105
+ type: string;
106
+ properties: Record<string, unknown>;
107
+ event: string;
108
+ options: Record<string, unknown>;
109
+ context: DotCMSAnalyticsContext;
110
+ };
111
+ };