feat(analytics): identify TMDB users by username via OpenPanel profileId
This commit is contained in:
1 parent
69814bcc68
commit
73eb8943b3
3 files changed
+41
-2
No files matched your search
@@ -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<string | undefined>(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;
|
||||||
@@ -44,10 +44,10 @@ const analytics = new Proxy({} as Analytics, {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
export const identify = (profileId: string) => {
|
export const identify = (profile: { profileId: string } & Record<string, unknown>) => {
|
||||||
if (!ready()) return;
|
if (!ready()) return;
|
||||||
|
|
||||||
op()?.('identify', { profileId });
|
op()?.('identify', profile);
|
||||||
};
|
};
|
||||||
|
|
||||||
export const clearIdentity = () => {
|
export const clearIdentity = () => {
|
||||||
|
|||||||
@@ -7,17 +7,25 @@ import { ReactQueryDevtools } from '@tanstack/react-query-devtools';
|
|||||||
import { ThemeProvider } from 'next-themes';
|
import { ThemeProvider } from 'next-themes';
|
||||||
|
|
||||||
import { Toaster } from '@/components/ui/toaster';
|
import { Toaster } from '@/components/ui/toaster';
|
||||||
|
import useIdentify from '@/hooks/useIdentify';
|
||||||
import getQueryClient from '@/lib/queryClient';
|
import getQueryClient from '@/lib/queryClient';
|
||||||
|
|
||||||
interface Props {
|
interface Props {
|
||||||
children: ReactNode;
|
children: ReactNode;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const Identify: FC = () => {
|
||||||
|
useIdentify();
|
||||||
|
|
||||||
|
return null;
|
||||||
|
};
|
||||||
|
|
||||||
const Providers: FC<Props> = ({ children }) => {
|
const Providers: FC<Props> = ({ children }) => {
|
||||||
const queryClient = getQueryClient();
|
const queryClient = getQueryClient();
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<QueryClientProvider client={queryClient}>
|
<QueryClientProvider client={queryClient}>
|
||||||
|
<Identify />
|
||||||
<ThemeProvider attribute='data-mode'>{children}</ThemeProvider>
|
<ThemeProvider attribute='data-mode'>{children}</ThemeProvider>
|
||||||
<ModalContainer exitTimeout={0} enterTimeout={0} />
|
<ModalContainer exitTimeout={0} enterTimeout={0} />
|
||||||
<Toaster />
|
<Toaster />
|
||||||
|
|||||||
Reference in new issue
Block a user