@dotcms/analytics 0.0.1-alpha.54

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/index.d.ts +1 -0
  2. package/index.mjs +6 -0
  3. package/lib/dotAnalytics/dot-content-analytics.d.ts +9 -0
  4. package/lib/dotAnalytics/dot-content-analytics.mjs +24 -0
  5. package/lib/dotAnalytics/plugin/dot-analytics.enricher.plugin.d.ts +31 -0
  6. package/lib/dotAnalytics/plugin/dot-analytics.enricher.plugin.mjs +28 -0
  7. package/lib/dotAnalytics/plugin/dot-analytics.plugin.d.ts +31 -0
  8. package/lib/dotAnalytics/plugin/dot-analytics.plugin.mjs +55 -0
  9. package/lib/dotAnalytics/shared/dot-content-analytics.constants.d.ts +13 -0
  10. package/lib/dotAnalytics/shared/dot-content-analytics.constants.mjs +10 -0
  11. package/lib/dotAnalytics/shared/dot-content-analytics.http.d.ts +9 -0
  12. package/lib/dotAnalytics/shared/dot-content-analytics.http.mjs +22 -0
  13. package/lib/dotAnalytics/shared/dot-content-analytics.model.d.ts +143 -0
  14. package/lib/dotAnalytics/shared/dot-content-analytics.utils.d.ts +48 -0
  15. package/lib/dotAnalytics/shared/dot-content-analytics.utils.mjs +46 -0
  16. package/lib/react/components/DotContentAnalyticsProvider.d.ts +20 -0
  17. package/lib/react/components/DotContentAnalyticsProvider.mjs +15 -0
  18. package/lib/react/contexts/DotContentAnalyticsContext.d.ts +13 -0
  19. package/lib/react/contexts/DotContentAnalyticsContext.mjs +5 -0
  20. package/lib/react/hook/useContentAnalytics.d.ts +22 -0
  21. package/lib/react/hook/useContentAnalytics.mjs +34 -0
  22. package/lib/react/hook/useRouterTracker.d.ts +10 -0
  23. package/lib/react/hook/useRouterTracker.mjs +19 -0
  24. package/lib/react/index.d.ts +1 -0
  25. package/lib/react/public-api.d.ts +2 -0
  26. package/lib/standalone.d.ts +8 -0
  27. package/package.json +41 -0
  28. package/react/index.mjs +6 -0
