ReviewOS

also looking at this

stacks/analyticshq

Harden AnalyticsHQ release and dashboard quality

#52
Merged glennmichael123 wants to merge docs/bunpress-product-guide into main
34 files +1236 -340
routes/analytics.tsmodified+7-19
Changes to routes/analytics.ts
@@ -20,6 +20,7 @@ import { formatCount, renderBadge, renderSparkline, sanitizeLabel } from '../app
2020import { ASSIGNABLE_ROLES, isAssignableRole, listSiteMembers, resolveSiteRole, satisfies, siteExists, type SiteRole } from '../app/Analytics/access'
2121import { ALERT_CONDITIONS, ALERT_METRICS, isAlertCondition, isAlertMetric, isRelative } from '../app/Analytics/alerts'
2222import { planForSite } from '../app/Analytics/entitlements'
23import { serializeEventProperties } from '../app/Analytics/event-properties'
2324import { expiryFrom, hashToken, inviteRefusal, looksLikeEmail, mintToken, normalizeEmail } from '../app/Analytics/invites'
2425import { sendSiteInvite } from '../app/Mail/SiteInvite'
2526import { featureUnavailableMessage, isUnlimited, limitReachedMessage, type PlanFeatures, type PlanLimits } from '../config/plans'
@@ -551,25 +552,12 @@ route.post('/collect', async (request: any) => {
551552 }).execute()
552553 }
553554 else {
554 // Reserved auto-tracked events (Outbound Link / File Download) carry only a url. Store
555 // it canonically as {"url":...} regardless of any extra keys / key-order a caller sends,
556 // so the dashboard's GROUP BY properties aggregates exactly one row per URL — a client
557 // can't split or pollute a URL's row by appending junk keys.
558 let props = body.p ? JSON.stringify(body.p) : null
559 if ((String(event) === 'Outbound Link' || String(event) === 'File Download') && body.p && body.p.url) {
560 let url = String(body.p.url)
561 props = JSON.stringify({ url })
562 // properties is varchar(255): trim the url until the wrapped JSON fits, so it stays
563 // valid JSON (the dashboard JSON.parses it) and never overflows the column — on Postgres
564 // an over-length varchar insert errors (22001) and would 500 the beacon. Only pathologically
565 // long hrefs hit the loop. (TODO: widen custom_events.properties for full-length urls.)
566 while (props.length > 255 && url.length) {
567 url = url.slice(0, -8)
568 props = JSON.stringify({ url })
569 }
570 }
571 // .catch like the sites/sessions inserts above: a storage failure (e.g. an over-length
572 // non-reserved props blob under strict sql_mode) must never 500 the public beacon.
555 // Reserved auto-tracked events carry only their canonical URL, so grouping
556 // stays stable even when a caller sends extra keys in a different order.
557 // Optional metadata is bounded before it reaches the database.
558 const props = serializeEventProperties(event, body.p)
559 // .catch like the sites/sessions inserts above: a storage failure must
560 // never turn the public beacon into a 500 response.
573561 await db.insertInto('custom_events').values({
574562 id: randomId(),
575563 site_id: String(siteId),