services/tracking/events.jsjavascript
import { getAnalytics } from "@tracking/messe-tracking/react";

// custom tracking events outside of React components (e.g. before a reload); components use
// useTracking() instead. payload.area comes from the SDK's context option (see index.jsx)
export const track = (eventType, payload, element = null) =>
  getAnalytics().trackEvent(eventType, { element, payload });

// sends the queue right away, e.g. before a reload (otherwise events can arrive twice); waits at
// most `timeout` ms, so a slow connection doesn't block
export const flushTracking = (timeout = 3000) =>
  Promise.race([
    getAnalytics()
      .flush()
      .catch(() => {}),
    new Promise((resolve) => setTimeout(resolve, timeout)),
  ]);

// the SDK has no getter: read from its localStorage key; null while tracking is off
export const getDeviceId = () => {
  if (!getAnalytics().enabled) return null;
  try {
    return localStorage.getItem("analytics_device_id");
  } catch {
    return null;
  }
};