All files / routes track.ts

100% Statements 30/30
100% Branches 44/44
100% Functions 3/3
100% Lines 30/30

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                      1x     1x                                                                                 1x                                   8x 8x       8x           8x                                     8x             8x   8x       1x 11x 11x     11x 11x 5x 5x   5x     6x 6x 6x     6x 6x 8x         8x 8x   3x 3x             6x           5x          
// Batch Analytics Tracking Route (Public, no auth required)
import { Hono } from "hono";
import { z } from "zod";
import { ValidationError } from "../lib/errors";
import { handleError } from "../lib/response";
import { logger } from "../lib/logger";
 
type Env = {
	Bindings: CloudflareBindings;
};
 
const track = new Hono<Env>();
 
// Validation schema for individual tracking event
const trackingEventSchema = z.object({
	event_type: z.enum([
		"page_view",
		"click",
		"blog_view",
		"blog_pro_click",
		"blog_project_click",
	]),
	entity_type: z
		.enum(["pro", "project", "image", "blog"])
		.nullable()
		.optional(),
	entity_id: z.string().nullable().optional(),
	pro_id: z.string().nullable().optional(),
	blog_id: z.string().nullable().optional(),
	action: z.string().nullable().optional(),
	session_id: z.string().min(1),
	timestamp: z.number().int().positive(),
	page_url: z.string().optional(),
	page_path: z.string().optional(),
	referrer: z.string().nullable().optional(),
	referrer_domain: z.string().nullable().optional(),
	referrer_source: z.string().nullable().optional(),
	referrer_medium: z.string().nullable().optional(),
	utm_source: z.string().nullable().optional(),
	utm_medium: z.string().nullable().optional(),
	utm_campaign: z.string().nullable().optional(),
	utm_term: z.string().nullable().optional(),
	utm_content: z.string().nullable().optional(),
	source_category: z.string().nullable().optional(),
	device_type: z.string().nullable().optional(),
	browser: z.string().nullable().optional(),
	browser_version: z.string().nullable().optional(),
	os: z.string().nullable().optional(),
	screen_width: z.number().int().positive().nullable().optional(),
	screen_height: z.number().int().positive().nullable().optional(),
	viewport_width: z.number().int().positive().nullable().optional(),
	viewport_height: z.number().int().positive().nullable().optional(),
});
 
// Validation schema for batch request
const batchTrackingSchema = z.object({
	events: z.array(trackingEventSchema).min(1).max(10),
});
 
type TrackingEvent = z.infer<typeof trackingEventSchema>;
 
/**
 * Extract server-side enrichment data from Cloudflare headers
 */
async function enrichEventData(
	event: TrackingEvent,
	headers: Headers,
): Promise<{
	blobs: string[];
	doubles: number[];
	indexes: string[];
}> {
	// Extract Cloudflare headers for geo data
	const country = headers.get("cf-ipcountry") || "unknown";
	const city = decodeURIComponent(headers.get("cf-city") || "unknown");
 
	// Determine referrer source - prefer UTM params, then parsed referrer domain
	const referrerSource =
		event.utm_source ||
		event.source_category ||
		event.referrer_domain ||
		"direct";
 
	// Build blobs array (strings) - matches spec blob mapping
	const blobs = [
		event.event_type, // blob1 - event type
		event.entity_type || "unknown", // blob2 - entity type
		event.entity_id || "unknown", // blob3 - entity ID
		event.pro_id || "unknown", // blob4 - pro ID (for rollups)
		event.blog_id || "unknown", // blob5 - blog ID (for blog events)
		event.action || "view", // blob6 - action
		event.session_id, // blob7 - session
		referrerSource, // blob8 - traffic source
		event.utm_medium || event.source_category || "none", // blob9 - traffic medium
		event.device_type || "unknown", // blob10 - device type
		event.browser || "unknown", // blob11 - browser
		event.os || "unknown", // blob12 - OS
		country, // blob13 - country (server)
		city, // blob14 - city (server)
		event.page_url || event.page_path || "unknown", // blob15 - page URL
	];
 
	// Build doubles array (numbers)
	const doubles = [
		event.timestamp, // double1 - client timestamp
		event.screen_width || 0, // double2 - screen width
		event.viewport_width || 0, // double3 - viewport width
	];
 
	// Build indexes array (for querying) - pro_id for filtering
	const indexes = [event.pro_id || "unknown"];
 
	return { blobs, doubles, indexes };
}
 
// POST /api/track/batch - Submit batch of tracking events
track.post("/batch", async (c) => {
	try {
		const body = await c.req.json();
 
		// Validate request body
		const validationResult = batchTrackingSchema.safeParse(body);
		if (!validationResult.success) {
			const errors = validationResult.error.issues
				.map((e) => `${e.path.join(".")}: ${e.message}`)
				.join(", ");
			throw new ValidationError(`Invalid request: ${errors}`);
		}
 
		const { events } = validationResult.data;
		const analytics = c.env.ANALYTICS;
		const headers = c.req.raw.headers;
 
		// Process each event and write to Analytics Engine
		let failedCount = 0;
		for (const event of events) {
			const { blobs, doubles, indexes } = await enrichEventData(
				event,
				headers,
			);
 
			try {
				analytics.writeDataPoint({ blobs, doubles, indexes });
			} catch (err) {
				failedCount++;
				logger.error(
					`[track] writeDataPoint failed for event=${event.event_type} entity=${event.entity_id ?? "unknown"}:`,
					err instanceof Error ? err.message : err,
				);
			}
		}
 
		return c.json({
			ok: failedCount === 0,
			count: events.length - failedCount,
			...(failedCount > 0 && { failed: failedCount }),
		});
	} catch (err) {
		return handleError(c, err);
	}
});
 
export default track;