Integrate user traits into event payloads
Extends the user traits (e.g., email) in dispatch events and integrates monitoring to identify users alongside emitting events. Additionally, ensures the analytics service captures these traits when identifying users.
This commit is contained in:
@@ -2,6 +2,7 @@
|
||||
|
||||
import { useCallback } from 'react';
|
||||
|
||||
import { useMonitoring } from '@kit/monitoring/hooks';
|
||||
import { useAppEvents } from '@kit/shared/events';
|
||||
import { useAuthChangeListener } from '@kit/supabase/hooks/use-auth-change-listener';
|
||||
|
||||
@@ -13,7 +14,9 @@ export function AuthProvider(props: React.PropsWithChildren) {
|
||||
useAuthChangeListener({
|
||||
appHomePath: pathsConfig.app.home,
|
||||
onEvent: (event, session) => {
|
||||
dispatchEvent(event, session?.user.id);
|
||||
dispatchEvent(event, session?.user.id, {
|
||||
email: session?.user.email ?? '',
|
||||
});
|
||||
},
|
||||
});
|
||||
|
||||
@@ -22,27 +25,36 @@ export function AuthProvider(props: React.PropsWithChildren) {
|
||||
|
||||
function useDispatchAppEventFromAuthEvent() {
|
||||
const { emit } = useAppEvents();
|
||||
const monitoring = useMonitoring();
|
||||
|
||||
return useCallback(
|
||||
(type: string, userId: string | undefined) => {
|
||||
(
|
||||
type: string,
|
||||
userId: string | undefined,
|
||||
traits: Record<string, string> = {},
|
||||
) => {
|
||||
switch (type) {
|
||||
case 'SIGNED_IN':
|
||||
emit({
|
||||
type: 'user.signedIn',
|
||||
payload: { userId: userId! },
|
||||
});
|
||||
if (userId) {
|
||||
emit({
|
||||
type: 'user.signedIn',
|
||||
payload: { userId, ...traits },
|
||||
});
|
||||
|
||||
monitoring.identifyUser({ id: userId, ...traits });
|
||||
}
|
||||
|
||||
break;
|
||||
|
||||
case 'USER_UPDATED':
|
||||
emit({
|
||||
type: 'user.updated',
|
||||
payload: { userId: userId! },
|
||||
payload: { userId: userId!, ...traits },
|
||||
});
|
||||
|
||||
break;
|
||||
}
|
||||
},
|
||||
[emit],
|
||||
[emit, monitoring],
|
||||
);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user