UNPKG

@dotcms/analytics

Version:

Official JavaScript library for Content Analytics with DotCMS.

56 lines (55 loc) 1.84 kB
import { CLICKABLE_ELEMENTS_SELECTOR as l, CLICK_EVENT_TYPE as m } from "../../shared/constants/dot-analytics.constants.js"; import { extractContentletData as p } from "../../shared/utils/dot-analytics.utils.js"; import { getViewportMetrics as b } from "../impression/dot-analytics.impression.utils.js"; const _ = (r, o, c, e) => { const a = r.target; e.debug("Click detected on:", a); const t = a.closest(l); if (!t) { e.debug("No <a> or <button> found in click path"); return; } if (!o.contains(t)) { e.debug("Click was outside contentlet boundary"); return; } e.debug("Found clickable element:", t); const n = p(o); if (!n.identifier) { e.debug("Contentlet has no identifier"); return; } e.debug("Contentlet data:", n); const d = b(o), s = []; for (const i of t.attributes) !i.name.startsWith("data-dot-analytics") && i.name !== "class" && i.name !== "id" && i.name !== "href" && s.push(`${i.name}:${i.value}`); const f = parseInt(o.dataset.dotAnalyticsDomIndex || "-1", 10), u = { content: { identifier: n.identifier, inode: n.inode, title: n.title, content_type: n.contentType }, position: { viewport_offset_pct: d.offsetPercentage, dom_index: f }, element: { text: (t.innerText || t.textContent || "").trim().substring(0, 100), // Limit length type: t.tagName.toLowerCase(), id: t.id || "", // Required by backend, empty string if not present class: t.className || "", // Required by backend, empty string if not present href: t.getAttribute("href") || "", // Path as written in HTML (relative), empty string for buttons attributes: s // Additional attributes (data-*, aria-*, target, etc.) } }; c(m, u); }; export { _ as handleContentletClick };