All files / app/features/analytics eventCollector.ts

89.41% Statements 76/85
65.51% Branches 19/29
94.73% Functions 18/19
94.73% Lines 72/76

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256                                            1x   1x 1x 1x 1x   1x     1x                   1x 1x 1x 1x 1x       15x 13x               15x   15x 14x 14x           25x                   22x   22x 22x   22x   22x   9x       13x 13x 13x 13x         22x                   23x   6x 6x   6x   6x               6x 6x                 1x           1x 1x         2x         15x   15x 15x         35x   35x 35x                                 18x         18x   17x             17x     15x 12x 8x 8x     2x 2x                   20x 20x   20x 5x 5x     20x 20x                                         15x 15x 15x 15x 15x 15x     15x 15x    
/**
 * Analytics Event Collector
 *
 * Tracks user behavior and app performance by collecting events (page views,
 * button clicks, errors, etc.) and sending them to our backend for analysis.
 *
 * HOW IT WORKS:
 * - Events are queued in the user's browser temporarily
 * - They're sent to the server in batches every 3 seconds
 * - If sending fails, we retry with exponential backoff
 * - When the user leaves the page, remaining events are sent immediately
 *
 * BACKEND INTEGRATION:
 * - Events are sent to the backend's ReportDiagnostics endpoint
 * - Backend stores them permanently in the database for analytics and debugging
 *
 * USAGE:
 * 1. Call initializeCollector() once when your app starts
 * 2. Use logEvent() anywhere to track actions
 * 3. Call destroyCollector() when cleaning up (e.g., on app unmount)
 */
 
import { DiagnosticEvent, reportDiagnostics } from "service/diagnostics";
 
const FLUSH_INTERVAL_MS = 3_000;
const MAX_BATCH_SIZE = 100;
const INITIAL_FLUSH_DELAY_MS = 150;
const MAX_BACKOFF_MS = 60_000;
 
const API_BASE_URL = (process.env.NEXT_PUBLIC_API_BASE_URL ||
  process.env.EXPO_PUBLIC_API_BASE_URL)!;
 
const FRONTEND_VERSION = process.env.NEXT_PUBLIC_VERSION ?? "unknown";
 
interface QueuedEvent {
  tag: string;
  properties: Record<string, unknown>;
  value: number;
  occurred: Date;
}
 
// Module-level state
let eventQueue: QueuedEvent[] = [];
let flushTimerId: ReturnType<typeof setTimeout> | null = null;
let hasFlushedOnce = false;
let isDestroyed = false;
let consecutiveFailures = 0;
 
// Calculate backoff interval based on consecutive failures
function getFlushInterval(): number {
  if (consecutiveFailures === 0) return FLUSH_INTERVAL_MS;
  return Math.min(
    FLUSH_INTERVAL_MS * Math.pow(2, consecutiveFailures),
    MAX_BACKOFF_MS,
  );
}
 
// Schedule the next flush with current interval
function scheduleFlush(): void {
  Iif (flushTimerId || isDestroyed) return;
 
  flushTimerId = setTimeout(() => {
    flushTimerId = null;
    flush();
  }, getFlushInterval());
}
 
// Convert queued events to diagnostic events format
function toDiagnosticEvents(events: QueuedEvent[]): DiagnosticEvent[] {
  return events.map((event) => ({
    tag: event.tag,
    propertiesJson: JSON.stringify(event.properties),
    value: event.value,
    occurred: event.occurred,
  }));
}
 
// Send events via gRPC with retry logic
function flush(): void {
  Iif (eventQueue.length === 0) return;
 
  const batch = eventQueue.splice(0, MAX_BATCH_SIZE);
  hasFlushedOnce = true;
 
  const diagnosticEvents = toDiagnosticEvents(batch);
 
  reportDiagnostics(diagnosticEvents, FRONTEND_VERSION)
    .then(() => {
      consecutiveFailures = 0;
    })
    .catch(() => {
      // On failure, put events back at front of queue for retry
      eventQueue.unshift(...batch);
      consecutiveFailures++;
      if (!isDestroyed && !flushTimerId) {
        scheduleFlush();
      }
    });
 
  // Schedule next flush if there are remaining events
  Iif (eventQueue.length > 0 && !flushTimerId && !isDestroyed) {
    scheduleFlush();
  }
}
 
