mirror of
https://github.com/walkxcode/dashboard-icons.git
synced 2025-10-26 21:19:04 +08:00
feat: add PostHog utility functions for user identification
- Create centralized identifyUserInPostHog function - Add resetPostHogIdentity function for logout handling - Implement comprehensive person properties mapping - Follow PostHog best practices for user identification - Centralize PostHog logic to avoid code duplication
This commit is contained in:
45
web/src/lib/posthog-utils.ts
Normal file
45
web/src/lib/posthog-utils.ts
Normal file
@@ -0,0 +1,45 @@
|
||||
import { pb } from "./pb"
|
||||
|
||||
/**
|
||||
* Identifies a user in PostHog with their PocketBase user data
|
||||
* Follows PostHog best practices for user identification
|
||||
*
|
||||
* @param posthog - PostHog instance
|
||||
* @param user - PocketBase user model (optional, will use current auth if not provided)
|
||||
*/
|
||||
export function identifyUserInPostHog(posthog: any, user?: any) {
|
||||
if (!posthog) return
|
||||
|
||||
const userData = user || pb.authStore.model
|
||||
|
||||
if (!userData) return
|
||||
|
||||
// Use PocketBase user ID as distinct_id (unique string)
|
||||
// Pass all available person properties for complete profile
|
||||
posthog.identify(userData.id, {
|
||||
email: userData.email,
|
||||
username: userData.username,
|
||||
name: userData.username, // Use username as name if no separate name field
|
||||
created: userData.created,
|
||||
updated: userData.updated,
|
||||
admin: userData.admin || false,
|
||||
avatar: userData.avatar || null,
|
||||
// Add any other relevant user properties
|
||||
user_id: userData.id,
|
||||
email_verified: userData.emailVisibility || false,
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* Resets PostHog identity (should be called on logout)
|
||||
* This unlinks future events from the user
|
||||
*
|
||||
* @param posthog - PostHog instance
|
||||
*/
|
||||
export function resetPostHogIdentity(posthog: any) {
|
||||
if (!posthog) return
|
||||
|
||||
// Reset PostHog identity to unlink future events from this user
|
||||
// This is important for shared computers and follows PostHog best practices
|
||||
posthog.reset()
|
||||
}
|
||||
Reference in New Issue
Block a user