@dotcms/analytics 1.2.0 → 1.2.1

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 (50) hide show
  1. package/README.md +314 -9
  2. package/lib/core/dot-analytics.content.js +84 -0
  3. package/lib/core/plugin/click/dot-analytics.click-tracker.d.ts +108 -0
  4. package/lib/core/plugin/click/dot-analytics.click-tracker.js +144 -0
  5. package/lib/core/plugin/click/dot-analytics.click.plugin.d.ts +36 -0
  6. package/lib/core/plugin/click/dot-analytics.click.plugin.js +27 -0
  7. package/lib/core/plugin/click/dot-analytics.click.utils.d.ts +12 -0
  8. package/lib/core/plugin/click/dot-analytics.click.utils.js +55 -0
  9. package/lib/core/plugin/enricher/dot-analytics.enricher.plugin.d.ts +14 -10
  10. package/lib/core/plugin/enricher/dot-analytics.enricher.plugin.js +26 -38
  11. package/lib/core/{shared/dot-content-analytics.activity-tracker.d.ts → plugin/identity/dot-analytics.identity.activity-tracker.d.ts} +2 -17
  12. package/lib/core/{shared/dot-content-analytics.activity-tracker.js → plugin/identity/dot-analytics.identity.activity-tracker.js} +17 -37
  13. package/lib/core/plugin/identity/dot-analytics.identity.plugin.d.ts +2 -20
  14. package/lib/core/plugin/identity/dot-analytics.identity.plugin.js +7 -7
  15. package/lib/core/plugin/identity/dot-analytics.identity.utils.d.ts +0 -16
  16. package/lib/core/plugin/impression/dot-analytics.impression-tracker.d.ts +62 -0
  17. package/lib/core/plugin/impression/dot-analytics.impression-tracker.js +202 -0
  18. package/lib/core/plugin/impression/dot-analytics.impression.plugin.d.ts +40 -0
  19. package/lib/core/plugin/impression/dot-analytics.impression.plugin.js +27 -0
  20. package/lib/core/plugin/impression/dot-analytics.impression.utils.d.ts +26 -0
  21. package/lib/core/plugin/impression/dot-analytics.impression.utils.js +27 -0
  22. package/lib/core/plugin/impression/index.d.ts +2 -0
  23. package/lib/core/plugin/main/dot-analytics.plugin.d.ts +46 -0
  24. package/lib/core/plugin/main/dot-analytics.plugin.js +129 -0
  25. package/lib/core/shared/constants/{dot-content-analytics.constants.d.ts → dot-analytics.constants.d.ts} +62 -0
  26. package/lib/core/shared/constants/dot-analytics.constants.js +53 -0
  27. package/lib/core/shared/constants/index.d.ts +1 -1
  28. package/lib/core/shared/dot-analytics.logger.d.ts +85 -0
  29. package/lib/core/shared/dot-analytics.logger.js +90 -0
  30. package/lib/core/shared/http/dot-analytics.http.d.ts +9 -0
  31. package/lib/core/shared/http/dot-analytics.http.js +34 -0
  32. package/lib/core/shared/models/data.model.d.ts +39 -1
  33. package/lib/core/shared/models/event.model.d.ts +108 -3
  34. package/lib/core/shared/models/library.model.d.ts +89 -28
  35. package/lib/core/shared/models/request.model.d.ts +17 -9
  36. package/lib/core/shared/queue/dot-analytics.queue.utils.js +44 -37
  37. package/lib/core/shared/{dot-content-analytics.utils.d.ts → utils/dot-analytics.utils.d.ts} +91 -3
  38. package/lib/core/shared/utils/dot-analytics.utils.js +202 -0
  39. package/lib/react/hook/useContentAnalytics.js +17 -11
  40. package/lib/react/hook/useRouterTracker.js +4 -4
  41. package/lib/react/internal/utils.js +1 -1
  42. package/package.json +7 -6
  43. package/lib/core/dot-content-analytics.js +0 -46
  44. package/lib/core/plugin/dot-analytics.plugin.d.ts +0 -33
  45. package/lib/core/plugin/dot-analytics.plugin.js +0 -42
  46. package/lib/core/shared/constants/dot-content-analytics.constants.js +0 -34
  47. package/lib/core/shared/dot-content-analytics.http.d.ts +0 -17
  48. package/lib/core/shared/dot-content-analytics.http.js +0 -41
  49. package/lib/core/shared/dot-content-analytics.utils.js +0 -147
  50. /package/lib/core/{dot-content-analytics.d.ts → dot-analytics.content.d.ts} +0 -0
