@dotcms/analytics
Version:
Official JavaScript library for Content Analytics with DotCMS.
33 lines (32 loc) • 869 B
JavaScript
import { useContext as c, useRef as u, useCallback as a } from "react";
import { getUVEState as i } from "../../../uve/src/lib/core/core.utils.js";
import "../../../uve/src/internal/constants.js";
import l from "../contexts/DotContentAnalyticsContext.js";
const S = () => {
const t = c(l), n = u(null);
if (!t)
throw new Error(
"useContentAnalytics must be used within a DotContentAnalyticsProvider and analytics must be successfully initialized"
);
const s = a(
(o, e = {}) => {
i() || t.track(o, {
...e,
timestamp: (/* @__PURE__ */ new Date()).toISOString()
});
},
[t]
), r = a(() => {
if (!i()) {
const e = window.location.pathname;
e !== n.current && (n.current = e, t.pageView());
}
}, [t]);
return {
track: s,
pageView: r
};
};
export {
S as useContentAnalytics
};