Files
js/components/wallets/PosthogIdentifier.tsx
T
Nacho Iacovino 3ff5df716c Init posthog cloud (#2098)
* Init posthog cloud

* posthog cloud public key

* Use dev key in dev

* Allow pg in script-src
2024-01-09 21:44:18 +00:00

54 lines
1.2 KiB
TypeScript

import posthog from "posthog-js-opensource";
import {
useAddress,
useBalance,
useChainId,
useWallet,
} from "@thirdweb-dev/react";
import React, { useEffect } from "react";
const walletIdToPHName: Record<string, string> = {
metamask: "metamask",
walletConnectV1: "WalletConnect",
walletConnectV2: "WalletConnect",
"paper-wallet": "Paper Wallet",
coinbaseWallet: "Coinbase Wallet",
injected: "Injected",
};
export const PosthogIdentifier: React.FC = () => {
const address = useAddress();
const chainId = useChainId();
const balance = useBalance();
const wallet = useWallet();
useEffect(() => {
if (wallet) {
const connector = walletIdToPHName[wallet.walletId] || wallet.walletId;
posthog.register({ connector });
posthog.capture("wallet_connected", { connector });
}
}, [wallet]);
useEffect(() => {
if (address) {
posthog.identify(address);
}
}, [address]);
useEffect(() => {
if (chainId) {
posthog.unregister("network");
posthog.register({ chain_id: chainId, ecosystem: "evm" });
}
}, [chainId]);
useEffect(() => {
if (balance?.data?.displayValue) {
posthog.register({ balance: balance.data.displayValue });
}
}, [balance]);
return null;
};