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,15 +2,15 @@
|
|||||||
|
|
||||||
import { useEffect } from 'react';
|
import { useEffect } from 'react';
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
import { usePathname, useSearchParams } from 'next/navigation';
|
import { usePathname, useSearchParams } from 'next/navigation';
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
import { analytics } from '@kit/analytics';
|
import { analytics } from '@kit/analytics';
|
||||||
import {
|
import { AppEvent, AppEventType, ConsumerProvidedEventTypes, useAppEvents } from '@kit/shared/events';
|
||||||
AppEvent,
|
|
||||||
AppEventType,
|
|
||||||
ConsumerProvidedEventTypes,
|
|
||||||
useAppEvents,
|
|
||||||
} from '@kit/shared/events';
|
|
||||||
|
|
||||||
type AnalyticsMapping<
|
type AnalyticsMapping<
|
||||||
T extends ConsumerProvidedEventTypes = NonNullable<unknown>,
|
T extends ConsumerProvidedEventTypes = NonNullable<unknown>,
|
||||||
@@ -48,10 +48,10 @@ function useAnalyticsMapping<T extends ConsumerProvidedEventTypes>(
|
|||||||
*/
|
*/
|
||||||
const analyticsMapping: AnalyticsMapping = {
|
const analyticsMapping: AnalyticsMapping = {
|
||||||
'user.signedIn': (event) => {
|
'user.signedIn': (event) => {
|
||||||
const userId = event.payload.userId;
|
const { userId, ...traits } = event.payload;
|
||||||
|
|
||||||
if (userId) {
|
if (userId) {
|
||||||
return analytics.identify(userId);
|
return analytics.identify(userId, traits);
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
'user.signedUp': (event) => {
|
'user.signedUp': (event) => {
|
||||||
|
|||||||
@@ -2,6 +2,7 @@
|
|||||||
|
|
||||||
import { useCallback } from 'react';
|
import { useCallback } from 'react';
|
||||||
|
|
||||||
|
import { useMonitoring } from '@kit/monitoring/hooks';
|
||||||
import { useAppEvents } from '@kit/shared/events';
|
import { useAppEvents } from '@kit/shared/events';
|
||||||
import { useAuthChangeListener } from '@kit/supabase/hooks/use-auth-change-listener';
|
import { useAuthChangeListener } from '@kit/supabase/hooks/use-auth-change-listener';
|
||||||
|
|
||||||
@@ -13,7 +14,9 @@ export function AuthProvider(props: React.PropsWithChildren) {
|
|||||||
useAuthChangeListener({
|
useAuthChangeListener({
|
||||||
appHomePath: pathsConfig.app.home,
|
appHomePath: pathsConfig.app.home,
|
||||||
onEvent: (event, session) => {
|
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() {
|
function useDispatchAppEventFromAuthEvent() {
|
||||||
const { emit } = useAppEvents();
|
const { emit } = useAppEvents();
|
||||||
|
const monitoring = useMonitoring();
|
||||||
|
|
||||||
return useCallback(
|
return useCallback(
|
||||||
(type: string, userId: string | undefined) => {
|
(
|
||||||
|
type: string,
|
||||||
|
userId: string | undefined,
|
||||||
|
traits: Record<string, string> = {},
|
||||||
|
) => {
|
||||||
switch (type) {
|
switch (type) {
|
||||||
case 'SIGNED_IN':
|
case 'SIGNED_IN':
|
||||||
emit({
|
if (userId) {
|
||||||
type: 'user.signedIn',
|
emit({
|
||||||
payload: { userId: userId! },
|
type: 'user.signedIn',
|
||||||
});
|
payload: { userId, ...traits },
|
||||||
|
});
|
||||||
|
|
||||||
|
monitoring.identifyUser({ id: userId, ...traits });
|
||||||
|
}
|
||||||
|
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case 'USER_UPDATED':
|
case 'USER_UPDATED':
|
||||||
emit({
|
emit({
|
||||||
type: 'user.updated',
|
type: 'user.updated',
|
||||||
payload: { userId: userId! },
|
payload: { userId: userId!, ...traits },
|
||||||
});
|
});
|
||||||
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
[emit],
|
[emit, monitoring],
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,8 +1,8 @@
|
|||||||
import { MonitoringService } from '@kit/monitoring-core';
|
import { MonitoringService } from '@kit/monitoring-core';
|
||||||
|
|
||||||
export class ConsoleMonitoringService implements MonitoringService {
|
export class ConsoleMonitoringService implements MonitoringService {
|
||||||
identifyUser() {
|
identifyUser(data: { id: string }) {
|
||||||
// noop
|
console.log(`[Console Monitoring] Identified user`, data);
|
||||||
}
|
}
|
||||||
|
|
||||||
captureException(error: Error) {
|
captureException(error: Error) {
|
||||||
|
|||||||
Reference in New Issue
Block a user