@dotcms/analytics
Version:
Official JavaScript library for Content Analytics with DotCMS.
35 lines (34 loc) • 1.04 kB
JavaScript
import { useContext as i, useRef as s } from "react";
import { isInsideEditor as r } from "../../dotAnalytics/shared/dot-content-analytics.utils.js";
import a from "../contexts/DotContentAnalyticsContext.js";
const f = () => {
const t = i(a), o = s(null);
if (!t)
throw new Error("useContentAnalytics must be used within a DotContentAnalyticsProvider");
return {
/**
* Track a custom event.
* @param {string} eventName - The name of the event to track.
* @param {Record<string, unknown>} payload - The payload to track.
*/
track: (n, e = {}) => {
r() || t == null || t.track(n, {
...e,
timestamp: (/* @__PURE__ */ new Date()).toISOString()
});
},
/**
* Track a page view.
* @param {Record<string, unknown>} payload - The payload to track.
*/
pageView: (n = {}) => {
if (!r()) {
const e = window.location.pathname;
e !== o.current && (o.current = e, t.pageView(n));
}
}
};
};
export {
f as useContentAnalytics
};