Refactor user tracking and event logging across components

- Added resetUser function call on logout in avatar and account dropdown components to ensure user state is cleared.
- Removed redundant time logging from event tracking in various components to streamline data sent to Amplitude.
- Updated user properties reporting in AppContextProvider to consolidate user and workspace information into a single properties object.
This commit is contained in:
CodingOnStar 2025-12-03 11:10:54 +08:00
parent 72e3aad6e1
commit 9a9935f018
10 changed files with 22 additions and 29 deletions

View File

@ -12,6 +12,7 @@ import { useProviderContext } from '@/context/provider-context'
import { LogOut01 } from '@/app/components/base/icons/src/vender/line/general'
import PremiumBadge from '@/app/components/base/premium-badge'
import { useLogout } from '@/service/use-common'
import { resetUser } from '@/app/components/base/amplitude/utils'
export type IAppSelector = {
isMobile: boolean
@ -28,6 +29,7 @@ export default function AppSelector() {
await logout()
localStorage.removeItem('setup_status')
resetUser()
// Tokens are now stored in cookies and cleared by backend
router.push('/signin')

View File

@ -148,8 +148,7 @@ const Apps = ({
app_mode: mode,
template_id: currApp?.app.id,
template_name: currApp?.app.name,
time: new Date().toLocaleString('zh-CN', { timeZone: 'Asia/Shanghai' }),
has_description: description,
description,
})
setIsShowCreateModal(false)

View File

@ -87,7 +87,6 @@ function CreateApp({ onClose, onSuccess, onCreateFromTemplate, defaultAppMode }:
// Track app creation success
trackEvent('create_app', {
app_mode: appMode,
time: new Date().toLocaleString('zh-CN', { timeZone: 'Asia/Shanghai' }),
description,
})

View File

@ -8,7 +8,7 @@ import quarterOfYear from 'dayjs/plugin/quarterOfYear'
import type { QueryParam } from './index'
import Chip from '@/app/components/base/chip'
import Input from '@/app/components/base/input'
import { trackEvent } from '../../base/amplitude/utils'
import { trackEvent } from '@/app/components/base/amplitude/utils'
dayjs.extend(quarterOfYear)
const today = dayjs()

View File

@ -12,11 +12,11 @@ export type IAmplitudeProps = {
}
const AmplitudeProvider: FC<IAmplitudeProps> = ({
apiKey = '702e89332ab88a7f14e665f417244e9d',
apiKey = process.env.NEXT_PUBLIC_AMPLITUDE_API_KEY ?? '',
sessionReplaySampleRate = 1,
}) => {
useEffect(() => {
// // Only enable in non-CE edition
// Only enable in non-CE edition
if (IS_CE_EDITION) {
console.warn('[Amplitude] Amplitude is disabled in CE edition')
return
@ -39,7 +39,7 @@ const AmplitudeProvider: FC<IAmplitudeProps> = ({
sampleRate: sessionReplaySampleRate,
})
amplitude.add(sessionReplay)
}, [apiKey, sessionReplaySampleRate])
}, [])
// This is a client component that renders nothing
return null

View File

@ -34,6 +34,7 @@ import { useGlobalPublicStore } from '@/context/global-public-context'
import { useDocLink } from '@/context/i18n'
import { useLogout } from '@/service/use-common'
import { ACCOUNT_SETTING_TAB } from '@/app/components/header/account-setting/constants'
import { resetUser } from '@/app/components/base/amplitude/utils'
export default function AppSelector() {
const itemClassName = `
@ -53,7 +54,7 @@ export default function AppSelector() {
const { mutateAsync: logout } = useLogout()
const handleLogout = async () => {
await logout()
resetUser()
localStorage.removeItem('setup_status')
// Tokens are now stored in cookies and cleared by backend

View File

@ -48,7 +48,6 @@ export default function CheckCode() {
// Track login success event
trackEvent('user_login_success', {
method: 'email_code',
time: new Date().toLocaleString('zh-CN', { timeZone: 'Asia/Shanghai' }),
is_invite: !!invite_token,
})

View File

@ -67,7 +67,6 @@ export default function MailAndPasswordAuth({ isInvite, isEmailSetup, allowRegis
// Track login success event
trackEvent('user_login_success', {
method: 'email_password',
time: new Date().toLocaleString('zh-CN', { timeZone: 'Asia/Shanghai' }),
is_invite: isInvite,
})

View File

@ -58,7 +58,6 @@ const ChangePasswordForm = () => {
// Track registration success event
trackEvent('user_registration_success', {
method: 'email',
time: new Date().toLocaleString('zh-CN', { timeZone: 'Asia/Shanghai' }),
})
Toast.notify({

View File

@ -160,32 +160,27 @@ export const AppContextProvider: FC<AppContextProviderProps> = ({ children }) =>
}, [currentWorkspace?.id])
// #endregion Zendesk conversation fields
// #region Amplitude user tracking
useEffect(() => {
// Report user info to Amplitude when loaded
// Report user and workspace info to Amplitude when loaded
if (userProfile?.id) {
setUserId(userProfile.email)
setUserProperties({
const properties: Record<string, any> = {
email: userProfile.email,
name: userProfile.name,
has_password: userProfile.is_password_set,
})
}
}, [userProfile?.id, userProfile?.email, userProfile?.name, userProfile?.is_password_set])
}
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,
})
if (currentWorkspace?.id) {
properties.workspace_id = currentWorkspace.id
properties.workspace_name = currentWorkspace.name
properties.workspace_plan = currentWorkspace.plan
properties.workspace_status = currentWorkspace.status
properties.workspace_role = currentWorkspace.role
}
setUserProperties(properties)
}
}, [currentWorkspace?.id, currentWorkspace?.name, currentWorkspace?.plan, currentWorkspace?.status, currentWorkspace?.role, userProfile?.id])
// #endregion Amplitude user tracking
}, [userProfile, currentWorkspace])
return (
<AppContext.Provider value={{