Files
multica/server/internal/metrics/record_event_test.go
Bohan Jiang 3cb5dc3ad6 chore(analytics): retire redundant PostHog tracking (MUL-4127) (#4996)
* chore(analytics): retire redundant PostHog tracking (MUL-4127)

PostHog had become a chaotic, largely-unused second copy of data we already
query from the DB and Grafana. Remove the redundant instrumentation.

Server: every product event (signup, workspace_created, issue_created,
issue_executed, chat_message_sent, team_invite_*, onboarding_*, agent_created,
cloud_waitlist_joined, feedback_submitted, contact_sales_submitted,
squad_created, autopilot_created) is now in metricsOnlyEvents, so
metrics.RecordEvent still increments the Prometheus/Grafana counter but no longer
ships to PostHog. DB rows remain the source of truth. Runtime/autopilot/
agent_task lifecycle were already Prometheus-only.

Frontend: delete the PostHog-only funnel instrumentation — $pageview (+ web and
desktop trackers), download_intent_expressed/page_viewed/initiated, the
onboarding_started mirror, onboarding_runtime_path_selected/detected,
feedback_opened, and source_backfill_*. The source-backfill modal itself stays
(it PATCHes the questionnaire to the DB).

Kept on PostHog (frontend only): $exception autocapture and the
client_crash / client_unresponsive stability telemetry (no DB equivalent), plus
$identify/$set. captureSignupSource (attribution cookie) stays — it still feeds
the signup_source Prometheus label.

Verified: pnpm typecheck, pnpm lint (0 errors), vitest (core/views/web/desktop),
go test ./internal/analytics/... ./internal/metrics/...

Co-authored-by: multica-agent <github@multica.ai>

* docs(analytics): fix stale PostHog references after MUL-4127 (review follow-up)

Addresses review of #4996 — three spots still described server events as active
PostHog signals after they became metrics-only:

- docs/analytics.md: issue_executed is no longer a PostHog success signal; it is
  Prometheus-only (multica_issue_executed_total) + issue.first_executed_at, in
  both the event contract and the Reconciliation section.
- docs/analytics.md: the signup $set_once person properties (email, signup_source)
  are no longer emitted — signup is Prometheus-only; only the bucketed
  signup_source survives as the multica_signup_total label.
- server/internal/metrics/business_events.go: RecordEvent doc comment no longer
  claims it ships product events to PostHog / "PostHog is reserved for
  user/product-behaviour events" — every server event is now metrics-only.

Co-authored-by: multica-agent <github@multica.ai>

---------

Co-authored-by: J <j@multica.ai>
Co-authored-by: multica-agent <github@multica.ai>
2026-07-07 01:43:15 +08:00

53 lines
2.2 KiB
Go

package metrics_test
import (
"testing"
"github.com/multica-ai/multica/server/internal/analytics"
"github.com/multica-ai/multica/server/internal/metrics"
)
// captureSpy records the names of every event handed to Capture so tests can
// assert which events actually reach PostHog.
type captureSpy struct{ names []string }
func (c *captureSpy) Capture(e analytics.Event) { c.names = append(c.names, e.Name) }
func (c *captureSpy) Close() {}
// TestRecordEventSkipsPostHogForMetricsOnly verifies that events flagged by
// analytics.IsMetricsOnly increment a Prometheus counter but are NOT shipped to
// PostHog. As of MUL-4127 every server-side event is metrics-only, so none of
// them reach PostHog; only a name outside the set (a frontend event) would.
func TestRecordEventSkipsPostHogForMetricsOnly(t *testing.T) {
spy := &captureSpy{}
m := metrics.NewBusinessMetrics()
// Operational event: Prometheus counter moves, PostHog gets nothing.
before := metrics.SumAllCounters(m)
metrics.RecordEvent(spy, m, analytics.RuntimeOffline("user-1", "ws-1", "rt-1", "daemon-1", "claude"))
if len(spy.names) != 0 {
t.Fatalf("runtime_offline shipped %d events to PostHog, want 0: %v", len(spy.names), spy.names)
}
if metrics.SumAllCounters(m) <= before {
t.Fatalf("runtime_offline did not increment a Prometheus counter")
}
// Product-behaviour event: since MUL-4127 it is metrics-only too — the
// Prometheus counter still moves but nothing ships to PostHog.
before = metrics.SumAllCounters(m)
metrics.RecordEvent(spy, m, analytics.WorkspaceCreated("user-1", "ws-1"))
if len(spy.names) != 0 {
t.Fatalf("workspace_created shipped to PostHog, want 0 (metrics-only since MUL-4127): %v", spy.names)
}
if metrics.SumAllCounters(m) <= before {
t.Fatalf("workspace_created did not increment a Prometheus counter")
}
// Sanity-check the Capture path is still wired: a name outside the
// metrics-only set (i.e. a frontend event) is shipped to PostHog.
metrics.RecordEvent(spy, m, analytics.Event{Name: "frontend_only_probe", DistinctID: "user-1"})
if len(spy.names) != 1 || spy.names[0] != "frontend_only_probe" {
t.Fatalf("a non-metrics-only event should ship to PostHog exactly once: %v", spy.names)
}
}