Enhance Amplitude tracking by validating user ID and properties

- Updated middleware to include Amplitude domains.
- Improved setUserId function to handle empty user IDs and log warnings in development.
- Enhanced setUserProperties function to filter out invalid properties and log results in development.
- Refactored user tracking in AppContextProvider to streamline setting user and workspace properties.
This commit is contained in:
CodingOnStar
2025-11-07 11:22:26 +08:00
parent d7d0fad436
commit adf8ae79d5
3 changed files with 70 additions and 23 deletions

View File

@ -163,28 +163,41 @@ export const AppContextProvider: FC<AppContextProviderProps> = ({ children }) =>
// #region Amplitude user tracking
useEffect(() => {
// Report user info to Amplitude when loaded
if (userProfile?.id) {
setUserId(userProfile.id)
setUserProperties({
email: userProfile.email,
name: userProfile.name,
has_password: userProfile.is_password_set,
})
}
}, [userProfile?.id, userProfile?.email, userProfile?.name, userProfile?.is_password_set])
if (!userProfile?.id)
return
useEffect(() => {
// Report workspace info to Amplitude when loaded
if (currentWorkspace?.id && userProfile?.id) {
setUserProperties({
workspace_id: currentWorkspace.id,
workspace_name: currentWorkspace.name,
workspace_plan: currentWorkspace.plan,
workspace_status: currentWorkspace.status,
workspace_role: currentWorkspace.role,
})
// Step 1: Set User ID first
setUserId(userProfile.id)
// Step 2: Set user properties
const userProperties: Record<string, any> = {
email: userProfile.email,
name: userProfile.name,
has_password: userProfile.is_password_set,
}
}, [currentWorkspace?.id, currentWorkspace?.name, currentWorkspace?.plan, currentWorkspace?.status, currentWorkspace?.role, userProfile?.id])
// Step 3: Add workspace properties if available
if (currentWorkspace?.id) {
userProperties.workspace_id = currentWorkspace.id
userProperties.workspace_name = currentWorkspace.name
userProperties.workspace_plan = currentWorkspace.plan
userProperties.workspace_status = currentWorkspace.status
userProperties.workspace_role = currentWorkspace.role
}
// Set all properties at once
setUserProperties(userProperties)
}, [
userProfile?.id,
userProfile?.email,
userProfile?.name,
userProfile?.is_password_set,
currentWorkspace?.id,
currentWorkspace?.name,
currentWorkspace?.plan,
currentWorkspace?.status,
currentWorkspace?.role,
])
// #endregion Amplitude user tracking
return (