|
1 | 1 | "use client"; |
2 | 2 |
|
3 | 3 | import { useThirdwebClient } from "@/constants/thirdweb.client"; |
4 | | -import posthog from "posthog-js"; |
| 4 | +import { usePostHog } from "posthog-js/react"; |
5 | 5 | import { useEffect } from "react"; |
6 | 6 | import { |
7 | 7 | useActiveAccount, |
@@ -32,48 +32,49 @@ export const PosthogIdentifierClient: React.FC<{ |
32 | 32 | client, |
33 | 33 | }); |
34 | 34 | const wallet = useActiveWallet(); |
| 35 | + const posthog = usePostHog(); |
35 | 36 |
|
36 | 37 | // legitimate use-case |
37 | 38 | // eslint-disable-next-line no-restricted-syntax |
38 | 39 | useEffect(() => { |
39 | | - if (wallet) { |
| 40 | + if (wallet && posthog && posthog.__loaded) { |
40 | 41 | const connector = walletIdToPHName[wallet.id] || wallet.id; |
41 | 42 | posthog.register({ connector }); |
42 | 43 | posthog.capture("wallet_connected", { connector }); |
43 | 44 | } |
44 | | - }, [wallet]); |
| 45 | + }, [wallet, posthog]); |
45 | 46 |
|
46 | 47 | // legitimate use-case |
47 | 48 | // eslint-disable-next-line no-restricted-syntax |
48 | 49 | useEffect(() => { |
49 | | - if (accountAddress) { |
| 50 | + if (accountAddress && posthog && posthog.__loaded) { |
50 | 51 | posthog.identify(accountAddress); |
51 | 52 | } |
52 | | - }, [accountAddress]); |
| 53 | + }, [accountAddress, posthog]); |
53 | 54 |
|
54 | 55 | // eslint-disable-next-line no-restricted-syntax |
55 | 56 | useEffect(() => { |
56 | | - if (accountId) { |
| 57 | + if (accountId && posthog && posthog.__loaded) { |
57 | 58 | posthog.identify(accountId); |
58 | 59 | } |
59 | | - }, [accountId]); |
| 60 | + }, [accountId, posthog]); |
60 | 61 |
|
61 | 62 | // legitimate use-case |
62 | 63 | // eslint-disable-next-line no-restricted-syntax |
63 | 64 | useEffect(() => { |
64 | | - if (chain?.id) { |
| 65 | + if (chain?.id && posthog && posthog.__loaded) { |
65 | 66 | posthog.unregister("network"); |
66 | 67 | posthog.register({ chain_id: chain?.id, ecosystem: "evm" }); |
67 | 68 | } |
68 | | - }, [chain?.id]); |
| 69 | + }, [chain?.id, posthog]); |
69 | 70 |
|
70 | 71 | // legitimate use-case |
71 | 72 | // eslint-disable-next-line no-restricted-syntax |
72 | 73 | useEffect(() => { |
73 | | - if (balance?.data?.displayValue) { |
| 74 | + if (balance?.data?.displayValue && posthog && posthog.__loaded) { |
74 | 75 | posthog.register({ balance: balance.data.displayValue }); |
75 | 76 | } |
76 | | - }, [balance]); |
| 77 | + }, [balance, posthog]); |
77 | 78 |
|
78 | 79 | return null; |
79 | 80 | }; |
0 commit comments