Skip to content

feat(telemetry): attach org/project groups to all CLI events#5087

Open
seanoliver wants to merge 2 commits intodevelopfrom
sean/growth-761-cli-telemetry-project-org-groups
Open

feat(telemetry): attach org/project groups to all CLI events#5087
seanoliver wants to merge 2 commits intodevelopfrom
sean/growth-761-cli-telemetry-project-org-groups

Conversation

@seanoliver
Copy link
Copy Markdown
Contributor

Problem

Only ~19% of CLI events have PostHog group properties set ($group_0 for org, $group_1 for project). Groups are only written during supabase link, so any command using --project-ref without linking first sends events that are invisible to PostHog group analytics. This means we can't filter or aggregate ~80% of CLI activity by org or project.

Back in early April when CLI telemetry shipped, the plumbing for groups was built out (the CommandContext.Groups field, mergeGroups() layering, linkedProjectGroups() reader) but only the link flow actually populates the cache.

Changes

  • Add EnsureProjectGroupsCached() in internal/telemetry/project.go — when a project ref is available but no matching linked-project.json exists, fetches the project from the management API and caches it. Subsequent commands with the same ref skip the API call entirely.
  • Call it from Execute() in cmd/root.go right before firing cli_command_executed, so the existing linkedProjectGroups() reader picks up the cached data.
  • Best-effort throughout — errors log to debug and never break commands.
  • New test file with 8 cases covering cache hit, cache miss, ref change, API errors, and 404s.

Testing

  • All 25 telemetry tests pass (go test ./internal/telemetry/... -v)
  • Full build passes (go build ./...)
  • Verified the API response type (V1ProjectWithDatabaseResponse) matches what SaveLinkedProject expects

Closes GROWTH-761

Only ~19% of CLI events had PostHog group properties ($group_0, $group_1)
because groups were only set during `supabase link`. Commands using
--project-ref without linking sent events invisible to group analytics.

Add EnsureProjectGroupsCached which resolves and caches project metadata
(including org ID) in linked-project.json when a project ref is available.
The cache is checked before every cli_command_executed event, so the API
call only happens once per unique project ref.

Closes GROWTH-761
@seanoliver seanoliver requested a review from a team as a code owner April 15, 2026 21:20
- Guard against log.Fatalln crash: check auth token before calling
  GetSupabase(), and move the API call to cmd/root.go where it belongs
- Don't overwrite existing linked-project.json cache — supabase link
  is the authoritative source, we only fill the gap when no cache exists
- Fire GroupIdentify for org and project after caching, matching the
  link flow so PostHog has group metadata
- Restructure so telemetry package has no API dependencies (pure
  caching + PostHog calls), making tests reliable without gock/mocks
@coveralls
Copy link
Copy Markdown

Coverage Report for CI Build 24479669207

Coverage decreased (-0.09%) to 63.66%

Details

  • Coverage decreased (-0.09%) from the base build.
  • Patch coverage: 28 uncovered changes across 2 files (18 of 46 lines covered, 39.13%).
  • 5 coverage regressions across 1 file.

Uncovered Changes

File Changed Covered %
cmd/root.go 22 0 0.0%
internal/telemetry/project.go 24 18 75.0%

Coverage Regressions

5 previously-covered lines in 1 file lost coverage.

File Lines Losing Coverage Coverage
internal/utils/git.go 5 57.14%

Coverage Stats

Coverage Status
Relevant Lines: 15498
Covered Lines: 9866
Line Coverage: 63.66%
Coverage Strength: 7.0 hits per line

💛 - Coveralls

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