@@ -1,34 +0,0 @@
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
- };
@@ -1,17 +0,0 @@
1
- import { DotCMSAnalyticsConfig, DotCMSEvent, DotCMSRequestBody } from './models';
2
- /**
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
12
- * @param payload - The event payload data
13
- * @param config - The analytics configuration
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)
16
- */
17
- export declare const sendAnalyticsEvent: (payload: DotCMSRequestBody<DotCMSEvent>, config: DotCMSAnalyticsConfig, transportType?: TransportType) => Promise<void>;
@@ -1,41 +0,0 @@
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
- }
15
- try {
16
- const e = await fetch(a, {
17
- method: "POST",
18
- headers: { "Content-Type": "application/json" },
19
- body: c
20
- });
21
- if (!e.ok) {
22
- const i = e.statusText || "Unknown Error", r = `HTTP ${e.status}: ${i}`;
23
- try {
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`
27
- );
28
- } catch (t) {
29
- console.warn(
30
- `DotCMS Analytics: ${r} - Failed to parse error response:`,
31
- t
32
- );
33
- }
34
- }
35
- } catch (e) {
36
- console.error("DotCMS Analytics: Error sending event:", e);
37
- }
38
- };
39
- export {
40
- d as sendAnalyticsEvent
41
- };
@@ -1,147 +0,0 @@
1
- import { ANALYTICS_JS_DEFAULT_PROPERTIES as f, SESSION_STORAGE_KEY as h, DEFAULT_SESSION_TIMEOUT_MINUTES as w, USER_ID_KEY as l, EXPECTED_UTM_KEYS as _ } from "./constants/dot-content-analytics.constants.js";
2
- import "../../../uve/src/internal/constants.js";
3
- function U(t) {
4
- var n, s;
5
- const e = [];
6
- return (n = t.siteAuth) != null && n.trim() || e.push('"siteAuth"'), (s = t.server) != null && s.trim() || e.push('"server"'), e.length > 0 ? e : null;
7
- }
8
- let u = null, g = null;
9
- const d = (t) => {
10
- const e = Date.now(), n = Math.random().toString(36).substr(2, 9), s = Math.random().toString(36).substr(2, 9);
11
- return `${t}_${e}_${n}${s}`;
12
- }, m = {
13
- getItem: (t) => {
14
- try {
15
- return localStorage.getItem(t);
16
- } catch {
17
- return null;
18
- }
19
- },
20
- setItem: (t, e) => {
21
- try {
22
- localStorage.setItem(t, e);
23
- } catch {
24
- console.warn(`DotCMS Analytics: Could not save ${t} to localStorage`);
25
- }
26
- }
27
- }, p = () => {
28
- let t = m.getItem(l);
29
- return t || (t = d("user"), m.setItem(l, t)), t;
30
- }, D = (t) => {
31
- const e = new Date(t), n = /* @__PURE__ */ new Date(), s = new Date(
32
- e.getUTCFullYear(),
33
- e.getUTCMonth(),
34
- e.getUTCDate()
35
- ), o = new Date(n.getUTCFullYear(), n.getUTCMonth(), n.getUTCDate());
36
- return s.getTime() !== o.getTime();
37
- }, T = () => {
38
- const t = Date.now();
39
- if (typeof window > "u")
40
- return d("session_fallback");
41
- try {
42
- const e = sessionStorage.getItem(h);
43
- if (e) {
44
- const { sessionId: o, startTime: r, lastActivity: a } = JSON.parse(e), c = !D(r), i = t - a < w * 60 * 1e3;
45
- if (c && i)
46
- return sessionStorage.setItem(
47
- h,
48
- JSON.stringify({
49
- sessionId: o,
50
- startTime: r,
51
- lastActivity: t
52
- })
53
- ), o;
54
- }
55
- const n = d("session"), s = {
56
- sessionId: n,
57
- startTime: t,
58
- lastActivity: t
59
- };
60
- return sessionStorage.setItem(h, JSON.stringify(s)), n;
61
- } catch {
62
- return d("session_fallback");
63
- }
64
- }, $ = (t) => {
65
- const e = T(), n = p(), s = I();
66
- return t.debug && console.warn("DotCMS Analytics Identity Context:", {
67
- sessionId: e,
68
- userId: n
69
- }), {
70
- site_auth: t.siteAuth,
71
- session_id: e,
72
- user_id: n,
73
- device: s
74
- };
75
- }, S = () => u || (u = {
76
- user_language: navigator.language,
77
- doc_encoding: document.characterSet || document.charset,
78
- screen_resolution: typeof screen < "u" && screen.width && screen.height ? `${screen.width}x${screen.height}` : ""
79
- }, u), I = () => {
80
- const t = S(), e = window.innerWidth || document.documentElement.clientWidth || 0, n = window.innerHeight || document.documentElement.clientHeight || 0;
81
- return {
82
- screen_resolution: t.screen_resolution ?? "",
83
- language: t.user_language ?? "",
84
- viewport_width: String(e),
85
- viewport_height: String(n)
86
- };
87
- }, y = (t) => {
88
- const e = t.search;
89
- if (g && g.search === e)
90
- return g.params;
91
- const n = new URLSearchParams(e), s = {};
92
- return _.forEach((o) => {
93
- const r = n.get(o);
94
- if (r) {
95
- const a = o.replace("utm_", "");
96
- s[a] = r;
97
- }
98
- }), g = { search: e, params: s }, s;
99
- }, E = () => {
100
- try {
101
- const t = (/* @__PURE__ */ new Date()).getTimezoneOffset(), e = t > 0 ? "-" : "+", n = Math.abs(t), s = Math.floor(n / 60), o = n % 60;
102
- return `${e}${s.toString().padStart(2, "0")}:${o.toString().padStart(2, "0")}`;
103
- } catch {
104
- return "+00:00";
105
- }
106
- }, C = () => {
107
- try {
108
- 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");
109
- return `${n}-${s}-${o}T${r}:${a}:${c}${e}`;
110
- } catch {
111
- return (/* @__PURE__ */ new Date()).toISOString();
112
- }
113
- }, A = (t, e = typeof window < "u" ? window.location : {}) => {
114
- const n = C(), s = S(), { properties: o } = t, r = {};
115
- Object.keys(o).forEach((i) => {
116
- f.includes(i) || (r[i] = o[i]);
117
- });
118
- const a = {
119
- url: e.href,
120
- doc_encoding: s.doc_encoding,
121
- doc_hash: e.hash,
122
- doc_protocol: e.protocol,
123
- doc_search: e.search,
124
- doc_host: e.hostname,
125
- doc_path: e.pathname,
126
- title: o.title ?? (document == null ? void 0 : document.title)
127
- }, c = y(e);
128
- return {
129
- ...t,
130
- page: a,
131
- ...Object.keys(c).length > 0 && { utm: c },
132
- // Only include custom if there are user-provided properties
133
- ...Object.keys(r).length > 0 && { custom: r },
134
- local_time: n
135
- };
136
- };
137
- export {
138
- A as enrichPagePayloadOptimized,
139
- y as extractUTMParameters,
140
- d as generateSecureId,
141
- $ as getAnalyticsContext,
142
- I as getDeviceDataForContext,
143
- C as getLocalTime,
144
- T as getSessionId,
145
- p as getUserId,
146
- U as validateAnalyticsConfig
147
- };