feat: add PostHog product analytics for key user actions (#110)
Tracks 9 key events: account_created, login_success, flow_viewed, session_started, session_completed, export_generated, ai_feature_used, psa_connected, session_shared. Identifies users on login, resets on logout. Autocapture disabled — only explicit discrete events. Set VITE_POSTHOG_KEY in environment to enable. Co-authored-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit was merged in pull request #110.
This commit is contained in:
@@ -3,6 +3,7 @@ import { persist } from 'zustand/middleware'
|
||||
import * as Sentry from '@sentry/react'
|
||||
import type { User, Token, UserCreate, UserLogin, Account, SubscriptionDetails } from '@/types'
|
||||
import { authApi } from '@/api/auth'
|
||||
import { identifyUser, resetAnalytics, analytics } from '@/lib/analytics'
|
||||
import { apiClient } from '@/api/client'
|
||||
import { clearCachedQuota } from '@/hooks/useCachedQuota'
|
||||
|
||||
@@ -49,6 +50,7 @@ export const useAuthStore = create<AuthState>()(
|
||||
|
||||
// Fetch user info
|
||||
await get().fetchUser()
|
||||
analytics.loginSuccess()
|
||||
} catch (error: unknown) {
|
||||
const axiosErr = error as { response?: { data?: { detail?: unknown } } }
|
||||
const rawDetail = axiosErr.response?.data?.detail
|
||||
@@ -62,6 +64,7 @@ export const useAuthStore = create<AuthState>()(
|
||||
set({ isLoading: true, error: null })
|
||||
try {
|
||||
await authApi.register(data)
|
||||
analytics.accountCreated()
|
||||
// After registration, log the user in
|
||||
await get().login({ email: data.email, password: data.password })
|
||||
} catch (error: unknown) {
|
||||
@@ -83,6 +86,7 @@ export const useAuthStore = create<AuthState>()(
|
||||
localStorage.removeItem('refresh_token')
|
||||
clearCachedQuota()
|
||||
Sentry.setUser(null)
|
||||
resetAnalytics()
|
||||
set({ user: null, token: null, account: null, subscription: null, isAuthenticated: false, error: null })
|
||||
}
|
||||
},
|
||||
@@ -109,6 +113,9 @@ export const useAuthStore = create<AuthState>()(
|
||||
// Set Sentry user context for error attribution
|
||||
Sentry.setUser({ id: user.id, email: user.email })
|
||||
|
||||
// Identify user in PostHog for product analytics
|
||||
identifyUser({ id: user.id, email: user.email, role: user.role, is_super_admin: user.is_super_admin, account_id: account?.id })
|
||||
|
||||
set({ user, account, subscription, isLoading: false })
|
||||
} catch (error: unknown) {
|
||||
const message = error instanceof Error ? error.message : 'Failed to fetch user'
|
||||
|
||||
Reference in New Issue
Block a user