UNPKG

@dotcms/analytics

Version:

Official JavaScript library for Content Analytics with DotCMS.

44 lines (43 loc) 1.57 kB
import { Analytics as s } from "analytics"; import { dotAnalytics as l } from "./plugin/dot-analytics.plugin.js"; import { dotAnalyticsEnricherPlugin as a } from "./plugin/enricher/dot-analytics.enricher.plugin.js"; import { dotAnalyticsIdentityPlugin as u } from "./plugin/identity/dot-analytics.identity.plugin.js"; import { cleanupActivityTracking as c, updateSessionActivity as o } from "./shared/dot-content-analytics.activity-tracker.js"; const f = (n) => { if (!n.siteKey) return console.error('DotContentAnalytics: Missing "siteKey" in configuration'), null; if (!n.server) return console.error('DotContentAnalytics: Missing "server" in configuration'), null; const t = s({ app: "dotAnalytics", debug: n.debug, plugins: [ u(n), // Inject identity context (user_id, session_id, local_tz) a(), // Enrich with page, device, utm data l(n) // Send events to server ] }), e = () => c(); return typeof window < "u" && (window.addEventListener("beforeunload", e), window.__dotAnalyticsCleanup = e), { /** * Track a page view. * @param {Record<string, unknown>} payload - The payload to track. */ pageView: (i = {}) => { o(), t == null || t.page(i); }, /** * Track a custom event. * @param {string} eventName - The name of the event to track. * @param {Record<string, unknown>} payload - The payload to track. */ track: (i, r = {}) => { o(), t == null || t.track(i, r); } }; }; export { f as initializeContentAnalytics };