Rollups & Data Retention
diy-analytics keeps dashboards fast across years of traffic while keeping storage compact, through automated daily rollups and pruning.
Why daily rollups matter
Raw, unaggregated telemetry — individual pageviews, device user agents, custom event payloads — gives you granular audit trails, but querying millions of raw rows across a 12-month window slows dashboard rendering.
diy-analytics solves this with a two-tier storage architecture:
- Raw event tier (recent data). Pageviews and custom events are stored at full granularity for real-time inspection, journey flow analysis, and drilldowns.
- Rollup tier (long-range data). A background cron job compresses high-cardinality rows into daily pre-aggregated summaries. Long-range, multi-month charts query these rollups in under 10ms.
Cron configuration
The cron routine is configured in vercel.json and runs daily at 03:00 UTC:
What the job does
When /api/cron/rollup runs:
- Compresses the prior day. Aggregates all raw pageviews and custom events from the previous UTC day into pre-calculated daily summary rows.
- Prunes expired raw records. Deletes raw pageview rows older than
PAGEVIEW_RETENTION_DAYS(default: 90 days) and custom event rows older thanEVENT_RETENTION_DAYS(default: 90 days). - Preserves rollups permanently. Aggregated daily summaries are never deleted, so your year-over-year growth charts stay accurate indefinitely.
Securing the cron endpoint
Set CRON_SECRET in your environment variables to prevent unauthorized manual triggers:
On Vercel, Vercel Cron automatically passes this token in the Authorization: Bearer $CRON_SECRET header.
Triggering rollups manually
Useful after backfilling historical data:
The endpoint returns a JSON summary of processed records and execution duration:
Running the rollup manually re-aggregates and re-prunes the prior UTC day. Avoid triggering it more than once per day outside of a backfill.