also looking at this
Harden AnalyticsHQ release and dashboard quality
#52
34 files
+1236
-340
| @@ -20,6 +20,7 @@ import { formatCount, renderBadge, renderSparkline, sanitizeLabel } from '../app | ||
| 20 | 20 | import { ASSIGNABLE_ROLES, isAssignableRole, listSiteMembers, resolveSiteRole, satisfies, siteExists, type SiteRole } from '../app/Analytics/access' |
| 21 | 21 | import { ALERT_CONDITIONS, ALERT_METRICS, isAlertCondition, isAlertMetric, isRelative } from '../app/Analytics/alerts' |
| 22 | 22 | import { planForSite } from '../app/Analytics/entitlements' |
| 23 | import { serializeEventProperties } from '../app/Analytics/event-properties' | |
| 23 | 24 | import { expiryFrom, hashToken, inviteRefusal, looksLikeEmail, mintToken, normalizeEmail } from '../app/Analytics/invites' |
| 24 | 25 | import { sendSiteInvite } from '../app/Mail/SiteInvite' |
| 25 | 26 | import { featureUnavailableMessage, isUnlimited, limitReachedMessage, type PlanFeatures, type PlanLimits } from '../config/plans' |
| @@ -551,25 +552,12 @@ route.post('/collect', async (request: any) => { | ||
| 551 | 552 | }).execute() |
| 552 | 553 | } |
| 553 | 554 | 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. | |
| 573 | 561 | await db.insertInto('custom_events').values({ |
| 574 | 562 | id: randomId(), |
| 575 | 563 | site_id: String(siteId), |