mirror of
https://github.com/langgenius/dify.git
synced 2026-04-29 04:26:30 +08:00
Add Amplitude analytics integration and tracking (#27890)
- Introduced AmplitudeProvider component for initializing Amplitude analytics. - Integrated user tracking in AppContextProvider to report user and workspace information. - Added event tracking for workflow log filter selections. - Updated package.json and pnpm-lock.yaml to include @amplitude/unified dependency.
This commit is contained in:
parent
1416d71ece
commit
837b6cfc19
@ -8,6 +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'
|
||||||
dayjs.extend(quarterOfYear)
|
dayjs.extend(quarterOfYear)
|
||||||
|
|
||||||
const today = dayjs()
|
const today = dayjs()
|
||||||
@ -37,6 +38,9 @@ const Filter: FC<IFilterProps> = ({ queryParams, setQueryParams }: IFilterProps)
|
|||||||
value={queryParams.status || 'all'}
|
value={queryParams.status || 'all'}
|
||||||
onSelect={(item) => {
|
onSelect={(item) => {
|
||||||
setQueryParams({ ...queryParams, status: item.value as string })
|
setQueryParams({ ...queryParams, status: item.value as string })
|
||||||
|
trackEvent('workflow_log_filter_status_selected', {
|
||||||
|
workflow_log_filter_status: item.value as string,
|
||||||
|
})
|
||||||
}}
|
}}
|
||||||
onClear={() => setQueryParams({ ...queryParams, status: 'all' })}
|
onClear={() => setQueryParams({ ...queryParams, status: 'all' })}
|
||||||
items={[{ value: 'all', name: 'All' },
|
items={[{ value: 'all', name: 'All' },
|
||||||
|
|||||||
@ -14,14 +14,6 @@ export const trackEvent = (eventName: string, eventProperties?: Record<string, a
|
|||||||
* @param userId User ID
|
* @param userId User ID
|
||||||
*/
|
*/
|
||||||
export const setUserId = (userId: string) => {
|
export const setUserId = (userId: string) => {
|
||||||
if (!userId) {
|
|
||||||
console.warn('[Amplitude] ⚠️ Cannot set empty user ID')
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
if (process.env.NODE_ENV === 'development')
|
|
||||||
console.log('[Amplitude] 👤 Setting User ID:', userId)
|
|
||||||
|
|
||||||
amplitude.setUserId(userId)
|
amplitude.setUserId(userId)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -30,37 +22,11 @@ export const setUserId = (userId: string) => {
|
|||||||
* @param properties User properties
|
* @param properties User properties
|
||||||
*/
|
*/
|
||||||
export const setUserProperties = (properties: Record<string, any>) => {
|
export const setUserProperties = (properties: Record<string, any>) => {
|
||||||
// Filter out undefined and null values
|
|
||||||
const validProperties = Object.entries(properties).reduce((acc, [key, value]) => {
|
|
||||||
if (value !== undefined && value !== null)
|
|
||||||
acc[key] = value
|
|
||||||
|
|
||||||
return acc
|
|
||||||
}, {} as Record<string, any>)
|
|
||||||
|
|
||||||
if (Object.keys(validProperties).length === 0) {
|
|
||||||
if (process.env.NODE_ENV === 'development')
|
|
||||||
console.warn('[Amplitude] ⚠️ No valid properties to set')
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
if (process.env.NODE_ENV === 'development')
|
|
||||||
console.log('[Amplitude] 📊 Setting user properties:', validProperties)
|
|
||||||
|
|
||||||
const identifyEvent = new amplitude.Identify()
|
const identifyEvent = new amplitude.Identify()
|
||||||
Object.entries(validProperties).forEach(([key, value]) => {
|
Object.entries(properties).forEach(([key, value]) => {
|
||||||
identifyEvent.set(key, value)
|
identifyEvent.set(key, value)
|
||||||
})
|
})
|
||||||
|
amplitude.identify(identifyEvent)
|
||||||
const result = amplitude.identify(identifyEvent)
|
|
||||||
|
|
||||||
// Log the result in development
|
|
||||||
result.promise.then(() => {
|
|
||||||
if (process.env.NODE_ENV === 'development')
|
|
||||||
console.log('[Amplitude] ✅ User properties set successfully')
|
|
||||||
}).catch((err) => {
|
|
||||||
console.error('[Amplitude] ❌ Failed to set user properties:', err)
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@ -163,41 +163,28 @@ export const AppContextProvider: FC<AppContextProviderProps> = ({ children }) =>
|
|||||||
// #region Amplitude user tracking
|
// #region Amplitude user tracking
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
// Report user info to Amplitude when loaded
|
// Report user info to Amplitude when loaded
|
||||||
if (!userProfile?.id)
|
if (userProfile?.id) {
|
||||||
return
|
setUserId(userProfile.id)
|
||||||
|
setUserProperties({
|
||||||
// Step 1: Set User ID first
|
email: userProfile.email,
|
||||||
setUserId(userProfile.id)
|
name: userProfile.name,
|
||||||
|
has_password: userProfile.is_password_set,
|
||||||
// Step 2: Set user properties
|
})
|
||||||
const userProperties: 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])
|
||||||
|
|
||||||
// Step 3: Add workspace properties if available
|
useEffect(() => {
|
||||||
if (currentWorkspace?.id) {
|
// Report workspace info to Amplitude when loaded
|
||||||
userProperties.workspace_id = currentWorkspace.id
|
if (currentWorkspace?.id && userProfile?.id) {
|
||||||
userProperties.workspace_name = currentWorkspace.name
|
setUserProperties({
|
||||||
userProperties.workspace_plan = currentWorkspace.plan
|
workspace_id: currentWorkspace.id,
|
||||||
userProperties.workspace_status = currentWorkspace.status
|
workspace_name: currentWorkspace.name,
|
||||||
userProperties.workspace_role = currentWorkspace.role
|
workspace_plan: currentWorkspace.plan,
|
||||||
|
workspace_status: currentWorkspace.status,
|
||||||
|
workspace_role: currentWorkspace.role,
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
}, [currentWorkspace?.id, currentWorkspace?.name, currentWorkspace?.plan, currentWorkspace?.status, currentWorkspace?.role, userProfile?.id])
|
||||||
// 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
|
// #endregion Amplitude user tracking
|
||||||
|
|
||||||
return (
|
return (
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user