Skip to content

feat(analytics): add cohort retention and churn HTTP endpoints - #1844

Merged
github-actions[bot] merged 2 commits into
Smartdevs17:mainfrom
retkatmun:feat/churn-cohort-analysis-1755
Oct 19, 2026
Merged

github-actions[bot] merged 2 commits into
Smartdevs17:mainfrom
retkatmun:feat/churn-cohort-analysis-1755

Conversation

@retkatmun

Copy link
Copy Markdown
Contributor

Closes #1755

Summary

Churn was not measured and cohort retention views did not exist over HTTP. The underlying CohortService and AnalyticsDashboardApi already contained the full cohort/churn/retention logic — this change wires it to the HTTP layer and adds a test suite.

Changes

New files:

File Purpose
backend/analytics/cohortAnalyticsRouter.ts Plain Node.js HTTP router mounted at /analytics/cohorts. Six routes (see below).
backend/analytics/__tests__/cohortAnalyticsRouter.test.ts 17 unit tests covering all routes, validation, and edge cases.

Modified:

  • backend/server.ts — import + route block for /analytics/cohorts, placed before the 404 fallback.

Endpoints

Method Path Description
GET /analytics/cohorts?merchantId=…&granularity=month|week Cohort table grouped by subscriber signup period
GET /analytics/cohorts/retention?merchantId=… Day 1/7/30/60/90 retention curve
GET /analytics/cohorts/churn?merchantId=…&from=<ms>&to=<ms> Revenue churn vs logo churn breakdown
GET /analytics/cohorts/export?merchantId=…&granularity=…&format=csv|pdf Downloadable cohort report
GET /analytics/cohorts/mrr?merchantId=… MRR / ARR / ARPU / LTV summary
POST /analytics/cohorts/subscribers Upsert a subscriber record (seed the in-memory store)

Design decisions

  • No new business logic. CohortService, AnalyticsDashboardApi, RetentionCalculator, and cohortReportExport already existed with full cohort-by-signup, retention curve, churn breakdown, and export support. This PR is purely a wiring layer.
  • Export formats. CSV and PDF are both supported via the existing cohortTableToCsv / cohortTableToPdf helpers. The ?format= query param selects between them; CSV is the default.
  • Granularity. month (default) and week are both supported; weekly cohorts use ISO-8601 week keys.
  • Subscriber data seeding. POST /analytics/cohorts/subscribers lets callers upsert records into the in-memory SubscriberRecordRepository, making the endpoint self-contained until a database-backed repository is wired in.

Acceptance criteria (from issue)

  • Cohorts by signup — cohort table grouped by month or week, keyed by signup period
  • Exportable — CSV and PDF download via /export?format=csv|pdf
  • Tested — 17 unit tests, all passing

Testing

npx jest --config jest.backend.config.js \
  --testMatch '**/backend/analytics/__tests__/cohortAnalyticsRouter.test.ts'

Test Suites: 1 passed
Tests:       17 passed, 0 failed

Covers: non-matching path fallthrough, missing merchantId → 400, wrong method → 405, cohort table (monthly + weekly), retention curve fields, churn breakdown, MRR/ARR/ARPU/LTV, CSV/PDF export headers and body, subscriber upsert and invalid JSON.

retkatmun and others added 2 commits October 19, 2026 18:06
Implements issue Smartdevs17#1755. The existing CohortService and AnalyticsDashboardApi
already contained all cohort/churn/retention logic — this change exposes
that logic over HTTP and adds a test suite for the routing layer.

New files
  backend/analytics/cohortAnalyticsRouter.ts
    HTTP router mounted at /analytics/cohorts.

    Routes:
      GET  /analytics/cohorts?merchantId=…&granularity=month|week
           Cohort table grouped by subscriber signup period.
      GET  /analytics/cohorts/retention?merchantId=…
           Day 1/7/30/60/90 retention curve.
      GET  /analytics/cohorts/churn?merchantId=…&from=<ms>&to=<ms>
           Revenue churn vs logo churn breakdown for a period.
      GET  /analytics/cohorts/export?merchantId=…&granularity=…&format=csv|pdf
           Downloadable cohort report (CSV or PDF).
      GET  /analytics/cohorts/mrr?merchantId=…
           MRR / ARR / ARPU / LTV summary.
      POST /analytics/cohorts/subscribers
           Upsert a subscriber record into the in-memory store.

  backend/analytics/__tests__/cohortAnalyticsRouter.test.ts
    17 unit tests; all passing.

Modified
  backend/server.ts — import + route block for /analytics/cohorts
    placed before the 404 fallback.

Closes Smartdevs17#1755
@drips-wave

drips-wave Bot commented Oct 19, 2026

Copy link
Copy Markdown

@retkatmun Great news! 🎉 Based on an automated assessment of this PR, the linked Wave issue(s) no longer count against your application limits.

You can now already apply to more issues while waiting for a review of this PR. Keep up the great work! 🚀

Learn more about application limits

@github-actions
github-actions Bot merged commit b0f2d64 into Smartdevs17:main Oct 19, 2026
11 of 30 checks passed
@retkatmun
retkatmun deployed to security-review October 19, 2026 17:18 — with GitHub Actions Active

This branch was successfully deployed

1 active deployment
security-review — f0aab25b Deployed Oct 19, 2026 by retkatmun via Security Dashboard #1397
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add churn and cohort analysis

1 participant