diff --git a/hooks/useIdentify.ts b/hooks/useIdentify.ts new file mode 100644 index 0000000..5892c30 --- /dev/null +++ b/hooks/useIdentify.ts @@ -0,0 +1,31 @@ +import { useEffect, useRef } from 'react'; + +import { identify } from '@/lib/analytics'; + +import useAccount from './useAccount'; + +const useIdentify = () => { + const account = useAccount(); + const sentRef = useRef(undefined); + + useEffect(() => { + if (!account) { + sentRef.current = undefined; + return; + } + + const profileId = `tmdb:${account.username || account.id}`; + + if (sentRef.current === profileId) return; + + sentRef.current = profileId; + + identify({ + profileId, + username: account.username, + name: account.name + }); + }, [account]); +}; + +export default useIdentify; diff --git a/lib/analytics/index.ts b/lib/analytics/index.ts index b377918..aa0cd55 100644 --- a/lib/analytics/index.ts +++ b/lib/analytics/index.ts @@ -44,10 +44,10 @@ const analytics = new Proxy({} as Analytics, { } }); -export const identify = (profileId: string) => { +export const identify = (profile: { profileId: string } & Record) => { if (!ready()) return; - op()?.('identify', { profileId }); + op()?.('identify', profile); }; export const clearIdentity = () => { diff --git a/providers/index.tsx b/providers/index.tsx index 2e44328..b740aa2 100644 --- a/providers/index.tsx +++ b/providers/index.tsx @@ -7,17 +7,25 @@ import { ReactQueryDevtools } from '@tanstack/react-query-devtools'; import { ThemeProvider } from 'next-themes'; import { Toaster } from '@/components/ui/toaster'; +import useIdentify from '@/hooks/useIdentify'; import getQueryClient from '@/lib/queryClient'; interface Props { children: ReactNode; } +const Identify: FC = () => { + useIdentify(); + + return null; +}; + const Providers: FC = ({ children }) => { const queryClient = getQueryClient(); return ( + {children}