@dotcms/analytics 1.1.1-next.3 → 1.1.1-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.
package/README.md CHANGED
@@ -262,6 +262,12 @@ When you call `pageView(customData?)`, the SDK **automatically enriches** the ev
262
262
  site_key: string; // Your site key
263
263
  session_id: string; // Current session ID
264
264
  user_id: string; // Anonymous user ID
265
+ device: { // 🤖 AUTOMATIC - Device & Browser Info
266
+ screen_resolution: string; // Screen size
267
+ language: string; // Browser language
268
+ viewport_width: string; // Viewport width
269
+ viewport_height: string; // Viewport height
270
+ }
265
271
  },
266
272
  events: [{
267
273
  event_type: "pageview",
@@ -278,12 +284,6 @@ When you call `pageView(customData?)`, the SDK **automatically enriches** the ev
278
284
  doc_hash: string; // URL hash
279
285
  doc_encoding: string; // Character encoding
280
286
  },
281
- device: { // 🤖 AUTOMATIC - Device & Browser Info
282
- screen_resolution: string; // Screen size
283
- language: string; // Browser language
284
- viewport_width: string; // Viewport width
285
- viewport_height: string; // Viewport height
286
- },
287
287
  utm?: { // 🤖 AUTOMATIC - Campaign Tracking (if present in URL)
288
288
  source: string; // utm_source
289
289
  medium: string; // utm_medium
@@ -319,6 +319,12 @@ When you call `track(eventName, properties)`, the following structure is sent:
319
319
  site_key: string; // Your site key
320
320
  session_id: string; // Current session ID
321
321
  user_id: string; // Anonymous user ID
322
+ device: { // 🤖 AUTOMATIC - Device & Browser Info
323
+ screen_resolution: string; // Screen size
324
+ language: string; // Browser language
325
+ viewport_width: string; // Viewport width
326
+ viewport_height: string; // Viewport height
327
+ }
322
328
  },
323
329
  events: [{
324
330
  event_type: string, // Your custom event name
@@ -2,8 +2,8 @@ import { AnalyticsBasePayloadWithContext, AnalyticsTrackPayloadWithContext, DotC
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_auth, 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
8
  * Returns the final request body structure ready to send to the server.
9
9
  */
@@ -1,6 +1,6 @@
1
- import { DotCMSPredefinedEventType as c } from "../../shared/constants/dot-content-analytics.constants.js";
2
- import { getLocalTime as s, enrichPagePayloadOptimized as d } from "../../shared/dot-content-analytics.utils.js";
3
- const p = () => ({
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
@@ -10,20 +10,19 @@ const p = () => ({
10
10
  "page:dot-analytics": ({
11
11
  payload: o
12
12
  }) => {
13
- const { context: r, page: t, device: e, utm: n, custom: a, local_time: i } = d(o);
14
- if (!t || !e)
15
- throw new Error("DotCMS Analytics: Missing required page or device data");
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
16
  return {
17
- context: r,
17
+ context: a,
18
18
  events: [
19
19
  {
20
- event_type: c.PAGEVIEW,
21
- local_time: i,
20
+ event_type: i.PAGEVIEW,
21
+ local_time: r,
22
22
  data: {
23
23
  page: t,
24
- device: e,
25
- ...n && { utm: n },
26
- ...a && { custom: a }
24
+ ...e && { utm: e },
25
+ ...n && { custom: n }
27
26
  }
28
27
  }
29
28
  ]
@@ -37,12 +36,12 @@ const p = () => ({
37
36
  "track:dot-analytics": ({
38
37
  payload: o
39
38
  }) => {
40
- const { event: r, properties: t, context: e } = o, n = s();
39
+ const { event: a, properties: t, context: e } = o, n = c();
41
40
  return {
42
41
  context: e,
43
42
  events: [
44
43
  {
45
- event_type: r,
44
+ event_type: a,
46
45
  local_time: n,
47
46
  data: {
48
47
  custom: t
@@ -53,5 +52,5 @@ const p = () => ({
53
52
  }
54
53
  });
55
54
  export {
56
- p as dotAnalyticsEnricherPlugin
55
+ m as dotAnalyticsEnricherPlugin
57
56
  };
@@ -60,6 +60,14 @@ export interface AnalyticsConfigResult {
60
60
  * @returns The analytics configuration object
61
61
  */
62
62
  export declare const getAnalyticsConfig: () => DotCMSAnalyticsConfig;
63
+ /**
64
+ * Gets current device data for analytics.
65
+ * Combines static browser data with dynamic viewport information.
66
+ * Used by the identity plugin to inject device data into context.
67
+ *
68
+ * @returns Device data with screen resolution, language, and viewport dimensions
69
+ */
70
+ export declare const getDeviceDataForContext: () => DotCMSEventDeviceData;
63
71
  /**
64
72
  * Retrieves the browser event data - optimized but accurate.
65
73
  * @internal This function is for internal use only.
@@ -126,7 +134,7 @@ export declare const enrichWithUtmData: <T extends Record<string, unknown>>(payl
126
134
  *
127
135
  * @param payload - The Analytics.js payload with context already injected by identity plugin
128
136
  * @param location - The Location object to extract page data from (defaults to window.location)
129
- * @returns Enriched payload with page, device, UTM, custom data, and local_time
137
+ * @returns Enriched payload with page, UTM, custom data, and local_time (device is in context)
130
138
  */
131
139
  export declare const enrichPagePayloadOptimized: (payload: AnalyticsBasePayloadWithContext, location?: Location) => EnrichedAnalyticsPayload;
132
140
  /**
@@ -1,9 +1,9 @@
1
- import { ANALYTICS_JS_DEFAULT_PROPERTIES as _, SESSION_STORAGE_KEY as S, DEFAULT_SESSION_TIMEOUT_MINUTES as f, USER_ID_KEY as l, EXPECTED_UTM_KEYS as w } from "./constants/dot-content-analytics.constants.js";
1
+ import { ANALYTICS_JS_DEFAULT_PROPERTIES as w, SESSION_STORAGE_KEY as h, DEFAULT_SESSION_TIMEOUT_MINUTES as _, USER_ID_KEY as l, EXPECTED_UTM_KEYS as f } from "./constants/dot-content-analytics.constants.js";
2
2
  let g = null, u = null;
3
3
  const d = (t) => {
4
- const e = Date.now(), s = Math.random().toString(36).substr(2, 9), n = Math.random().toString(36).substr(2, 9);
5
- return `${t}_${e}_${s}${n}`;
6
- }, m = {
4
+ const e = Date.now(), n = Math.random().toString(36).substr(2, 9), s = Math.random().toString(36).substr(2, 9);
5
+ return `${t}_${e}_${n}${s}`;
6
+ }, S = {
7
7
  getItem: (t) => {
8
8
  try {
9
9
  return localStorage.getItem(t);
@@ -19,26 +19,26 @@ const d = (t) => {
19
19
  }
20
20
  }
21
21
  }, p = () => {
22
- let t = m.getItem(l);
23
- return t || (t = d("user"), m.setItem(l, t)), t;
24
- }, T = (t) => {
25
- const e = new Date(t), s = /* @__PURE__ */ new Date(), n = new Date(
22
+ let t = S.getItem(l);
23
+ return t || (t = d("user"), S.setItem(l, t)), t;
24
+ }, D = (t) => {
25
+ const e = new Date(t), n = /* @__PURE__ */ new Date(), s = new Date(
26
26
  e.getUTCFullYear(),
27
27
  e.getUTCMonth(),
28
28
  e.getUTCDate()
29
- ), o = new Date(s.getUTCFullYear(), s.getUTCMonth(), s.getUTCDate());
30
- return n.getTime() !== o.getTime();
31
- }, D = () => {
29
+ ), o = new Date(n.getUTCFullYear(), n.getUTCMonth(), n.getUTCDate());
30
+ return s.getTime() !== o.getTime();
31
+ }, T = () => {
32
32
  const t = Date.now();
33
33
  if (typeof window > "u")
34
34
  return d("session_fallback");
35
35
  try {
36
- const e = sessionStorage.getItem(S);
36
+ const e = sessionStorage.getItem(h);
37
37
  if (e) {
38
- const { sessionId: o, startTime: r, lastActivity: a } = JSON.parse(e), c = !T(r), i = t - a < f * 60 * 1e3;
38
+ const { sessionId: o, startTime: r, lastActivity: a } = JSON.parse(e), c = !D(r), i = t - a < _ * 60 * 1e3;
39
39
  if (c && i)
40
40
  return sessionStorage.setItem(
41
- S,
41
+ h,
42
42
  JSON.stringify({
43
43
  sessionId: o,
44
44
  startTime: r,
@@ -46,91 +46,95 @@ const d = (t) => {
46
46
  })
47
47
  ), o;
48
48
  }
49
- const s = d("session"), n = {
50
- sessionId: s,
49
+ const n = d("session"), s = {
50
+ sessionId: n,
51
51
  startTime: t,
52
52
  lastActivity: t
53
53
  };
54
- return sessionStorage.setItem(S, JSON.stringify(n)), s;
54
+ return sessionStorage.setItem(h, JSON.stringify(s)), n;
55
55
  } catch {
56
56
  return d("session_fallback");
57
57
  }
58
- }, $ = (t) => {
59
- const e = D(), s = p();
58
+ }, U = (t) => {
59
+ const e = T(), n = p(), s = I();
60
60
  return t.debug && console.warn("DotCMS Analytics Identity Context:", {
61
61
  sessionId: e,
62
- userId: s
62
+ userId: n
63
63
  }), {
64
64
  site_auth: t.siteAuth,
65
65
  session_id: e,
66
- user_id: s
66
+ user_id: n,
67
+ device: s
67
68
  };
68
- }, I = () => g || (g = {
69
+ }, m = () => g || (g = {
69
70
  user_language: navigator.language,
70
71
  doc_encoding: document.characterSet || document.charset,
71
72
  screen_resolution: typeof screen < "u" && screen.width && screen.height ? `${screen.width}x${screen.height}` : ""
72
- }, g), y = (t) => {
73
+ }, g), I = () => {
74
+ const t = m(), e = window.innerWidth || document.documentElement.clientWidth || 0, n = window.innerHeight || document.documentElement.clientHeight || 0;
75
+ return {
76
+ screen_resolution: t.screen_resolution ?? "",
77
+ language: t.user_language ?? "",
78
+ viewport_width: String(e),
79
+ viewport_height: String(n)
80
+ };
81
+ }, y = (t) => {
73
82
  const e = t.search;
74
83
  if (u && u.search === e)
75
84
  return u.params;
76
- const s = new URLSearchParams(e), n = {};
77
- return w.forEach((o) => {
78
- const r = s.get(o);
85
+ const n = new URLSearchParams(e), s = {};
86
+ return f.forEach((o) => {
87
+ const r = n.get(o);
79
88
  if (r) {
80
89
  const a = o.replace("utm_", "");
81
- n[a] = r;
90
+ s[a] = r;
82
91
  }
83
- }), u = { search: e, params: n }, n;
92
+ }), u = { search: e, params: s }, s;
84
93
  }, E = () => {
85
94
  try {
86
- const t = (/* @__PURE__ */ new Date()).getTimezoneOffset(), e = t > 0 ? "-" : "+", s = Math.abs(t), n = Math.floor(s / 60), o = s % 60;
87
- return `${e}${n.toString().padStart(2, "0")}:${o.toString().padStart(2, "0")}`;
95
+ const t = (/* @__PURE__ */ new Date()).getTimezoneOffset(), e = t > 0 ? "-" : "+", n = Math.abs(t), s = Math.floor(n / 60), o = n % 60;
96
+ return `${e}${s.toString().padStart(2, "0")}:${o.toString().padStart(2, "0")}`;
88
97
  } catch {
89
98
  return "+00:00";
90
99
  }
91
- }, O = () => {
100
+ }, C = () => {
92
101
  try {
93
- const t = /* @__PURE__ */ new Date(), e = E(), s = t.getFullYear(), n = (t.getMonth() + 1).toString().padStart(2, "0"), o = t.getDate().toString().padStart(2, "0"), r = t.getHours().toString().padStart(2, "0"), a = t.getMinutes().toString().padStart(2, "0"), c = t.getSeconds().toString().padStart(2, "0");
94
- return `${s}-${n}-${o}T${r}:${a}:${c}${e}`;
102
+ const t = /* @__PURE__ */ new Date(), e = E(), n = t.getFullYear(), s = (t.getMonth() + 1).toString().padStart(2, "0"), o = t.getDate().toString().padStart(2, "0"), r = t.getHours().toString().padStart(2, "0"), a = t.getMinutes().toString().padStart(2, "0"), c = t.getSeconds().toString().padStart(2, "0");
103
+ return `${n}-${s}-${o}T${r}:${a}:${c}${e}`;
95
104
  } catch {
96
105
  return (/* @__PURE__ */ new Date()).toISOString();
97
106
  }
98
- }, C = (t, e = typeof window < "u" ? window.location : {}) => {
99
- const s = O(), n = I(), { properties: o } = t, r = {};
100
- Object.keys(o).forEach((h) => {
101
- _.includes(h) || (r[h] = o[h]);
107
+ }, $ = (t, e = typeof window < "u" ? window.location : {}) => {
108
+ const n = C(), s = m(), { properties: o } = t, r = {};
109
+ Object.keys(o).forEach((i) => {
110
+ w.includes(i) || (r[i] = o[i]);
102
111
  });
103
112
  const a = {
104
113
  url: e.href,
105
- doc_encoding: n.doc_encoding,
114
+ doc_encoding: s.doc_encoding,
106
115
  doc_hash: e.hash,
107
116
  doc_protocol: e.protocol,
108
117
  doc_search: e.search,
109
118
  doc_host: e.hostname,
110
119
  doc_path: e.pathname,
111
120
  title: o.title ?? (document == null ? void 0 : document.title)
112
- }, c = {
113
- screen_resolution: n.screen_resolution ?? "",
114
- language: n.user_language ?? "",
115
- viewport_width: String(o.width),
116
- viewport_height: String(o.height)
117
- }, i = y(e);
121
+ }, c = y(e);
118
122
  return {
119
123
  ...t,
120
124
  page: a,
121
- device: c,
122
- ...Object.keys(i).length > 0 && { utm: i },
125
+ ...Object.keys(c).length > 0 && { utm: c },
123
126
  // Only include custom if there are user-provided properties
124
127
  ...Object.keys(r).length > 0 && { custom: r },
125
- local_time: s
128
+ local_time: n
126
129
  };
127
130
  };
128
131
  export {
129
- C as enrichPagePayloadOptimized,
132
+ $ as enrichPagePayloadOptimized,
130
133
  y as extractUTMParameters,
131
134
  d as generateSecureId,
132
- $ as getAnalyticsContext,
133
- O as getLocalTime,
134
- D as getSessionId,
135
+ U as getAnalyticsContext,
136
+ I as getDeviceDataForContext,
137
+ C as getLocalTime,
138
+ T as getSessionId,
135
139
  p as getUserId
136
140
  };
@@ -53,6 +53,8 @@ export interface DotCMSAnalyticsEventContext {
53
53
  session_id: string;
54
54
  /** Unique user identifier */
55
55
  user_id: string;
56
+ /** Device and browser information */
57
+ device: DotCMSEventDeviceData;
56
58
  }
57
59
  /**
58
60
  * Device and browser information for DotCMS analytics tracking.
@@ -1,4 +1,4 @@
1
- import { DotCMSEventDeviceData, DotCMSEventPageData, DotCMSEventUtmData } from './data.model';
1
+ import { DotCMSEventPageData, DotCMSEventUtmData } from './data.model';
2
2
  import { DotCMSCustomEventType, DotCMSEventType, DotCMSPredefinedEventType } from '../constants/dot-content-analytics.constants';
3
3
  /**
4
4
  * JSON value type for analytics custom data.
@@ -32,13 +32,11 @@ export interface DotCMSEventBase<TEventType extends DotCMSEventType, TData> {
32
32
  }
33
33
  /**
34
34
  * Data structure for pageview events.
35
- * Contains page, device, and optional UTM/custom data.
35
+ * Contains page and optional UTM/custom data.
36
36
  */
37
37
  export type DotCMSPageViewEventData = {
38
38
  /** Page data associated with the event */
39
39
  page: DotCMSEventPageData;
40
- /** Device and browser information */
41
- device: DotCMSEventDeviceData;
42
40
  /** UTM parameters for campaign tracking (optional) */
43
41
  utm?: DotCMSEventUtmData;
44
42
  /** Custom data associated with the event (any valid JSON) */
@@ -1,4 +1,4 @@
1
- import { DotCMSAnalyticsEventContext, DotCMSEventDeviceData, DotCMSEventPageData, DotCMSEventUtmData } from './data.model';
1
+ import { DotCMSAnalyticsEventContext, DotCMSEventPageData, DotCMSEventUtmData } from './data.model';
2
2
  import { JsonObject } from './event.model';
3
3
  import { DotCMSAnalyticsRequestBody } from './request.model';
4
4
  import { DotCMSCustomEventType } from '../constants';
@@ -117,13 +117,11 @@ export interface AnalyticsBasePayloadWithContext extends AnalyticsBasePayload {
117
117
  /**
118
118
  * Enriched analytics payload with DotCMS-specific data.
119
119
  * This is the result of enriching the base Analytics.js payload with context (from identity plugin)
120
- * and then adding page, device, UTM, and custom data (from enricher plugin).
120
+ * and then adding page, UTM, and custom data (from enricher plugin).
121
121
  */
122
122
  export type EnrichedAnalyticsPayload = AnalyticsBasePayloadWithContext & {
123
123
  /** Page data for the current page */
124
124
  page: DotCMSEventPageData;
125
- /** Device and browser information */
126
- device: DotCMSEventDeviceData;
127
125
  /** UTM parameters for campaign tracking */
128
126
  utm?: DotCMSEventUtmData;
129
127
  /** Custom data associated with the event (any valid JSON) */
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dotcms/analytics",
3
- "version": "1.1.1-next.3",
3
+ "version": "1.1.1-next.4",
4
4
  "description": "Official JavaScript library for Content Analytics with DotCMS.",
5
5
  "repository": {
6
6
  "type": "git",