mirror of
https://github.com/langgenius/dify.git
synced 2026-04-29 20:48:01 +08:00
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:
parent
72e3aad6e1
commit
9a9935f018
@ -12,6 +12,7 @@ import { useProviderContext } from '@/context/provider-context'
|
|||||||
import { LogOut01 } from '@/app/components/base/icons/src/vender/line/general'
|
import { LogOut01 } from '@/app/components/base/icons/src/vender/line/general'
|
||||||
import PremiumBadge from '@/app/components/base/premium-badge'
|
import PremiumBadge from '@/app/components/base/premium-badge'
|
||||||
import { useLogout } from '@/service/use-common'
|
import { useLogout } from '@/service/use-common'
|
||||||
|
import { resetUser } from '@/app/components/base/amplitude/utils'
|
||||||
|
|
||||||
export type IAppSelector = {
|
export type IAppSelector = {
|
||||||
isMobile: boolean
|
isMobile: boolean
|
||||||
@ -28,6 +29,7 @@ export default function AppSelector() {
|
|||||||
await logout()
|
await logout()
|
||||||
|
|
||||||
localStorage.removeItem('setup_status')
|
localStorage.removeItem('setup_status')
|
||||||
|
resetUser()
|
||||||
// Tokens are now stored in cookies and cleared by backend
|
// Tokens are now stored in cookies and cleared by backend
|
||||||
|
|
||||||
router.push('/signin')
|
router.push('/signin')
|
||||||
|
|||||||
@ -148,8 +148,7 @@ const Apps = ({
|
|||||||
app_mode: mode,
|
app_mode: mode,
|
||||||
template_id: currApp?.app.id,
|
template_id: currApp?.app.id,
|
||||||
template_name: currApp?.app.name,
|
template_name: currApp?.app.name,
|
||||||
time: new Date().toLocaleString('zh-CN', { timeZone: 'Asia/Shanghai' }),
|
description,
|
||||||
has_description: description,
|
|
||||||
})
|
})
|
||||||
|
|
||||||
setIsShowCreateModal(false)
|
setIsShowCreateModal(false)
|
||||||
|
|||||||
@ -87,7 +87,6 @@ function CreateApp({ onClose, onSuccess, onCreateFromTemplate, defaultAppMode }:
|
|||||||
// Track app creation success
|
// Track app creation success
|
||||||
trackEvent('create_app', {
|
trackEvent('create_app', {
|
||||||
app_mode: appMode,
|
app_mode: appMode,
|
||||||
time: new Date().toLocaleString('zh-CN', { timeZone: 'Asia/Shanghai' }),
|
|
||||||
description,
|
description,
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|||||||
@ -8,7 +8,7 @@ import quarterOfYear from 'dayjs/plugin/quarterOfYear'
|
|||||||
import type { QueryParam } from './index'
|
import type { QueryParam } from './index'
|
||||||
import Chip from '@/app/components/base/chip'
|
import Chip from '@/app/components/base/chip'
|
||||||
import Input from '@/app/components/base/input'
|
import Input from '@/app/components/base/input'
|
||||||
import { trackEvent } from '../../base/amplitude/utils'
|
import { trackEvent } from '@/app/components/base/amplitude/utils'
|
||||||
dayjs.extend(quarterOfYear)
|
dayjs.extend(quarterOfYear)
|
||||||
|
|
||||||
const today = dayjs()
|
const today = dayjs()
|
||||||
|
|||||||
@ -12,11 +12,11 @@ export type IAmplitudeProps = {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const AmplitudeProvider: FC<IAmplitudeProps> = ({
|
const AmplitudeProvider: FC<IAmplitudeProps> = ({
|
||||||
apiKey = '702e89332ab88a7f14e665f417244e9d',
|
apiKey = process.env.NEXT_PUBLIC_AMPLITUDE_API_KEY ?? '',
|
||||||
sessionReplaySampleRate = 1,
|
sessionReplaySampleRate = 1,
|
||||||
}) => {
|
}) => {
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
// // Only enable in non-CE edition
|
// Only enable in non-CE edition
|
||||||
if (IS_CE_EDITION) {
|
if (IS_CE_EDITION) {
|
||||||
console.warn('[Amplitude] Amplitude is disabled in CE edition')
|
console.warn('[Amplitude] Amplitude is disabled in CE edition')
|
||||||
return
|
return
|
||||||
@ -39,7 +39,7 @@ const AmplitudeProvider: FC<IAmplitudeProps> = ({
|
|||||||
sampleRate: sessionReplaySampleRate,
|
sampleRate: sessionReplaySampleRate,
|
||||||
})
|
})
|
||||||
amplitude.add(sessionReplay)
|
amplitude.add(sessionReplay)
|
||||||
}, [apiKey, sessionReplaySampleRate])
|
}, [])
|
||||||
|
|
||||||
// This is a client component that renders nothing
|
// This is a client component that renders nothing
|
||||||
return null
|
return null
|
||||||
|
|||||||
@ -34,6 +34,7 @@ import { useGlobalPublicStore } from '@/context/global-public-context'
|
|||||||
import { useDocLink } from '@/context/i18n'
|
import { useDocLink } from '@/context/i18n'
|
||||||
import { useLogout } from '@/service/use-common'
|
import { useLogout } from '@/service/use-common'
|
||||||
import { ACCOUNT_SETTING_TAB } from '@/app/components/header/account-setting/constants'
|
import { ACCOUNT_SETTING_TAB } from '@/app/components/header/account-setting/constants'
|
||||||
|
import { resetUser } from '@/app/components/base/amplitude/utils'
|
||||||
|
|
||||||
export default function AppSelector() {
|
export default function AppSelector() {
|
||||||
const itemClassName = `
|
const itemClassName = `
|
||||||
@ -53,7 +54,7 @@ export default function AppSelector() {
|
|||||||
const { mutateAsync: logout } = useLogout()
|
const { mutateAsync: logout } = useLogout()
|
||||||
const handleLogout = async () => {
|
const handleLogout = async () => {
|
||||||
await logout()
|
await logout()
|
||||||
|
resetUser()
|
||||||
localStorage.removeItem('setup_status')
|
localStorage.removeItem('setup_status')
|
||||||
// Tokens are now stored in cookies and cleared by backend
|
// Tokens are now stored in cookies and cleared by backend
|
||||||
|
|
||||||
|
|||||||
@ -48,7 +48,6 @@ export default function CheckCode() {
|
|||||||
// Track login success event
|
// Track login success event
|
||||||
trackEvent('user_login_success', {
|
trackEvent('user_login_success', {
|
||||||
method: 'email_code',
|
method: 'email_code',
|
||||||
time: new Date().toLocaleString('zh-CN', { timeZone: 'Asia/Shanghai' }),
|
|
||||||
is_invite: !!invite_token,
|
is_invite: !!invite_token,
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|||||||
@ -67,7 +67,6 @@ export default function MailAndPasswordAuth({ isInvite, isEmailSetup, allowRegis
|
|||||||
// Track login success event
|
// Track login success event
|
||||||
trackEvent('user_login_success', {
|
trackEvent('user_login_success', {
|
||||||
method: 'email_password',
|
method: 'email_password',
|
||||||
time: new Date().toLocaleString('zh-CN', { timeZone: 'Asia/Shanghai' }),
|
|
||||||
is_invite: isInvite,
|
is_invite: isInvite,
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|||||||
@ -58,7 +58,6 @@ const ChangePasswordForm = () => {
|
|||||||
// Track registration success event
|
// Track registration success event
|
||||||
trackEvent('user_registration_success', {
|
trackEvent('user_registration_success', {
|
||||||
method: 'email',
|
method: 'email',
|
||||||
time: new Date().toLocaleString('zh-CN', { timeZone: 'Asia/Shanghai' }),
|
|
||||||
})
|
})
|
||||||
|
|
||||||
Toast.notify({
|
Toast.notify({
|
||||||
|
|||||||
@ -160,32 +160,27 @@ export const AppContextProvider: FC<AppContextProviderProps> = ({ children }) =>
|
|||||||
}, [currentWorkspace?.id])
|
}, [currentWorkspace?.id])
|
||||||
// #endregion Zendesk conversation fields
|
// #endregion Zendesk conversation fields
|
||||||
|
|
||||||
// #region Amplitude user tracking
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
// Report user info to Amplitude when loaded
|
// Report user and workspace info to Amplitude when loaded
|
||||||
if (userProfile?.id) {
|
if (userProfile?.id) {
|
||||||
setUserId(userProfile.email)
|
setUserId(userProfile.email)
|
||||||
setUserProperties({
|
const properties: Record<string, any> = {
|
||||||
email: userProfile.email,
|
email: userProfile.email,
|
||||||
name: userProfile.name,
|
name: userProfile.name,
|
||||||
has_password: userProfile.is_password_set,
|
has_password: userProfile.is_password_set,
|
||||||
})
|
}
|
||||||
}
|
|
||||||
}, [userProfile?.id, userProfile?.email, userProfile?.name, userProfile?.is_password_set])
|
|
||||||
|
|
||||||
useEffect(() => {
|
if (currentWorkspace?.id) {
|
||||||
// Report workspace info to Amplitude when loaded
|
properties.workspace_id = currentWorkspace.id
|
||||||
if (currentWorkspace?.id && userProfile?.id) {
|
properties.workspace_name = currentWorkspace.name
|
||||||
setUserProperties({
|
properties.workspace_plan = currentWorkspace.plan
|
||||||
workspace_id: currentWorkspace.id,
|
properties.workspace_status = currentWorkspace.status
|
||||||
workspace_name: currentWorkspace.name,
|
properties.workspace_role = currentWorkspace.role
|
||||||
workspace_plan: currentWorkspace.plan,
|
}
|
||||||
workspace_status: currentWorkspace.status,
|
|
||||||
workspace_role: currentWorkspace.role,
|
setUserProperties(properties)
|
||||||
})
|
|
||||||
}
|
}
|
||||||
}, [currentWorkspace?.id, currentWorkspace?.name, currentWorkspace?.plan, currentWorkspace?.status, currentWorkspace?.role, userProfile?.id])
|
}, [userProfile, currentWorkspace])
|
||||||
// #endregion Amplitude user tracking
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<AppContext.Provider value={{
|
<AppContext.Provider value={{
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user