@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 +12 -6
- package/lib/core/plugin/enricher/dot-analytics.enricher.plugin.d.ts +2 -2
- package/lib/core/plugin/enricher/dot-analytics.enricher.plugin.js +14 -15
- package/lib/core/shared/dot-content-analytics.utils.d.ts +9 -1
- package/lib/core/shared/dot-content-analytics.utils.js +55 -51
- package/lib/core/shared/models/data.model.d.ts +2 -0
- package/lib/core/shared/models/event.model.d.ts +2 -4
- package/lib/core/shared/models/library.model.d.ts +2 -4
- package/package.json +1 -1
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/
|
|
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
|
|
2
|
-
import { getLocalTime as
|
|
3
|
-
const
|
|
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:
|
|
14
|
-
if (!t
|
|
15
|
-
throw new Error("DotCMS Analytics: Missing required page
|
|
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:
|
|
17
|
+
context: a,
|
|
18
18
|
events: [
|
|
19
19
|
{
|
|
20
|
-
event_type:
|
|
21
|
-
local_time:
|
|
20
|
+
event_type: i.PAGEVIEW,
|
|
21
|
+
local_time: r,
|
|
22
22
|
data: {
|
|
23
23
|
page: t,
|
|
24
|
-
|
|
25
|
-
...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:
|
|
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:
|
|
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
|
-
|
|
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,
|
|
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
|
|
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(),
|
|
5
|
-
return `${t}_${e}_${
|
|
6
|
-
},
|
|
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 =
|
|
23
|
-
return t || (t = d("user"),
|
|
24
|
-
},
|
|
25
|
-
const e = new Date(t),
|
|
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(
|
|
30
|
-
return
|
|
31
|
-
},
|
|
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(
|
|
36
|
+
const e = sessionStorage.getItem(h);
|
|
37
37
|
if (e) {
|
|
38
|
-
const { sessionId: o, startTime: r, lastActivity: a } = JSON.parse(e), c = !
|
|
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
|
-
|
|
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
|
|
50
|
-
sessionId:
|
|
49
|
+
const n = d("session"), s = {
|
|
50
|
+
sessionId: n,
|
|
51
51
|
startTime: t,
|
|
52
52
|
lastActivity: t
|
|
53
53
|
};
|
|
54
|
-
return sessionStorage.setItem(
|
|
54
|
+
return sessionStorage.setItem(h, JSON.stringify(s)), n;
|
|
55
55
|
} catch {
|
|
56
56
|
return d("session_fallback");
|
|
57
57
|
}
|
|
58
|
-
},
|
|
59
|
-
const e =
|
|
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:
|
|
62
|
+
userId: n
|
|
63
63
|
}), {
|
|
64
64
|
site_auth: t.siteAuth,
|
|
65
65
|
session_id: e,
|
|
66
|
-
user_id:
|
|
66
|
+
user_id: n,
|
|
67
|
+
device: s
|
|
67
68
|
};
|
|
68
|
-
},
|
|
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),
|
|
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
|
|
77
|
-
return
|
|
78
|
-
const r =
|
|
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
|
-
|
|
90
|
+
s[a] = r;
|
|
82
91
|
}
|
|
83
|
-
}), u = { search: e, params:
|
|
92
|
+
}), u = { search: e, params: s }, s;
|
|
84
93
|
}, E = () => {
|
|
85
94
|
try {
|
|
86
|
-
const t = (/* @__PURE__ */ new Date()).getTimezoneOffset(), e = t > 0 ? "-" : "+",
|
|
87
|
-
return `${e}${
|
|
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
|
-
},
|
|
100
|
+
}, C = () => {
|
|
92
101
|
try {
|
|
93
|
-
const t = /* @__PURE__ */ new Date(), e = E(),
|
|
94
|
-
return `${
|
|
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
|
-
},
|
|
99
|
-
const
|
|
100
|
-
Object.keys(o).forEach((
|
|
101
|
-
|
|
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:
|
|
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
|
-
|
|
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:
|
|
128
|
+
local_time: n
|
|
126
129
|
};
|
|
127
130
|
};
|
|
128
131
|
export {
|
|
129
|
-
|
|
132
|
+
$ as enrichPagePayloadOptimized,
|
|
130
133
|
y as extractUTMParameters,
|
|
131
134
|
d as generateSecureId,
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
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 {
|
|
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
|
|
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,
|
|
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,
|
|
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) */
|