export function packageIntervalLog()

in products/userale/src/packageLogs.ts [254:337]


export function packageIntervalLog(e: Event) {
  try {
    const target = e.target ? getSelector(e.target) : null;
    const path = buildPath(e);
    const type = e.type;
    const timestamp = Math.floor(
      e.timeStamp && e.timeStamp > 0 ? config.time(e.timeStamp) : Date.now(),
    );

    // Init - this should only happen once on initialization
    if (intervalId == null) {
      intervalId = target;
      intervalType = type;
      intervalPath = path;
      intervalTimer = timestamp;
      intervalCounter = 0;
    }

    if ((intervalId !== target || intervalType !== type) && intervalTimer) {
      // When to create log? On transition end
      // @todo Possible for intervalLog to not be pushed in the event the interval never ends...

      intervalLog = {
        target: intervalId,
        path: intervalPath,
        pageUrl: window.location.href,
        pageTitle: document.title,
        pageReferrer: document.referrer,
        browser: detectBrowser(),
        count: intervalCounter,
        duration: timestamp - intervalTimer, // microseconds
        startTime: intervalTimer,
        endTime: timestamp,
        type: intervalType,
        logType: "interval",
        targetChange: intervalId !== target,
        typeChange: intervalType !== type,
        userAction: false,
        userId: config.userId,
        toolVersion: config.toolVersion,
        toolName: config.toolName,
        useraleVersion: config.useraleVersion,
        sessionId: config.sessionId,
        httpSessionId: config.httpSessionId,
        browserSessionId: config.browserSessionId,
      };

      if (typeof filterHandler === "function" && !filterHandler(intervalLog)) {
        return false;
      }

      if (typeof mapHandler === "function") {
        intervalLog = mapHandler(intervalLog, e);
      }

      for (const func of Object.values(cbHandlers)) {
        if (typeof func === "function") {
          intervalLog = func(intervalLog, null);
          if (!intervalLog) {
            return false;
          }
        }
      }

      if (intervalLog) logs.push(intervalLog);

      // Reset
      intervalId = target;
      intervalType = type;
      intervalPath = path;
      intervalTimer = timestamp;
      intervalCounter = 0;
    }

    // Interval is still occuring, just update counter
    if (intervalId == target && intervalType == type && intervalCounter) {
      intervalCounter = intervalCounter + 1;
    }

    return true;
  } catch {
    return false;
  }
}