Skip to content

feat: add first-party Tinybird analytics - #2003

Open
richiemcilroy wants to merge 82 commits into
mainfrom
codex/first-party-analytics
Open

feat: add first-party Tinybird analytics#2003
richiemcilroy wants to merge 82 commits into
mainfrom
codex/first-party-analytics

Conversation

@richiemcilroy

@richiemcilroy richiemcilroy commented Jul 12, 2026

Copy link
Copy Markdown
Member

Summary

  • Add a strict, bounded first-party product analytics path for web, desktop, and mobile checkout attribution.
  • Replace PostHog completely with Tinybird-backed ingestion for signup, checkout, paid purchase, trial, organization expansion, and core recording events.
  • Remove PostHog clients, dependencies, bootstrap and feature-flag plumbing, environment variables, and CI/release secrets.
  • Add Tinybird Docker infrastructure, safe CI deployment, retry-safe aggregates, and read-only agent queries.

Validation

  • pnpm analytics:test — 152 tests passed
  • pnpm --filter @cap/web test --run __tests__/unit/mobile-checkout.test.ts — 7 tests passed
  • pnpm typecheck
  • cargo fmt --all
  • cargo check -p cap-desktop with the repository FFmpeg 7 toolchain
  • cargo test -p cap-desktop product_analytics --lib — 9 tests passed
  • Scoped Biome checks, diff checks, and a repository-wide PostHog residue scan passed

Greptile Summary

This PR replaces PostHog with first-party Tinybird product analytics. The main changes are:

  • Tinybird-backed web event ingestion and server-side analytics helpers.
  • Desktop and mobile checkout attribution updates.
  • Analytics CI, local Docker tooling, deploy checks, and Tinybird datafiles.
  • Removal of PostHog clients, dependencies, and environment wiring.

Confidence Score: 4/5

This is close, but the remaining replay path should be fixed before merging.

  • Anonymous web analytics can still be written with a copied browser token and spoofed request metadata.
  • The latest checks limit the accepted event names, but they do not bind the token to a server-verifiable browser or session.
  • The deploy workspace check now compares the token workspace id with the configured workspace id.

Files Needing Attention: apps/web/app/api/events/route.ts

Security Review

The anonymous analytics endpoint still accepts replayed browser tokens for allowlisted web events, which can let raw clients append fabricated anonymous product events.

Important Files Changed

Filename Overview
apps/web/app/api/events/route.ts Adds the product analytics ingestion route with browser-token, actor, rate-limit, normalization, and append logic.
apps/web/lib/analytics/request.ts Adds request metadata checks, anonymous event allowlisting, payload normalization, geo normalization, and fallback rate-limit keying.
apps/web/lib/analytics/browser-token.ts Adds signed, expiring browser analytics tokens for anonymous web ingestion.
scripts/analytics/tooling.js Adds Tinybird analytics validation, local tooling, cloud deploy planning, and workspace-id checks.
Prompt To Fix All With AI
### Issue 1
apps/web/app/api/events/route.ts:134-143
**Browser Token Replay**
This anonymous branch still accepts replayed browser tokens from raw HTTP clients. A visitor can receive a valid `cap_analytics_browser_token`, copy that cookie value, then post to `/api/events` for the next hour with `Origin: https://cap.so`, `Sec-Fetch-Site: same-origin`, and a payload whose `anonymousId` matches the token. The request has no authenticated actor, but this check still allows the batch as long as the event names are in the anonymous allowlist. Those rows are appended with attacker-controlled path, referrer, session, and properties, so anonymous funnel metrics can be polluted without a server-verifiable browser or session check.

---

For each issue above, determine whether it is valid and should be fixed. If so, fix it directly.

Reviews (13): Last reviewed commit: "fix: normalize Tinybird health query tim..." | Re-trigger Greptile

Greptile also left 1 inline comment on this PR.

Context used:

  • Context used - CLAUDE.md (source)
  • Context used - AGENTS.md (source)

@richiemcilroy
richiemcilroy marked this pull request as ready for review July 12, 2026 15:56
Comment thread apps/web/app/utils/product-analytics.ts Fixed
Comment thread scripts/analytics/tooling.js Fixed
Comment thread scripts/analytics/tooling.js Fixed
Comment thread apps/web/lib/analytics/request.ts Outdated
Comment thread apps/web/lib/analytics/request.ts
Comment thread scripts/analytics/tooling.js

@superagent-security superagent-security Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Superagent found 1 security concern(s).

Comment thread .github/workflows/analytics.yml Outdated
@superagent-security superagent-security Bot added the pr:flagged PR flagged for review by security analysis. label Jul 12, 2026
Comment thread apps/web/lib/analytics/request.ts Outdated
Comment thread scripts/analytics/tooling.js
@superagent-security superagent-security Bot removed the pr:flagged PR flagged for review by security analysis. label Jul 12, 2026
@richiemcilroy

Copy link
Copy Markdown
Member Author

hey @greptileai, please re-review the PR

Comment thread apps/web/lib/analytics/request.ts Outdated
@richiemcilroy

Copy link
Copy Markdown
Member Author

hey @greptileai, please re-review the PR

Comment thread apps/web/lib/analytics/request.ts
Comment thread apps/web/lib/analytics/request.ts
@richiemcilroy

Copy link
Copy Markdown
Member Author

hey @greptileai, please re-review the PR

@richiemcilroy

Copy link
Copy Markdown
Member Author

hey @greptileai, please re-review the PR

Comment thread apps/web/lib/analytics/request.ts Outdated
@superagent-security superagent-security Bot added the pr:flagged PR flagged for review by security analysis. label Jul 12, 2026
Comment thread apps/web/lib/analytics/request.ts
Comment thread apps/web/lib/analytics/request.ts
@superagent-security superagent-security Bot removed the pr:flagged PR flagged for review by security analysis. label Jul 12, 2026
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.

2 participants