feat: Implement Cohort & Retention Analysis API (#623)#661
Merged
joelpeace48-cell merged 2 commits intoJun 18, 2026
Merged
Conversation
- Add database migration for user activities, cohort stats, and retention data - Implement cohort repository with complete data access layer - Create cohort service with deterministic retention calculations - Add REST API endpoints for cohort analysis and retention curves - Support multiple granularities (day, week, month) and metrics (claimed, active) - Include comprehensive unit tests with hand-computed expected values - All tests passing with deterministic fixture verification - TypeScript type checking and prettier formatting passing
|
@williams1604 is attempting to deploy a commit to the joelpeace48-cell's projects Team on Vercel. A member of the Team first needs to authorize it. |
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
48f665d
into
FinesseStudioLab:main
10 of 11 checks passed
4 tasks
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.
🎯 Overview
This PR implements a comprehensive cohort and retention analysis API for the Trivela platform, enabling campaign operators to answer questions like "of users who registered in week N, how many claimed by week N+k?"
🔗 Issue
Closes #623
✨ Features Implemented
1. Database Schema (Migration 011)
2. Data Access Layer
sqliteCohortRepository.js): Complete CRUD for cohort data3. Business Logic Service
4. REST API Endpoints
All under
/api/v1/campaigns/:campaignId/cohorts(API key required):Cohort Analysis:
GET /cohorts- Full cohort analysis with retention curvesgranularity(day/week/month),metric(claimed/active),recompute(bool)GET /cohorts/:cohortPeriod/retention- Specific cohort retention curvegranularity,metricRecomputation:
POST /cohorts/recompute- Force cache invalidation and recomputationgranularity,metricActivity Recording:
POST /activities- Record user activity (for testing/manual entry){ userAddress, activityType, occurredAt?, metadata? }5. Comprehensive Testing
📋 Files Changed
New Files
backend/src/db/migrations/011_cohort_retention_tables.js- Database schemabackend/src/dal/sqliteCohortRepository.js- Data access layer (241 lines)backend/src/services/cohortService.js- Business logic (377 lines)backend/src/routes/cohorts.js- API routes (208 lines)backend/src/services/cohortService.test.js- Unit tests (247 lines)IMPLEMENTATION_ISSUE_623.md- Complete documentationModified Files
backend/src/dal/index.js- Integrated cohort repositorybackend/src/index.js- Registered cohort service and routes🔄 Technical Highlights
Deterministic Cohort Assignment
Users assigned to cohorts based on registration timestamp:
Retention Offset Calculation
Retention Rate Formula
Period Handling
💡 Usage Example
✅ Testing & Verification
Deterministic Fixture Test
Hand-computed expected values verified:
Test Results
CI Checks
🎯 Acceptance Criteria
✅ A known fixture yields the expected cohort/retention curves
Implemented with deterministic test using hand-computed values. All retention rates match expected percentages with exact precision.
🔒 Edge Cases Handled
recomputeflag supported📊 Performance
🔐 Security
📚 Documentation
Complete documentation in
IMPLEMENTATION_ISSUE_623.mdincluding:🚀 Deployment
npm run db:migrate*Ready for reviewpush origin feat/cohort-retention-api-623 This implementation provides production-ready cohort and retention analysis with deterministic, testable outputs and statistical rigor.