From 8647c13896d1e092829bbd3228d548db629951cb Mon Sep 17 00:00:00 2001 From: gbuomprisco Date: Fri, 16 Aug 2024 11:00:37 +0200 Subject: [PATCH] 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. --- apps/web/components/analytics-provider.tsx | 16 +++++------ apps/web/components/auth-provider.tsx | 28 +++++++++++++------ .../core/src/console-monitoring.service.ts | 4 +-- 3 files changed, 30 insertions(+), 18 deletions(-) diff --git a/apps/web/components/analytics-provider.tsx b/apps/web/components/analytics-provider.tsx index 395c78f50..410bd6661 100644 --- a/apps/web/components/analytics-provider.tsx +++ b/apps/web/components/analytics-provider.tsx @@ -2,15 +2,15 @@ import { useEffect } from 'react'; + + import { usePathname, useSearchParams } from 'next/navigation'; + + import { analytics } from '@kit/analytics'; -import { - AppEvent, - AppEventType, - ConsumerProvidedEventTypes, - useAppEvents, -} from '@kit/shared/events'; +import { AppEvent, AppEventType, ConsumerProvidedEventTypes, useAppEvents } from '@kit/shared/events'; + type AnalyticsMapping< T extends ConsumerProvidedEventTypes = NonNullable, @@ -48,10 +48,10 @@ function useAnalyticsMapping( */ const analyticsMapping: AnalyticsMapping = { 'user.signedIn': (event) => { - const userId = event.payload.userId; + const { userId, ...traits } = event.payload; if (userId) { - return analytics.identify(userId); + return analytics.identify(userId, traits); } }, 'user.signedUp': (event) => { diff --git a/apps/web/components/auth-provider.tsx b/apps/web/components/auth-provider.tsx index 1cfec12d2..236e8104f 100644 --- a/apps/web/components/auth-provider.tsx +++ b/apps/web/components/auth-provider.tsx @@ -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 = {}, + ) => { 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], ); } diff --git a/packages/monitoring/core/src/console-monitoring.service.ts b/packages/monitoring/core/src/console-monitoring.service.ts index aa79817b4..48d1ca554 100644 --- a/packages/monitoring/core/src/console-monitoring.service.ts +++ b/packages/monitoring/core/src/console-monitoring.service.ts @@ -1,8 +1,8 @@ import { MonitoringService } from '@kit/monitoring-core'; export class ConsoleMonitoringService implements MonitoringService { - identifyUser() { - // noop + identifyUser(data: { id: string }) { + console.log(`[Console Monitoring] Identified user`, data); } captureException(error: Error) {