/**
 * Flush via fetch with keepalive (works during page unload).
 * Falls back to putting events back in queue if fetch fails.
 */
function flushViaBeacon(): void {
  if (eventQueue.length === 0) return;
 
  const batch = eventQueue.splice(0, MAX_BATCH_SIZE);
  hasFlushedOnce = true;
 
  const body = JSON.stringify({
    frontendVersion: FRONTEND_VERSION,
    infos: batch.map((event) => ({
      tag: event.tag,
      propertiesJson: JSON.stringify(event.properties),
      value: event.value,
      occurred: event.occurred.toISOString(),
    })),
  });
 
  try {
    fetch(`${API_BASE_URL}/org.couchers.bugs.Bugs/ReportDiagnostics`, {
      method: "POST",
      headers: { "Content-Type": "application/json" },
      credentials: "include",
      keepalive: true,
      body,
    });
  } catch {
    // If fetch fails (e.g. SSR or unsupported), put events back
    eventQueue.unshift(...batch);
  }
}
 
// Event handlers for page visibility and unload
function handleVisibilityChange(): void {
  if (document.visibilityState === "hidden") {
    flushViaBeacon();
  }
}
 
function handleBeforeUnload(): void {
  flushViaBeacon();
}
 
// Initialize event listeners (browser only)
function initializeListeners(): void {
  Iif (typeof window === "undefined") return;
 
  document.addEventListener("visibilitychange", handleVisibilityChange);
  window.addEventListener("beforeunload", handleBeforeUnload);
}
 
// Clean up event listeners
function cleanupListeners(): void {
  Iif (typeof window === "undefined") return;
 
  document.removeEventListener("visibilitychange", handleVisibilityChange);
  window.removeEventListener("beforeunload", handleBeforeUnload);
}
 
/**
 * Track a user action or app event.
 *
 * Use this whenever something meaningful happens that you want to measure
 * (e.g., "page.viewed", "button.clicked", "search.performed").
 *
 * @param eventType - Name of the event (e.g., "page.viewed")
 * @param properties - Additional data about the event (e.g., {path: "/dashboard"})
 * @param value - Numeric value to associate (defaults to 1)
 *
 * @example
 * logEvent("search.performed", { query: "Paris", results: 42 });
 * logEvent("button.clicked", { button_id: "submit" });
 */
export function logEvent(
  eventType: string,
  properties: Record<string, unknown> = {},
  value = 1,
): void {
  if (isDestroyed) return;
 
  eventQueue.push({
    tag: eventType,
    properties,
    value,
    occurred: new Date(),
  });
 
  if (!hasFlushedOnce) {
    // Batch the initial events (e.g. session.started + first page.viewed)
    // together with a short delay instead of flushing immediately
    if (!flushTimerId) {
      flushTimerId = setTimeout(() => {
        flushTimerId = null;
        flush();
      }, INITIAL_FLUSH_DELAY_MS);
    }
  } else if (!flushTimerId) {
    scheduleFlush();
  }
}
 
/**
 * Stop tracking and send any remaining events.
 *
 * Call this when cleaning up (e.g., when unmounting your app's root component).
 * Any events still in the queue will be sent immediately.
 */
export function destroyCollector(): void {
  isDestroyed = true;
 
  if (flushTimerId) {
    clearTimeout(flushTimerId);
    flushTimerId = null;
  }
 
  cleanupListeners();
  flushViaBeacon();
}
 
/**
 * Set up the event collector to start tracking.
 *
 * Call this once when your app starts (typically in your root provider).
 * This enables automatic event flushing when users close their tab or
 * switch away from the page.
 */
export function initializeCollector(): void {
  Iif (typeof window === "undefined") return;
  initializeListeners();
}
 
/**
 * Reset the collector to a clean state.
 *
 * FOR TESTING ONLY - clears the queue and resets all internal state.
 * Call this in your test setup to ensure tests don't affect each other.
 */
export function _resetCollectorState(): void {
  eventQueue = [];
  flushTimerId = null;
  hasFlushedOnce = false;
  isDestroyed = false;
  consecutiveFailures = 0;
 
  // Re-initialize listeners for tests (they may have been cleaned up)
  cleanupListeners();
  initializeListeners();
}