package/index.d.ts ADDED
@@ -0,0 +1 @@
1
+ export * from './lib/react';
package/index.mjs ADDED
@@ -0,0 +1,6 @@
1
+ import { DotContentAnalyticsProvider as e } from "./lib/react/components/DotContentAnalyticsProvider.mjs";
2
+ import { useContentAnalytics as r } from "./lib/react/hook/useContentAnalytics.mjs";
3
+ export {
4
+ e as DotContentAnalyticsProvider,
5
+ r as useContentAnalytics
6
+ };
@@ -0,0 +1,9 @@
1
+ import { DotAnalytics, DotContentAnalyticsConfig } from './shared/dot-content-analytics.model';
2
+
3
+ /**
4
+ * Creates an analytics instance.
5
+ *
6
+ * @param {DotContentAnalyticsConfig} config - The configuration object for the analytics instance.
7
+ * @returns {DotAnalytics} - The analytics instance.
8
+ */
9
+ export declare const initializeContentAnalytics: (config: DotContentAnalyticsConfig) => DotAnalytics;
@@ -0,0 +1,24 @@
1
+ import { createAnalyticsInstance as o } from "./shared/dot-content-analytics.utils.mjs";
2
+ const i = (r) => {
3
+ const t = o(r);
4
+ return {
5
+ /**
6
+ * Track a page view.
7
+ * @param {Record<string, unknown>} payload - The payload to track.
8
+ */
9
+ pageView: (e = {}) => {
10
+ t == null || t.page(e);
11
+ },
12
+ /**
13
+ * Track a custom event.
14
+ * @param {string} eventName - The name of the event to track.
15
+ * @param {Record<string, unknown>} payload - The payload to track.
16
+ */
17
+ track: (e, n = {}) => {
18
+ t == null || t.track(e, n);
19
+ }
20
+ };
21
+ };
22
+ export {
23
+ i as initializeContentAnalytics
24
+ };
@@ -0,0 +1,31 @@
1
+ import { DotAnalyticsPayload, PageViewEvent, TrackEvent } from '../shared/dot-content-analytics.model';
2
+
3
+ /**
4
+ * Plugin that enriches the analytics payload data based on the event type.
5
+ * For page view events, it adds browser data like viewport size, user agent, etc.
6
+ * For track events, it adds custom event properties and standardizes the event format.
7
+ * All events are enriched with source type and anonymous ID if available.
8
+ */
9
+ export declare const dotAnalyticsEnricherPlugin: {
10
+ name: string;
11
+ 'page:dot-analytics': ({ payload }: {
12
+ payload: DotAnalyticsPayload;
13
+ }) => {
14
+ properties: PageViewEvent;
15
+ type: string;
16
+ event: string;
17
+ options: Record<string, unknown>;
18
+ userId: string | null;
19
+ anonymousId: string | null;
20
+ };
21
+ 'track:dot-analytics': ({ payload }: {
22
+ payload: DotAnalyticsPayload;
23
+ }) => {
24
+ properties: TrackEvent;
25
+ type: string;
26
+ event: string;
27
+ options: Record<string, unknown>;
28
+ userId: string | null;
29
+ anonymousId: string | null;
30
+ };
31
+ };
@@ -0,0 +1,28 @@
1
+ import { ANALYTICS_PAGEVIEW_EVENT as n, ANALYTICS_SOURCE_TYPE as r, EventType as o } from "../shared/dot-content-analytics.constants.mjs";
2
+ import { getBrowserEventData as i } from "../shared/dot-content-analytics.utils.mjs";
3
+ const p = {
4
+ name: "enrich-dot-analytics",
5
+ "page:dot-analytics": ({ payload: e }) => {
6
+ const t = {
7
+ ...i(window.location),
8
+ ...e.properties,
9
+ event_type: n,
10
+ anonymousId: e.anonymousId || void 0,
11
+ src: r
12
+ };
13
+ return { ...e, properties: t };
14
+ },
15
+ "track:dot-analytics": ({ payload: e }) => {
16
+ const t = {
17
+ ...e.properties,
18
+ custom_event: e.event,
19
+ event_type: o.Track,
20
+ anonymousId: e.anonymousId || void 0,
21
+ src: r
22
+ };
23
+ return { ...e, properties: t };
24
+ }
25
+ };
26
+ export {
27
+ p as dotAnalyticsEnricherPlugin
28
+ };
@@ -0,0 +1,31 @@
1
+ import { DotAnalyticsParams, DotContentAnalyticsConfig } from '../shared/dot-content-analytics.model';
2
+
3
+ /**
4
+ * Analytics plugin for tracking page views and custom events in DotCMS applications.
5
+ * This plugin handles sending analytics data to the DotCMS server, managing initialization,
6
+ * and processing both automatic and manual tracking events.
7
+ *
8
+ * @param {DotAnalyticsConfig} config - Configuration object containing API key, server URL,
9
+ * debug mode and auto page view settings
10
+ * @returns {Object} Plugin object with methods for initialization and event tracking
11
+ */
12
+ export declare const dotAnalytics: (config: DotContentAnalyticsConfig) => {
13
+ name: string;
14
+ config: DotContentAnalyticsConfig;
15
+ /**
16
+ * Initialize the plugin
17
+ */
18
+ initialize: (params: DotAnalyticsParams) => Promise<void>;
19
+ /**
20
+ * Track a page view event
21
+ */
22
+ page: (params: DotAnalyticsParams) => Promise<void>;
23
+ /**
24
+ * Track a custom event
25
+ */
26
+ track: (params: DotAnalyticsParams) => Promise<void>;
27
+ /**
28
+ * Check if the plugin is loaded
29
+ */
30
+ loaded: () => boolean;
31
+ };
@@ -0,0 +1,55 @@
1
+ import { sendAnalyticsEventToServer as r } from "../shared/dot-content-analytics.http.mjs";
2
+ const s = (a) => {
3
+ let n = !1;
4
+ return {
5
+ name: "dot-analytics",
6
+ config: a,
7
+ /**
8
+ * Initialize the plugin
9
+ */
10
+ initialize: (o) => {
11
+ const { config: i, payload: t } = o;
12
+ if (i.debug && console.warn("DotAnalytics: Initialized with config", i), n = !0, i.autoPageView) {
13
+ const e = {
14
+ ...t.properties,
15
+ key: i.apiKey
16
+ };
17
+ return r(e, i);
18
+ }
19
+ return Promise.resolve();
20
+ },
21
+ /**
22
+ * Track a page view event
23
+ */
24
+ page: (o) => {
25
+ const { config: i, payload: t } = o;
26
+ if (!n)
27
+ throw new Error("DotAnalytics: Plugin not initialized");
28
+ const e = {
29
+ ...t.properties,
30
+ key: i.apiKey
31
+ };
32
+ return r(e, i);
33
+ },
34
+ /**
35
+ * Track a custom event
36
+ */
37
+ track: (o) => {
38
+ const { config: i, payload: t } = o;
39
+ if (!n)
40
+ throw new Error("DotAnalytics: Plugin not initialized");
41
+ const e = {
42
+ ...t.properties,
43
+ key: i.apiKey
44
+ };
45
+ return r(e, i);
46
+ },
47
+ /**
48
+ * Check if the plugin is loaded
49
+ */
50
+ loaded: () => n
51
+ };
52
+ };
53
+ export {
54
+ s as dotAnalytics
55
+ };
@@ -0,0 +1,13 @@
1
+ export declare const ANALYTICS_WINDOWS_KEY = "dotAnalytics";
2
+ export declare const ANALYTICS_SOURCE_TYPE = "dotAnalytics";
3
+ export declare const ANALYTICS_ENDPOINT = "/api/v1/analytics/content/event";
4
+ export declare const ANALYTICS_PAGEVIEW_EVENT = "PAGE_REQUEST";
5
+ export declare const ANALYTICS_TRACK_EVENT = "TRACK_EVENT";
6
+ export declare const EXPECTED_UTM_KEYS: string[];
7
+ /**
8
+ * The type of event.
9
+ */
10
+ export declare enum EventType {
11
+ Track = "track",
12
+ PageView = "pageview"
13
+ }
@@ -0,0 +1,10 @@
1
+ const E = "dotAnalytics", c = E, a = "/api/v1/analytics/content/event", n = "PAGE_REQUEST", A = ["utm_source", "utm_medium", "utm_campaign", "utm_id"];
2
+ var _ = /* @__PURE__ */ ((t) => (t.Track = "track", t.PageView = "pageview", t))(_ || {});
3
+ export {
4
+ a as ANALYTICS_ENDPOINT,
5
+ n as ANALYTICS_PAGEVIEW_EVENT,
6
+ c as ANALYTICS_SOURCE_TYPE,
7
+ E as ANALYTICS_WINDOWS_KEY,
8
+ A as EXPECTED_UTM_KEYS,
9
+ _ as EventType
10
+ };
@@ -0,0 +1,9 @@
1
+ import { DotContentAnalyticsConfig } from './dot-content-analytics.model';
2
+
3
+ /**
4
+ * Send an analytics event to the server
5
+ * @param data - The event data
6
+ * @param options - The options for the event
7
+ * @returns A promise that resolves to the response from the server
8
+ */
9
+ export declare const sendAnalyticsEventToServer: (data: Record<string, unknown>, options: DotContentAnalyticsConfig) => Promise<void>;
@@ -0,0 +1,22 @@
1
+ import { ANALYTICS_ENDPOINT as o } from "./dot-content-analytics.constants.mjs";
2
+ const a = async (n, t) => {
3
+ const r = {
4
+ ...n,
5
+ timestamp: (/* @__PURE__ */ new Date()).toISOString(),
6
+ key: t.apiKey
7
+ };
8
+ t.debug && console.warn("DotAnalytics: Event sent:", r);
9
+ try {
10
+ const e = await fetch(`${t.server}${o}`, {
11
+ method: "POST",
12
+ headers: { "Content-Type": "application/json" },
13
+ body: JSON.stringify(r)
14
+ });
15
+ e.ok || console.error(`DotAnalytics: Server responded with status ${e.status}`);
16
+ } catch (e) {
17
+ console.error("DotAnalytics: Error sending event:", e);
18
+ }
19
+ };
20
+ export {
21
+ a as sendAnalyticsEventToServer
22
+ };
@@ -0,0 +1,143 @@
1
+ import { ANALYTICS_PAGEVIEW_EVENT, EventType, EXPECTED_UTM_KEYS } from './dot-content-analytics.constants';
2
+
3
+ /**
4
+ * Configuration interface for DotAnalytics SDK.
5
+ *
6
+ * @interface DotAnalyticsConfig
7
+ */
8
+ export interface DotContentAnalyticsConfig {
9
+ /**
10
+ * The URL of the Analytics server endpoint.
11
+ */
12
+ server: string;
13
+ /**
14
+ * Enable debug mode to get additional logging information.
15
+ */
16
+ debug: boolean;
17
+ /**
18
+ * Automatically track page views when set to true.
19
+ */
20
+ autoPageView?: boolean;
21
+ /**
22
+ * The API key for authenticating with the Analytics service.
23
+ */
24
+ apiKey: string;
25
+ /**
26
+ * Custom redirect function handler.
27
+ * When provided, this function will be called instead of the default browser redirect
28
+ * for handling URL redirections.
29
+ *
30
+ * @param {string} url - The URL to redirect to
31
+ */
32
+ redirectFn?: (url: string) => void;
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;
44
+ }
45
+ export interface BrowserEventData {
46
+ utc_time: string;
47
+ 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;
56
+ doc_hash: string;
57
+ doc_search: string;
58
+ referrer: string;
59
+ page_title: string;
60
+ utm: UTMParams;
61
+ }
62
+ export interface PageViewEvent extends BaseEventData, BrowserEventData {
63
+ event_type: typeof ANALYTICS_PAGEVIEW_EVENT;
64
+ }
65
+ export interface TrackEvent {
66
+ event_type: 'track';
67
+ custom_event: string;
68
+ [key: string]: unknown;
69
+ }
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
+ };
82
+ }
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
+ };
95
+ }
96
+ /**
97
+ * The payload for a track event.
98
+ */
99
+ export interface DotAnalyticsPayload {
100
+ type: string;
101
+ properties: {
102
+ title: string;
103
+ url: string;
104
+ path: string;
105
+ hash: string;
106
+ search: string;
107
+ width: number;
108
+ height: number;
109
+ source: string;
110
+ };
111
+ event: string;
112
+ options: Record<string, unknown>;
113
+ 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;
128
+ }
129
+ /**
130
+ * Params for the DotAnalytics plugin
131
+ */
132
+ export interface DotAnalyticsParams {
133
+ config: DotContentAnalyticsConfig;
134
+ payload: DotAnalyticsPayload;
135
+ }
136
+ /**
137
+ * Shared interface for the DotAnalytics plugin
138
+ */
139
+ export interface DotAnalytics {
140
+ pageView: (payload?: Record<string, unknown>) => void;
141
+ track: (eventName: string, payload?: Record<string, unknown>) => void;
142
+ }
143
+ export {};
@@ -0,0 +1,48 @@
1
+ import { BrowserEventData, DotContentAnalyticsConfig } from './dot-content-analytics.model';
2
+
3
+ /**
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.
7
+ */
8
+ export declare const getDataAnalyticsAttributes: (location: Location) => DotContentAnalyticsConfig;
9
+ /**
10
+ * Retrieves the analytics script tag from the document.
11
+ *
12
+ * @returns {HTMLScriptElement} - The analytics script tag.
13
+ */
14
+ export declare const getAnalyticsScriptTag: () => HTMLScriptElement;
15
+ /**
16
+ * Retrieves the browser event data.
17
+ *
18
+ * @param {Location} location - The location object.
19
+ * @returns {BrowserEventData} - The browser event data.
20
+ */
21
+ export declare const getBrowserEventData: (location: Location) => BrowserEventData;
22
+ /**
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.
27
+ */
28
+ export declare const extractUTMParameters: (location: Location) => Record<string, string>;
29
+ /**
30
+ * A function to redirect the user to a new URL.
31
+ *
32
+ * @param {string} href - The URL to redirect to.
33
+ * @returns {void}
34
+ */
35
+ export declare const defaultRedirectFn: (href: string) => string;
36
+ /**
37
+ * Checks if the current environment is inside the dotCMS editor.
38
+ *
39
+ * @returns {boolean} - True if inside the editor, false otherwise.
40
+ */
41
+ export declare const isInsideEditor: () => boolean;
42
+ /**
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.
47
+ */
48
+ export declare const createAnalyticsInstance: (config: DotContentAnalyticsConfig) => import('analytics').AnalyticsInstance | null;
@@ -0,0 +1,46 @@
1
+ import { Analytics as i } from "../../../node_modules/analytics/lib/analytics.browser.es.mjs";
2
+ import { EXPECTED_UTM_KEYS as a } from "./dot-content-analytics.constants.mjs";
3
+ import { dotAnalyticsEnricherPlugin as s } from "../plugin/dot-analytics.enricher.plugin.mjs";
4
+ import { dotAnalytics as c } from "../plugin/dot-analytics.plugin.mjs";
5
+ const h = (e) => ({
6
+ utc_time: (/* @__PURE__ */ new Date()).toISOString(),
7
+ local_tz_offset: (/* @__PURE__ */ new Date()).getTimezoneOffset(),
8
+ screen_resolution: `${window.screen.width}x${window.screen.height}`,
9
+ vp_size: `${window.innerWidth}x${window.innerHeight}`,
10
+ userAgent: navigator.userAgent,
11
+ user_language: navigator.language,
12
+ doc_encoding: document.characterSet,
13
+ doc_path: e.pathname,
14
+ doc_host: e.hostname,
15
+ doc_protocol: e.protocol,
16
+ doc_hash: e.hash,
17
+ doc_search: e.search,
18
+ referrer: document.referrer,
19
+ page_title: document.title,
20
+ utm: u(window.location)
21
+ }), u = (e) => {
22
+ const o = new URLSearchParams(e.search);
23
+ return a.reduce(
24
+ (t, r) => {
25
+ const n = o.get(r);
26
+ return n !== null && (t[r.replace("utm_", "")] = n), t;
27
+ },
28
+ {}
29
+ );
30
+ }, m = () => {
31
+ try {
32
+ return typeof window > "u" || !window.parent ? !1 : window.parent !== window;
33
+ } catch {
34
+ return !1;
35
+ }
36
+ }, p = (e) => e.apiKey ? e.server ? i({
37
+ app: "dotAnalytics",
38
+ debug: e.debug,
39
+ plugins: [s, c(e)]
40
+ }) : (console.error('DotContentAnalytics: Missing "server" in configuration'), null) : (console.error('DotContentAnalytics: Missing "apiKey" in configuration'), null);
41
+ export {
42
+ p as createAnalyticsInstance,
43
+ u as extractUTMParameters,
44
+ h as getBrowserEventData,
45
+ m as isInsideEditor
46
+ };
@@ -0,0 +1,20 @@
1
+ import { DotContentAnalyticsConfig } from '../../dotAnalytics/shared/dot-content-analytics.model';
2
+ import { ReactElement, ReactNode } from 'react';
3
+
4
+ interface DotContentAnalyticsProviderProps {
5
+ children?: ReactNode;
6
+ config: DotContentAnalyticsConfig;
7
+ }
8
+ /**
9
+ * Provider component that initializes and manages DotContentAnalytics instance.
10
+ * It makes the analytics functionality available to all child components through React Context.
11
+ * This component is responsible for:
12
+ * - Initializing the DotContentAnalytics singleton instance with the provided config
13
+ * - Making the instance accessible via useContext hook to child components
14
+ * - Managing the lifecycle of the analytics instance
15
+ *
16
+ * @param {DotContentAnalyticsProviderProps} props - Configuration and children to render
17
+ * @returns {ReactElement} Provider component that enables analytics tracking
18
+ */
19
+ export declare const DotContentAnalyticsProvider: ({ children, config }: DotContentAnalyticsProviderProps) => ReactElement;
20
+ export {};
@@ -0,0 +1,15 @@
1
+ import { jsx as e } from "react/jsx-runtime";
2
+ import { useMemo as i } from "react";
3
+ import { initializeContentAnalytics as n } from "../../dotAnalytics/dot-content-analytics.mjs";
4
+ import a from "../contexts/DotContentAnalyticsContext.mjs";
5
+ import { useRouterTracker as m } from "../hook/useRouterTracker.mjs";
6
+ const f = ({
7
+ children: r,
8
+ config: t
9
+ }) => {
10
+ const o = i(() => n(t), [t]);
11
+ return t.autoPageView !== !1 && m(o), /* @__PURE__ */ e(a.Provider, { value: o, children: r });
12
+ };
13
+ export {
14
+ f as DotContentAnalyticsProvider
15
+ };
@@ -0,0 +1,13 @@
1
+ import { DotAnalytics } from '../../dotAnalytics/shared/dot-content-analytics.model';
2
+
3
+ /**
4
+ * `DotContentAnalyticsContext` is a React context that is designed to provide an instance of
5
+ * `DotContentAnalytics` to all of the components within its tree that are Consumers of this context.
6
+ *
7
+ * The context is created with a default value of `null`. It is meant to be provided a real value
8
+ * using the `DotContentAnalyticsProvider` component.
9
+ *
10
+ * @see {@link https://reactjs.org/docs/context.html|React Context}
11
+ */
12
+ declare const DotContentAnalyticsContext: import('react').Context<DotAnalytics | null>;
13
+ export default DotContentAnalyticsContext;
@@ -0,0 +1,5 @@
1
+ import { createContext as t } from "react";
2
+ const e = t(null);
3
+ export {
4
+ e as default
5
+ };
@@ -0,0 +1,22 @@
1
+ import { DotAnalytics } from '../../dotAnalytics/shared/dot-content-analytics.model';
2
+
3
+ /**
4
+ * Custom hook that handles analytics page view tracking.
5
+ *
6
+ * @example
7
+ * ```tsx
8
+ * function Button({ title, urlTitle }) {
9
+ * const { track } = useContentAnalytics();
10
+ *
11
+ * // First parameter: custom event name to identify the action
12
+ * // Second parameter: object with properties you want to track
13
+ * return (
14
+ * <button onClick={() => track('btn-click', { title, urlTitle })}>
15
+ * See Details →
16
+ * </button>
17
+ * );
18
+ * }
19
+ * ```
20
+ * @returns {DotContentAnalyticsCustomHook} - The analytics instance used to track page views
21
+ */
22
+ export declare const useContentAnalytics: () => DotAnalytics;
@@ -0,0 +1,34 @@
1
+ import { useContext as i, useRef as s } from "react";
2
+ import { isInsideEditor as r } from "../../dotAnalytics/shared/dot-content-analytics.utils.mjs";
3
+ import a from "../contexts/DotContentAnalyticsContext.mjs";
4
+ const f = () => {
5
+ const t = i(a), o = s(null);
6
+ if (!t)
7
+ throw new Error("useContentAnalytics must be used within a DotContentAnalyticsProvider");
8
+ return {
9
+ /**
10
+ * Track a custom event.
11
+ * @param {string} eventName - The name of the event to track.
12
+ * @param {Record<string, unknown>} payload - The payload to track.
13
+ */
14
+ track: (n, e = {}) => {
15
+ r() || t == null || t.track(n, {
16
+ ...e,
17
+ timestamp: (/* @__PURE__ */ new Date()).toISOString()
18
+ });
19
+ },
20
+ /**
21
+ * Track a page view.
22
+ * @param {Record<string, unknown>} payload - The payload to track.
23
+ */
24
+ pageView: (n = {}) => {
25
+ if (!r()) {
26
+ const e = window.location.pathname;
27
+ e !== o.current && (o.current = e, t.pageView(n));
28
+ }
29
+ }
30
+ };
31
+ };
32
+ export {
33
+ f as useContentAnalytics
34
+ };
@@ -0,0 +1,10 @@
1
+ import { DotAnalytics } from '../../dotAnalytics/shared/dot-content-analytics.model';
2
+
3
+ /**
4
+ * Internal custom hook that handles analytics page view tracking.
5
+ *
6
+ * @param {DotContentAnalytics | null} instance - The analytics instance used to track page views
7
+ * @returns {void}
8
+ *
9
+ */
10
+ export declare function useRouterTracker(analytics: DotAnalytics | null): void;
@@ -0,0 +1,19 @@
1
+ import { useRef as r, useEffect as i } from "react";
2
+ import { isInsideEditor as u } from "../../dotAnalytics/shared/dot-content-analytics.utils.mjs";
3
+ function s(t) {
4
+ const n = r(null);
5
+ i(() => {
6
+ if (!t)
7
+ return;
8
+ function e() {
9
+ const o = window.location.pathname;
10
+ o !== n.current && !u() && t && (n.current = o, t.pageView());
11
+ }
12
+ return e(), window.addEventListener("popstate", e), window.addEventListener("beforeunload", e), () => {
13
+ window.removeEventListener("popstate", e), window.removeEventListener("beforeunload", e);
14
+ };
15
+ }, [t]);
16
+ }
17
+ export {
18
+ s as useRouterTracker
19
+ };
@@ -0,0 +1 @@
1
+ export * from './public-api';
@@ -0,0 +1,2 @@
1
+ export { DotContentAnalyticsProvider } from './components/DotContentAnalyticsProvider';
2
+ export { useContentAnalytics } from './hook/useContentAnalytics';
@@ -0,0 +1,8 @@
1
+ import { DotAnalytics } from './dotAnalytics/shared/dot-content-analytics.model';
2
+ import { ANALYTICS_WINDOWS_KEY } from './dotAnalytics/shared/dot-content-analytics.constants';
3
+
4
+ declare global {
5
+ interface Window {
6
+ [ANALYTICS_WINDOWS_KEY]: DotAnalytics;
7
+ }
8
+ }
package/package.json ADDED
@@ -0,0 +1,41 @@
1
+ {
2
+ "name": "@dotcms/analytics",
3
+ "version": "0.0.1-alpha.54",
4
+ "description": "Official JavaScript library for Content Analytics with DotCMS.",
5
+ "repository": {
6
+ "type": "git",
7
+ "url": "git+https://github.com/dotCMS/core.git#main"
8
+ },
9
+ "keywords": [
10
+ "dotCMS",
11
+ "CMS",
12
+ "Content Management",
13
+ "Analytics",
14
+ "Tracking"
15
+ ],
16
+ "author": "dotcms <dev@dotcms.com>",
17
+ "license": "MIT",
18
+ "bugs": {
19
+ "url": "https://github.com/dotCMS/core/issues"
20
+ },
21
+ "homepage": "https://github.com/dotCMS/core/tree/main/core-web/libs/sdk/analytics/README.md",
22
+ "dependencies": {
23
+ "analytics": "^0.8.14",
24
+ "vite": "~5.0.0"
25
+ },
26
+ "peerDependencies": {
27
+ "react": "^18.2.0"
28
+ },
29
+ "devDependencies": {
30
+ "@testing-library/jest-dom": "^6.1.6",
31
+ "@testing-library/react": "^14.0.0"
32
+ },
33
+ "main": "./index.cjs.js",
34
+ "module": "./index.esm.js",
35
+ "exports": {
36
+ "./react": {
37
+ "import": "./react/index.js",
38
+ "types": "./src/lib/react/index.d.ts"
39
+ }
40
+ }
41
+ }
@@ -0,0 +1,6 @@
1
+ import { DotContentAnalyticsProvider as e } from "../lib/react/components/DotContentAnalyticsProvider.mjs";
2
+ import { useContentAnalytics as r } from "../lib/react/hook/useContentAnalytics.mjs";
3
+ export {
4
+ e as DotContentAnalyticsProvider,
5
+ r as useContentAnalytics
6
+ };