UNPKG

@dotcms/analytics

Version:

Official JavaScript library for Content Analytics with DotCMS.

88 lines (87 loc) 3.38 kB
import { Analytics as l } from "analytics"; import { getUVEState as d } from "../../uve/src/lib/core/core.utils.js"; import "../../uve/src/internal/constants.js"; import { dotAnalyticsClickPlugin as y } from "./plugin/click/dot-analytics.click.plugin.js"; import { dotAnalyticsEnricherPlugin as m } from "./plugin/enricher/dot-analytics.enricher.plugin.js"; import { dotAnalyticsIdentityPlugin as u } from "./plugin/identity/dot-analytics.identity.plugin.js"; import { dotAnalyticsImpressionPlugin as A } from "./plugin/impression/dot-analytics.impression.plugin.js"; import { dotAnalytics as p } from "./plugin/main/dot-analytics.plugin.js"; import { ANALYTICS_WINDOWS_ACTIVE_KEY as e, ANALYTICS_WINDOWS_CLEANUP_KEY as f, DotCMSPredefinedEventType as w } from "./shared/constants/dot-analytics.constants.js"; import { validateAnalyticsConfig as C, isBrowser as g, getEnhancedTrackingPlugins as E } from "./shared/utils/dot-analytics.utils.js"; import { cleanupActivityTracking as S } from "./plugin/identity/dot-analytics.identity.activity-tracker.js"; const O = (t) => { const r = C(t); if (r) return console.error( `DotCMS Analytics [Core]: Missing ${r.join(" and ")} in configuration` ), typeof window < "u" && (window[e] = !1), null; if (g() && d()) return console.warn("DotCMS Analytics [Core]: Analytics disabled inside UVE editor"), window[e] = !1, null; const s = E( t, A, y ), i = l({ app: "dotAnalytics", debug: t.debug, plugins: [ u(t), // Inject identity context ...s, //Track content impressions & clicks (conditionally loaded) m(), // Enrich and clean payload with page, device, utm data and custom data p(t) // Send events to server ] }), a = () => S(); return typeof window < "u" && (window.addEventListener("beforeunload", a), window[f] = a, window[e] = !0, window.dispatchEvent(new CustomEvent("dotcms:analytics:ready"))), { /** * Track a page view. * Session activity is automatically updated by the identity plugin. * @param payload - Optional custom data to include with the page view (any valid JSON object) */ pageView: (n = {}) => { if (!i) { console.warn("DotCMS Analytics [Core]: Analytics instance not initialized"); return; } i.page(n); }, /** * Track a custom event. * @param eventName - The name of the event to track * @param payload - Custom data to include with the event (any valid JSON object) */ track: (n, o = {}) => { if (!i) { console.warn("DotCMS Analytics [Core]: Analytics instance not initialized"); return; } i.track(n, o); }, /** * Track a conversion event. * @param name - Name of the conversion (e.g., 'purchase', 'download', 'signup') * @param options - Optional custom data */ conversion: (n, o = {}) => { if (!i) { console.warn("DotCMS Analytics [Core]: Analytics instance not initialized"); return; } if (!n || n.trim() === "") { console.warn("DotCMS Analytics [Core]: Conversion name cannot be empty"); return; } const c = { name: n, ...Object.keys(o).length > 0 && { custom: o } }; i.track(w.CONVERSION, c); } }; }; export { O as initializeContentAnalytics };