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 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 31x 31x 31x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 31x 31x 31x 31x 31x 31x 31x 31x 31x 31x 31x 31x 31x 1x 1x 1x 1x 1x 1x 1x 1x 11x 11x 11x 11x 8x 8x 3x 3x 3x 3x 3x 11x 4x 4x 4x 4x 4x 4x 4x 4x 4x 3x 3x 3x 1x 1x 1x 1x 1x 1x 1x 1x | /**
* Analytics Utility Functions
*
* Server-side utility functions for analytics processing.
* Used by API routes for parsing user agents, hashing IPs, etc.
*/
import { createHash } from "crypto";
/**
* Generate a unique visitor ID
* Format: v_{timestamp}_{random}
*/
export function generateVisitorId(): string {
return `v_${Date.now()}_${Math.random().toString(36).substring(2, 15)}`;
}
/**
* Hash IP address for privacy
* Returns first 16 characters of SHA-256 hash
*/
export function hashIP(ip: string): string {
return createHash("sha256").update(ip).digest("hex").substring(0, 16);
}
/**
* Parse user agent string to extract browser and OS
*/
export function parseUserAgent(ua: string): { browser: string; os: string } {
let browser = "Unknown";
let os = "Unknown";
if (!ua) {
return { browser, os };
}
// Browser detection (order matters - more specific first)
if (ua.includes("Edg/") || ua.includes("Edge/")) {
browser = "Edge";
} else if (ua.includes("OPR/") || ua.includes("Opera/")) {
browser = "Opera";
} else if (ua.includes("Firefox/")) {
browser = "Firefox";
} else if (ua.includes("Chrome/")) {
browser = "Chrome";
} else if (ua.includes("Safari/") && !ua.includes("Chrome")) {
browser = "Safari";
} else if (ua.includes("MSIE") || ua.includes("Trident/")) {
browser = "Internet Explorer";
}
// OS detection
if (ua.includes("Windows NT 10")) {
os = "Windows 10/11";
} else if (ua.includes("Windows NT 6.3")) {
os = "Windows 8.1";
} else if (ua.includes("Windows NT 6.2")) {
os = "Windows 8";
} else if (ua.includes("Windows NT 6.1")) {
os = "Windows 7";
} else if (ua.includes("Windows")) {
os = "Windows";
} else if (ua.includes("Mac OS X")) {
os = "macOS";
} else if (ua.includes("Android")) {
os = "Android";
} else if (ua.includes("iPhone") || ua.includes("iPad") || ua.includes("iPod")) {
os = "iOS";
} else if (ua.includes("Linux")) {
os = "Linux";
} else if (ua.includes("CrOS")) {
os = "Chrome OS";
}
return { browser, os };
}
/**
* Detect device type from user agent
*/
export function getDeviceType(ua: string): "desktop" | "mobile" | "tablet" {
if (!ua) {
return "desktop";
}
const lowerUA = ua.toLowerCase();
// Tablet detection (must come before mobile as some tablets include mobile keywords)
if (
/tablet|ipad|playbook|silk|kindle/i.test(lowerUA) ||
(/android/i.test(lowerUA) && !/mobile/i.test(lowerUA))
) {
return "tablet";
}
// Mobile detection
if (
/mobile|iphone|ipod|android|blackberry|opera mini|iemobile|wpdesktop|windows phone/i.test(
lowerUA
)
) {
return "mobile";
}
return "desktop";
}
/**
* 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 {};
}
}
/**
* Get client IP from request headers
* Handles various proxy headers
*/
export function getClientIP(headers: Headers): string {
// Check for common proxy headers
const forwardedFor = headers.get("x-forwarded-for");
if (forwardedFor) {
// x-forwarded-for can contain multiple IPs, first one is the client
return forwardedFor.split(",")[0].trim();
}
const realIP = headers.get("x-real-ip");
if (realIP) {
return realIP;
}
const cfConnectingIP = headers.get("cf-connecting-ip");
if (cfConnectingIP) {
return cfConnectingIP;
}
return "unknown";
}
/**
* Check if a path should be excluded from analytics tracking
*/
export function shouldExcludePath(path: string): boolean {
const excludedPaths = [
"/api/",
"/_next/",
"/favicon.ico",
"/robots.txt",
"/sitemap.xml",
"/manifest.json",
];
const excludedExtensions = [".js", ".css", ".png", ".jpg", ".jpeg", ".gif", ".svg", ".ico"];
// Check excluded paths
if (excludedPaths.some((excluded) => path.startsWith(excluded))) {
return true;
}
// Check file extensions
if (excludedExtensions.some((ext) => path.endsWith(ext))) {
return true;
}
return false;
}
/**
* Sanitize event properties to remove any PII
*/
export function sanitizeEventProperties(
properties: Record<string, unknown> | undefined
): Record<string, unknown> | undefined {
if (!properties) {
return undefined;
}
const piiFields = ["email", "password", "phone", "ssn", "creditCard", "credit_card"];
const sanitized: Record<string, unknown> = {};
for (const [key, value] of Object.entries(properties)) {
// Skip PII fields
if (piiFields.some((pii) => key.toLowerCase().includes(pii.toLowerCase()))) {
continue;
}
// Recursively sanitize nested objects
if (typeof value === "object" && value !== null && !Array.isArray(value)) {
sanitized[key] = sanitizeEventProperties(value as Record<string, unknown>);
} else {
sanitized[key] = value;
}
}
return sanitized;
}
/**
* Generate a session ID
*/
export function generateSessionId(): string {
return `s_${Date.now()}_${Math.random().toString(36).substring(2, 15)}`;
}
/**
* Check if session has expired (30 minute timeout)
*/
export function isSessionExpired(lastSeenAt: Date, timeoutMs: number = 30 * 60 * 1000): boolean {
return Date.now() - lastSeenAt.getTime() > timeoutMs;
}
|