File size: 20,757 Bytes
94193b5 | 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 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 | /**
* Analytics Database Manager
*
* Manages the analytics portion of per-deployment SQLite databases containing:
* - Pageviews
* - Interactions (clicks, scrolls, exits)
* - Sessions
*
* Each deployment gets its own analytics database at deployments/{deploymentId}/analytics.sqlite
*/
import type { Database } from 'better-sqlite3';
import { v4 as uuidv4 } from 'uuid';
import { getAnalyticsDatabaseConnection, closeAnalyticsDatabase } from './sqlite-connection';
// Analytics types
export interface PageviewData {
pagePath: string;
referrer?: string;
country?: string;
userAgent?: string;
deviceType?: string;
sessionId: string;
loadTime?: number;
}
export interface InteractionData {
sessionId: string;
pagePath: string;
interactionType: string;
elementSelector?: string;
coordinates?: {
x: number;
y: number;
scrollY?: number;
viewportWidth?: number;
viewportHeight?: number;
documentHeight?: number;
};
scrollDepth?: number;
timeOnPage?: number;
}
export interface SessionData {
sessionId: string;
entryPage?: string;
exitPage?: string;
pageCount?: number;
duration?: number;
isBounce?: boolean;
}
export interface AnalyticsStats {
totalPageviews: number;
uniqueSessions: number;
avgSessionDuration: number;
bounceRate: number;
topPages: Array<{ path: string; views: number }>;
topReferrers: Array<{ referrer: string; count: number }>;
}
/**
* Helper to parse JSON safely with a fallback
*/
function parseJSON<T>(value: string | null | undefined, fallback: T): T {
if (!value) return fallback;
try {
return JSON.parse(value) as T;
} catch {
return fallback;
}
}
/**
* Per-deployment analytics database manager
*/
export class AnalyticsDatabase {
private db: Database;
private deploymentId: string;
private initialized = false;
constructor(deploymentId: string) {
this.deploymentId = deploymentId;
this.db = getAnalyticsDatabaseConnection(deploymentId);
}
/**
* Initialize the database schema
*/
init(): void {
if (this.initialized) return;
// Pageviews
this.db.exec(`
CREATE TABLE IF NOT EXISTS pageviews (
id INTEGER PRIMARY KEY AUTOINCREMENT,
page_path TEXT NOT NULL,
referrer TEXT,
country TEXT,
user_agent TEXT,
device_type TEXT,
session_id TEXT NOT NULL,
load_time INTEGER,
timestamp TEXT NOT NULL DEFAULT (datetime('now'))
)
`);
this.db.exec(`CREATE INDEX IF NOT EXISTS idx_pageviews_timestamp ON pageviews(timestamp)`);
this.db.exec(`CREATE INDEX IF NOT EXISTS idx_pageviews_session_id ON pageviews(session_id)`);
// Interactions
this.db.exec(`
CREATE TABLE IF NOT EXISTS interactions (
id TEXT PRIMARY KEY,
session_id TEXT NOT NULL,
page_path TEXT NOT NULL,
interaction_type TEXT NOT NULL,
element_selector TEXT,
coordinates TEXT,
scroll_depth INTEGER,
time_on_page INTEGER,
timestamp TEXT NOT NULL DEFAULT (datetime('now'))
)
`);
this.db.exec(`CREATE INDEX IF NOT EXISTS idx_interactions_page_path ON interactions(page_path)`);
this.db.exec(`CREATE INDEX IF NOT EXISTS idx_interactions_timestamp ON interactions(timestamp)`);
// Sessions
this.db.exec(`
CREATE TABLE IF NOT EXISTS sessions (
id TEXT PRIMARY KEY,
session_id TEXT NOT NULL,
entry_page TEXT,
exit_page TEXT,
page_count INTEGER DEFAULT 1,
duration INTEGER,
is_bounce INTEGER DEFAULT 1,
created_at TEXT NOT NULL DEFAULT (datetime('now')),
ended_at TEXT
)
`);
this.db.exec(`CREATE INDEX IF NOT EXISTS idx_sessions_session_id ON sessions(session_id)`);
this.db.exec(`CREATE INDEX IF NOT EXISTS idx_sessions_created_at ON sessions(created_at)`);
this.initialized = true;
}
/**
* Close the database connection
*/
close(): void {
closeAnalyticsDatabase(this.deploymentId);
}
// ============================================
// Analytics: Recording
// ============================================
recordPageview(data: PageviewData): void {
const stmt = this.db.prepare(`
INSERT INTO pageviews (
page_path, referrer, country, user_agent,
device_type, session_id, load_time
) VALUES (?, ?, ?, ?, ?, ?, ?)
`);
stmt.run(
data.pagePath,
data.referrer ?? null,
data.country ?? null,
data.userAgent ?? null,
data.deviceType ?? null,
data.sessionId,
data.loadTime ?? null
);
}
recordInteraction(data: InteractionData): void {
const stmt = this.db.prepare(`
INSERT INTO interactions (
id, session_id, page_path, interaction_type,
element_selector, coordinates, scroll_depth, time_on_page
) VALUES (?, ?, ?, ?, ?, ?, ?, ?)
`);
stmt.run(
uuidv4(),
data.sessionId,
data.pagePath,
data.interactionType,
data.elementSelector ?? null,
data.coordinates ? JSON.stringify(data.coordinates) : null,
data.scrollDepth ?? null,
data.timeOnPage ?? null
);
}
upsertSession(sessionId: string, pagePath: string): void {
const existing = this.db.prepare(
'SELECT * FROM sessions WHERE session_id = ?'
).get(sessionId) as Record<string, unknown> | undefined;
if (existing) {
const pageCount = (existing.page_count as number) + 1;
this.db.prepare(`
UPDATE sessions SET
exit_page = ?,
page_count = ?,
is_bounce = 0,
ended_at = datetime('now')
WHERE session_id = ?
`).run(pagePath, pageCount, sessionId);
} else {
this.db.prepare(`
INSERT INTO sessions (
id, session_id, entry_page, exit_page,
page_count, is_bounce
) VALUES (?, ?, ?, ?, 1, 1)
`).run(uuidv4(), sessionId, pagePath, pagePath);
}
}
updateSessionDuration(sessionId: string, duration: number): void {
this.db.prepare(`
UPDATE sessions SET duration = ?, ended_at = datetime('now')
WHERE session_id = ?
`).run(duration, sessionId);
}
// ============================================
// Analytics: Queries
// ============================================
getStats(days: number = 30): AnalyticsStats {
const dateLimit = new Date();
dateLimit.setDate(dateLimit.getDate() - days);
const dateLimitStr = dateLimit.toISOString();
const totalPageviews = (this.db.prepare(`
SELECT COUNT(*) as count FROM pageviews WHERE timestamp >= ?
`).get(dateLimitStr) as { count: number }).count;
const uniqueSessions = (this.db.prepare(`
SELECT COUNT(DISTINCT session_id) as count FROM pageviews WHERE timestamp >= ?
`).get(dateLimitStr) as { count: number }).count;
const avgDuration = (this.db.prepare(`
SELECT AVG(duration) as avg FROM sessions WHERE created_at >= ? AND duration IS NOT NULL
`).get(dateLimitStr) as { avg: number | null }).avg ?? 0;
const bounceData = this.db.prepare(`
SELECT
SUM(CASE WHEN is_bounce = 1 THEN 1 ELSE 0 END) as bounces,
COUNT(*) as total
FROM sessions WHERE created_at >= ?
`).get(dateLimitStr) as { bounces: number; total: number };
const bounceRate = bounceData.total > 0 ? (bounceData.bounces / bounceData.total) * 100 : 0;
const topPages = this.db.prepare(`
SELECT page_path as path, COUNT(*) as views
FROM pageviews WHERE timestamp >= ?
GROUP BY page_path
ORDER BY views DESC
LIMIT 10
`).all(dateLimitStr) as Array<{ path: string; views: number }>;
const topReferrers = this.db.prepare(`
SELECT referrer, COUNT(*) as count
FROM pageviews WHERE timestamp >= ? AND referrer IS NOT NULL AND referrer != ''
GROUP BY referrer
ORDER BY count DESC
LIMIT 10
`).all(dateLimitStr) as Array<{ referrer: string; count: number }>;
return {
totalPageviews,
uniqueSessions,
avgSessionDuration: avgDuration,
bounceRate,
topPages,
topReferrers,
};
}
getTopPages(days: number = 30, limit: number = 10): Array<{ path: string; views: number }> {
const dateLimit = new Date();
dateLimit.setDate(dateLimit.getDate() - days);
return this.db.prepare(`
SELECT page_path as path, COUNT(*) as views
FROM pageviews WHERE timestamp >= ?
GROUP BY page_path
ORDER BY views DESC
LIMIT ?
`).all(dateLimit.toISOString(), limit) as Array<{ path: string; views: number }>;
}
getTopReferrers(days: number = 30, limit: number = 10): Array<{ referrer: string; count: number }> {
const dateLimit = new Date();
dateLimit.setDate(dateLimit.getDate() - days);
return this.db.prepare(`
SELECT referrer, COUNT(*) as count
FROM pageviews WHERE timestamp >= ? AND referrer IS NOT NULL AND referrer != ''
GROUP BY referrer
ORDER BY count DESC
LIMIT ?
`).all(dateLimit.toISOString(), limit) as Array<{ referrer: string; count: number }>;
}
getPageviewsOverTime(days: number = 30): Array<{ date: string; views: number }> {
const dateLimit = new Date();
dateLimit.setDate(dateLimit.getDate() - days);
return this.db.prepare(`
SELECT DATE(timestamp) as date, COUNT(*) as views
FROM pageviews WHERE timestamp >= ?
GROUP BY DATE(timestamp)
ORDER BY date
`).all(dateLimit.toISOString()) as Array<{ date: string; views: number }>;
}
getHeatmapData(pagePath: string, interactionType: string = 'click'): Array<{
x: number;
y: number;
count: number;
}> {
const rows = this.db.prepare(`
SELECT coordinates, COUNT(*) as count
FROM interactions
WHERE page_path = ? AND interaction_type = ? AND coordinates IS NOT NULL
GROUP BY coordinates
`).all(pagePath, interactionType) as Array<{ coordinates: string; count: number }>;
return rows.map(row => {
const coords = parseJSON(row.coordinates, { x: 0, y: 0 });
return {
x: coords.x,
y: coords.y,
count: row.count,
};
});
}
getClickData(pagePath: string, dateFrom?: string, dateTo?: string, limit: number = 10000): Array<{
coordinates: string;
elementSelector: string | null;
timestamp: string;
}> {
let query = `
SELECT coordinates, element_selector, timestamp
FROM interactions
WHERE page_path = ? AND interaction_type = 'click' AND coordinates IS NOT NULL
`;
const params: (string | number)[] = [pagePath];
if (dateFrom) {
query += ' AND timestamp >= ?';
params.push(dateFrom);
}
if (dateTo) {
query += ' AND timestamp <= ?';
params.push(dateTo);
}
query += ' ORDER BY timestamp DESC LIMIT ?';
params.push(limit);
return this.db.prepare(query).all(...params) as Array<{
coordinates: string;
elementSelector: string | null;
timestamp: string;
}>;
}
getScrollData(pagePath: string, dateFrom?: string, dateTo?: string, limit: number = 10000): Array<{
scrollDepth: number;
timeOnPage: number | null;
timestamp: string;
}> {
let query = `
SELECT scroll_depth, time_on_page, timestamp
FROM interactions
WHERE page_path = ? AND interaction_type = 'scroll' AND scroll_depth IS NOT NULL
`;
const params: (string | number)[] = [pagePath];
if (dateFrom) {
query += ' AND timestamp >= ?';
params.push(dateFrom);
}
if (dateTo) {
query += ' AND timestamp <= ?';
params.push(dateTo);
}
query += ' ORDER BY timestamp DESC LIMIT ?';
params.push(limit);
const rows = this.db.prepare(query).all(...params) as Array<Record<string, unknown>>;
return rows.map(row => ({
scrollDepth: row.scroll_depth as number,
timeOnPage: row.time_on_page as number | null,
timestamp: row.timestamp as string,
}));
}
getSessionsWithJourneys(dateFrom?: string, dateTo?: string, limit: number = 100): Array<{
sessionId: string;
entryPage: string;
exitPage: string;
pageCount: number;
duration: number | null;
isBounce: boolean;
createdAt: string;
endedAt: string | null;
pages: Array<{ path: string; timestamp: string }>;
}> {
let sessionQuery = `
SELECT id, session_id, entry_page, exit_page, page_count, duration, is_bounce, created_at, ended_at
FROM sessions
WHERE 1=1
`;
const sessionParams: (string | number)[] = [];
if (dateFrom) {
sessionQuery += ' AND created_at >= ?';
sessionParams.push(dateFrom);
}
if (dateTo) {
sessionQuery += ' AND created_at <= ?';
sessionParams.push(dateTo);
}
sessionQuery += ' ORDER BY created_at DESC LIMIT ?';
sessionParams.push(limit);
const sessions = this.db.prepare(sessionQuery).all(...sessionParams) as Array<Record<string, unknown>>;
if (sessions.length === 0) {
return [];
}
const sessionIds = sessions.map(s => s.session_id as string);
const placeholders = sessionIds.map(() => '?').join(',');
const pageviews = this.db.prepare(`
SELECT session_id, page_path, timestamp
FROM pageviews
WHERE session_id IN (${placeholders})
ORDER BY session_id, timestamp ASC
`).all(...sessionIds) as Array<Record<string, unknown>>;
const pageviewsBySession = new Map<string, Array<{ path: string; timestamp: string }>>();
for (const pv of pageviews) {
const sessionId = pv.session_id as string;
if (!pageviewsBySession.has(sessionId)) {
pageviewsBySession.set(sessionId, []);
}
pageviewsBySession.get(sessionId)!.push({
path: pv.page_path as string,
timestamp: pv.timestamp as string,
});
}
return sessions.map(s => ({
sessionId: s.session_id as string,
entryPage: s.entry_page as string,
exitPage: s.exit_page as string,
pageCount: s.page_count as number,
duration: s.duration as number | null,
isBounce: Boolean(s.is_bounce),
createdAt: s.created_at as string,
endedAt: s.ended_at as string | null,
pages: pageviewsBySession.get(s.session_id as string) || [],
}));
}
getEngagementMetrics(days: number = 30): {
avgTimeOnPage: number;
avgScrollDepth: number;
scrollDepthDistribution: Record<number, number>;
exitPageCounts: Array<{ page: string; count: number }>;
} {
const dateLimit = new Date();
dateLimit.setDate(dateLimit.getDate() - days);
const dateLimitStr = dateLimit.toISOString();
const avgTime = (this.db.prepare(`
SELECT AVG(time_on_page) as avg
FROM interactions
WHERE timestamp >= ? AND interaction_type = 'exit' AND time_on_page IS NOT NULL
`).get(dateLimitStr) as { avg: number | null }).avg ?? 0;
const avgScroll = (this.db.prepare(`
SELECT AVG(scroll_depth) as avg
FROM interactions
WHERE timestamp >= ? AND interaction_type = 'scroll' AND scroll_depth IS NOT NULL
`).get(dateLimitStr) as { avg: number | null }).avg ?? 0;
const scrollRows = this.db.prepare(`
SELECT scroll_depth, COUNT(*) as count
FROM interactions
WHERE timestamp >= ? AND interaction_type = 'scroll' AND scroll_depth IS NOT NULL
GROUP BY scroll_depth
`).all(dateLimitStr) as Array<{ scroll_depth: number; count: number }>;
const scrollDepthDistribution: Record<number, number> = {};
for (const row of scrollRows) {
scrollDepthDistribution[row.scroll_depth] = row.count;
}
const exitPages = this.db.prepare(`
SELECT exit_page as page, COUNT(*) as count
FROM sessions
WHERE created_at >= ? AND exit_page IS NOT NULL
GROUP BY exit_page
ORDER BY count DESC
LIMIT 10
`).all(dateLimitStr) as Array<{ page: string; count: number }>;
return {
avgTimeOnPage: avgTime,
avgScrollDepth: avgScroll,
scrollDepthDistribution,
exitPageCounts: exitPages,
};
}
getOverviewStats(days: number = 30): {
totalPageviews: number;
uniqueSessions: number;
bounceRate: number;
avgSessionDuration: number;
avgPagesPerSession: number;
deviceBreakdown: Array<{ device: string; count: number }>;
} {
const dateLimit = new Date();
dateLimit.setDate(dateLimit.getDate() - days);
const dateLimitStr = dateLimit.toISOString();
const stats = this.getStats(days);
const avgPages = (this.db.prepare(`
SELECT AVG(page_count) as avg FROM sessions WHERE created_at >= ?
`).get(dateLimitStr) as { avg: number | null }).avg ?? 0;
const devices = this.db.prepare(`
SELECT device_type as device, COUNT(*) as count
FROM pageviews
WHERE timestamp >= ? AND device_type IS NOT NULL
GROUP BY device_type
ORDER BY count DESC
`).all(dateLimitStr) as Array<{ device: string; count: number }>;
return {
totalPageviews: stats.totalPageviews,
uniqueSessions: stats.uniqueSessions,
bounceRate: stats.bounceRate,
avgSessionDuration: stats.avgSessionDuration,
avgPagesPerSession: avgPages,
deviceBreakdown: devices,
};
}
getSessionJourneys(limit: number = 50): Array<{
sessionId: string;
entryPage: string;
exitPage: string;
pageCount: number;
duration: number;
isBounce: boolean;
}> {
const rows = this.db.prepare(`
SELECT session_id, entry_page, exit_page, page_count, duration, is_bounce
FROM sessions
ORDER BY created_at DESC
LIMIT ?
`).all(limit) as Array<Record<string, unknown>>;
return rows.map(row => ({
sessionId: row.session_id as string,
entryPage: row.entry_page as string,
exitPage: row.exit_page as string,
pageCount: row.page_count as number,
duration: row.duration as number ?? 0,
isBounce: Boolean(row.is_bounce),
}));
}
// ============================================
// Analytics: Management
// ============================================
clearAnalytics(type?: 'pageviews' | 'interactions' | 'sessions', beforeDate?: Date): void {
const tables = type ? [type] : ['pageviews', 'interactions', 'sessions'];
for (const table of tables) {
if (beforeDate) {
this.db.prepare(`DELETE FROM ${table} WHERE timestamp < ? OR created_at < ?`)
.run(beforeDate.toISOString(), beforeDate.toISOString());
} else {
this.db.prepare(`DELETE FROM ${table}`).run();
}
}
}
getAnalyticsStorageInfo(): {
pageviewCount: number;
interactionCount: number;
sessionCount: number;
estimatedSizeKB: number;
} {
const pageviewCount = (this.db.prepare('SELECT COUNT(*) as count FROM pageviews').get() as { count: number }).count;
const interactionCount = (this.db.prepare('SELECT COUNT(*) as count FROM interactions').get() as { count: number }).count;
const sessionCount = (this.db.prepare('SELECT COUNT(*) as count FROM sessions').get() as { count: number }).count;
const estimatedSizeKB = Math.round((pageviewCount * 200 + interactionCount * 150 + sessionCount * 100) / 1024);
return {
pageviewCount,
interactionCount,
sessionCount,
estimatedSizeKB,
};
}
exportAnalyticsData(type: 'all' | 'pageviews' | 'interactions' | 'sessions' = 'all'): {
pageviews?: Array<Record<string, unknown>>;
interactions?: Array<Record<string, unknown>>;
sessions?: Array<Record<string, unknown>>;
} {
const data: {
pageviews?: Array<Record<string, unknown>>;
interactions?: Array<Record<string, unknown>>;
sessions?: Array<Record<string, unknown>>;
} = {};
if (type === 'all' || type === 'pageviews') {
data.pageviews = this.db.prepare(`
SELECT id, page_path, referrer, country, user_agent, session_id,
load_time, device_type, timestamp
FROM pageviews
ORDER BY timestamp DESC
`).all() as Array<Record<string, unknown>>;
}
if (type === 'all' || type === 'interactions') {
data.interactions = this.db.prepare(`
SELECT id, session_id, page_path, interaction_type, element_selector,
coordinates, scroll_depth, time_on_page, timestamp
FROM interactions
ORDER BY timestamp DESC
`).all() as Array<Record<string, unknown>>;
}
if (type === 'all' || type === 'sessions') {
data.sessions = this.db.prepare(`
SELECT id, session_id, entry_page, exit_page, page_count,
duration, is_bounce, created_at, ended_at
FROM sessions
ORDER BY created_at DESC
`).all() as Array<Record<string, unknown>>;
}
return data;
}
}
|