From 5dd0efc58864ab4d921c71f36593b6f0b6abe7ed Mon Sep 17 00:00:00 2001 From: Alec Fong Date: Wed, 25 Feb 2026 21:34:15 -0800 Subject: [PATCH] feat(analytics): send PostHog event for analytics opt-in choice Capture an analytics_opt_in event when users choose to opt in or out of analytics during onboarding. The event bypasses the analytics enabled check so we can measure opt-in rates from both choices. --- pkg/analytics/posthog.go | 24 ++++++++++++++++++++++++ pkg/cmd/login/login.go | 1 + 2 files changed, 25 insertions(+) diff --git a/pkg/analytics/posthog.go b/pkg/analytics/posthog.go index 8ecc90b0..9d7b50cd 100644 --- a/pkg/analytics/posthog.go +++ b/pkg/analytics/posthog.go @@ -94,6 +94,30 @@ func GetOrCreateAnalyticsID() string { return settings.AnalyticsID } +// CaptureAnalyticsOptIn sends an event recording the user's analytics consent choice. +// This is sent regardless of the user's choice so we can measure opt-in rates. +func CaptureAnalyticsOptIn(optedIn bool) { + anonID := GetOrCreateAnalyticsID() + if anonID == "" { + return + } + + c, err := getClient() + if err != nil { + return + } + + _ = c.Enqueue(posthog.Capture{ + DistinctId: anonID, + Event: "analytics_opt_in", + Properties: posthog.NewProperties(). + Set("opted_in", optedIn). + Set("os", runtime.GOOS). + Set("arch", runtime.GOARCH). + Set("cli_version", version.Version), + }) +} + // IdentifyUser links the anonymous analytics ID to a real user ID using PostHog Alias. func IdentifyUser(userID string) { enabled, asked := IsAnalyticsEnabled() diff --git a/pkg/cmd/login/login.go b/pkg/cmd/login/login.go index a898b845..4f7991ab 100644 --- a/pkg/cmd/login/login.go +++ b/pkg/cmd/login/login.go @@ -241,6 +241,7 @@ func (o LoginOptions) handleOnboarding(user *entity.User, _ *terminal.Terminal) }) optIn := strings.HasPrefix(choice, "Yes") _ = analytics.SetAnalyticsPreference(optIn) + analytics.CaptureAnalyticsOptIn(optIn) } analytics.IdentifyUser(user.ID)