Skip to content

Commit d6cd062

Browse files
authored
feat(analytics): send PostHog event for analytics opt-in choice (#296)
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.
1 parent 7567a87 commit d6cd062

2 files changed

Lines changed: 25 additions & 0 deletions

File tree

pkg/analytics/posthog.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,30 @@ func GetOrCreateAnalyticsID() string {
9494
return settings.AnalyticsID
9595
}
9696

97+
// CaptureAnalyticsOptIn sends an event recording the user's analytics consent choice.
98+
// This is sent regardless of the user's choice so we can measure opt-in rates.
99+
func CaptureAnalyticsOptIn(optedIn bool) {
100+
anonID := GetOrCreateAnalyticsID()
101+
if anonID == "" {
102+
return
103+
}
104+
105+
c, err := getClient()
106+
if err != nil {
107+
return
108+
}
109+
110+
_ = c.Enqueue(posthog.Capture{
111+
DistinctId: anonID,
112+
Event: "analytics_opt_in",
113+
Properties: posthog.NewProperties().
114+
Set("opted_in", optedIn).
115+
Set("os", runtime.GOOS).
116+
Set("arch", runtime.GOARCH).
117+
Set("cli_version", version.Version),
118+
})
119+
}
120+
97121
// IdentifyUser links the anonymous analytics ID to a real user ID using PostHog Alias.
98122
func IdentifyUser(userID string) {
99123
enabled, asked := IsAnalyticsEnabled()

pkg/cmd/login/login.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,7 @@ func (o LoginOptions) handleOnboarding(user *entity.User, _ *terminal.Terminal)
241241
})
242242
optIn := strings.HasPrefix(choice, "Yes")
243243
_ = analytics.SetAnalyticsPreference(optIn)
244+
analytics.CaptureAnalyticsOptIn(optIn)
244245
}
245246

246247
analytics.IdentifyUser(user.ID)

0 commit comments

Comments
 (0)