export const TRACKING_EVENT_NAME = "developerwebsite_track"; export const LISTENER_QUEUE = "__DX_INSTRUMENTATION_QUEUE__"; export const LISTENER_INDICATOR = "__DX_INSTRUMENTATION_IS_INITIALIZED__"; export function track( element: EventTarget, event: string, payload: Record ): void { const detail = { event, payload }; // if window.LISTENER_INDICATOR is undefined, `dx-instrumentation` has not // been mounted, and the listener hasn't been attached. if ((window as any)[LISTENER_INDICATOR] === undefined) { (window as any)[LISTENER_QUEUE] = (window as any)[LISTENER_QUEUE] || []; (window as any)[LISTENER_QUEUE].push(detail); } else { const e = new CustomEvent(TRACKING_EVENT_NAME, { bubbles: true, composed: true, detail }); element.dispatchEvent(e); } } /** * Creates a link click handler that tracks analytics and prevents default navigation. * Handles links within shadow DOM using composedPath(). * * @param contentCategory - Optional content category for analytics (default: "documentation") * @returns Event handler function for link clicks */ export function buildDocLinkClickHandler( contentCategory: string = "documentation" ): (event: Event) => void { return (event: Event): void => { // Get the full path including shadow DOM elements const path = event.composedPath(); // Find the first anchor element in the path const anchor = path.find((el) => el instanceof HTMLAnchorElement) as | HTMLAnchorElement | undefined; if (anchor && anchor.href && !anchor.href.startsWith("#")) { track(anchor, "custEv_linkClick", { click_text: anchor.textContent || "", click_url: anchor.href, element_title: anchor.textContent || "", element_type: "link", content_category: contentCategory }); } }; }