Implementation Plan - Phase 1: Views and Downloads Tracking (Dataset Page)
Implementation Plan - Phase 1: Views and Downloads Tracking (Dataset Page)
Implement daily tracking of dataset views (from Google Analytics) and downloads (first-party), and display total counts on dataset pages.
Proposed Changes
Database
[MODIFY] schema.prisma
- Add the
SiteStatmodel for daily aggregate tracking:
model SiteStat {
id String @id @default(cuid())
siteId String
date DateTime
views Int @default(0)
downloads Int @default(0)
site Site @relation(fields: [siteId], references: [id], onDelete: Cascade)
@@unique([siteId, date])
@@index([siteId])
@@index([date])
}
Tracking Logic
[MODIFY] route.tsx
- Add
upsertlogic to incrementdownloadsinSiteStatfor the current date whenever a file is requested.
[NEW] ga.ts
- Utility to fetch page view data from GA4 Reporting API.
[MODIFY] functions.ts
- Add Inngest function
syncGAStats(cron) to fetch previous day's views and updateSiteStat.
Frontend (Dataset Page Only)
[MODIFY] datapackage.tsx
- Aggregate
viewsanddownloadsfromSiteStatfor the given site and display them in the header area.
Verification Plan
Automated Tests
e2e/api.spec.ts: VerifySiteStatincrement on download.
Manual Verification
- Check dataset page for aggregated view/download counts.