Repository navigation
perf: batch event/ad tracking into beacon requests - #172
Merged
Merged
Conversation
Impressions are now queued client-side (src/lib/tracking-queue.ts) and
flushed every 3s / at 25 items / on visibilitychange-hidden and pagehide
via navigator.sendBeacon (fetch keepalive fallback). Clicks flush
immediately. Per-session dedup semantics unchanged.
/api/events/track and /api/ads/track now accept either a single object
(legacy) or { events: [...] } (max 50, invalid items dropped, 413 over
cap) and do a single bulk insert.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
This branch was successfully deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Every impression used to fire its own POST (~10 on /kbw2026 load). Now they're batched.
Client — new
src/lib/tracking-queue.ts, used byevent-tracking.tsandad-tracking.ts:visibilitychange→ hidden /pagehidenavigator.sendBeaconusing anapplication/jsonBlob. Falls back tofetchwithkeepalive: truewhen sendBeacon is missing or returns falseevent_id:event_typefor all types; ads: impressions deduped, clicks always sent). visitor_id logic is unchanged. SSR-safe and never throwsServer —
/api/events/trackand/api/ads/track:{ events: [...] }with up to 50 itemsinserted: n). More than 50 items returns 413.insert(rows). The 42P01 graceful handling is keptreq.json()ignores content-type, so sendBeacon Blobs parse finesrc/lib/api-validation.ts(EventTrackSchema,AdTrackSchema) with newparseBatch/parseBatchBodyhelpersVerification
tsc --noEmit, eslint andnext buildpassnpm test: all 279 tests pass, including newsrc/lib/__tests__/tracking-queue.test.ts(batching, size-triggered flush, immediate click flush, visibility/pagehide flush, fetch fallback, dedup, batch schema parsing)next devat /kbw2026: before 9 events + 1 ads = 10 requests; after 1 + 1 = 2 requests (inserted: 9andinserted: 1rows confirmed)inserted: 1; a 51-item batch returns 413🤖 Generated with Claude Code