All files / src/lib/analytics client-utils.ts

60.19% Statements 62/103
100% Branches 0/0
0% Functions 0/7
60.19% Lines 62/103

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 1041x 1x 1x 1x 1x 1x 1x 1x 1x 1x       1x 1x 1x 1x       1x 1x 1x 1x                                         1x 1x 1x 1x       1x 1x 1x 1x         1x 1x 1x 1x         1x 1x 1x 1x         1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x  
/**
 * Client-Side Analytics Utilities
 *
 * Utility functions for client-side analytics tracking.
 * These functions are safe to use in browser environments.
 */
 
/**
 * Generate a unique visitor ID
 */
export function generateVisitorId(): string {
  return `v_${Date.now()}_${Math.random().toString(36).substring(2, 15)}`;
}
 
/**
 * Generate a unique session ID
 */
export function generateSessionId(): string {
  return `s_${Date.now()}_${Math.random().toString(36).substring(2, 15)}`;
}
 
/**
 * Extract UTM parameters from URL
 */
export function extractUTMParams(url: string): {
  source?: string;
  medium?: string;
  campaign?: string;
  term?: string;
  content?: string;
} {
  try {
    const urlObj = new URL(url);
    return {
      source: urlObj.searchParams.get("utm_source") || undefined,
      medium: urlObj.searchParams.get("utm_medium") || undefined,
      campaign: urlObj.searchParams.get("utm_campaign") || undefined,
      term: urlObj.searchParams.get("utm_term") || undefined,
      content: urlObj.searchParams.get("utm_content") || undefined
    };
  } catch {
    return {};
  }
}
 
/**
 * Check if running in browser
 */
export function isBrowser(): boolean {
  return typeof window !== "undefined";
}
 
/**
 * Get current page URL safely
 */
export function getCurrentUrl(): string {
  if (!isBrowser()) return "";
  return window.location.href;
}
 
/**
 * Get document referrer safely
 */
export function getReferrer(): string | undefined {
  if (!isBrowser()) return undefined;
  return document.referrer || undefined;
}
 
/**
 * Get document title safely
 */
export function getPageTitle(): string | undefined {
  if (!isBrowser()) return undefined;
  return document.title || undefined;
}
 
/**
 * Cookie names for analytics
 */
export const ANALYTICS_COOKIES = {
  VISITOR_ID: "__ee_vid",
  SESSION_ID: "__ee_sid"
} as const;
 
/**
 * Session timeout in milliseconds (30 minutes)
 */
export const SESSION_TIMEOUT_MS = 30 * 60 * 1000;
 
/**
 * Heartbeat interval in milliseconds (30 seconds)
 */
export const HEARTBEAT_INTERVAL_MS = 30 * 1000;
 
/**
 * Event queue flush delay in milliseconds
 */
export const EVENT_FLUSH_DELAY_MS = 1000;
 
/**
 * Maximum events per batch
 */
export const MAX_EVENTS_PER_BATCH = 50;