Skip to content

Commit

Permalink
fix: Ensure anonymousId is kept through every Segment identify call (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
luads authored Nov 28, 2024
1 parent 307be70 commit abc296a
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ export function ConnectLoader({
connectionStatus, deepLink, provider,
} = connectLoaderState;

const { identify } = useAnalytics();
const { identify, user } = useAnalytics();

const hasNoWalletProviderNameAndNoWeb3Provider = (localProvider?: Web3Provider): boolean => {
if (!walletProviderName && !localProvider) {
Expand Down Expand Up @@ -212,7 +212,9 @@ export function ConnectLoader({
try {
// WT-1698 Analytics - Identify user here then progress to widget
// TODO: Identify user should be separated out into a use Effect with only the provider (from connect loader state) as dependency
await identifyUser(identify, web3Provider!);
const userData = user ? await user() : undefined;
const anonymousId = userData?.anonymousId();
await identifyUser(identify, web3Provider!, { anonymousId });
} catch (err) {
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { isMetaMaskProvider, isPassportProvider } from '../provider';
* identifyUser - function to identify a user by their wallet address and call the function to raise analytics
* @param identify - The identify function from the useAnalytics() hook
* @param provider - the Web3Provider used to find the user's walletAddress
* @param options - additional options to pass to the identify function
*/
export async function identifyUser(
identify: (id: string, attributes: Record<string, any>, options?: Record<string, any>) => void,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ export default function ConnectWidget({
[viewState, viewDispatch],
);

const { identify, page } = useAnalytics();
const { identify, page, user } = useAnalytics();

let targetChain = targetChainId;
if (!targetChain) {
Expand Down Expand Up @@ -186,7 +186,11 @@ export default function ConnectWidget({
});
// Set up EIP-1193 provider event listeners for widget root instances
addProviderListenersForWidgetRoot(provider);
await identifyUser(identify, provider);

const userData = user ? await user() : undefined;
const anonymousId = userData?.anonymousId();

await identifyUser(identify, provider, { anonymousId });
sendProviderUpdatedEvent({ provider });

// Find the wallet provider info via injected with Passport and MetaMask fallbacks
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ export function WalletList(props: WalletListProps) {
connectState: { checkout },
} = useContext(ConnectContext);
const { viewDispatch } = useContext(ViewContext);
const { track, identify } = useAnalytics();
const { track, identify, user } = useAnalytics();
const { providers } = useInjectedProviders({ checkout });
const [showWalletDrawer, setShowWalletDrawer] = useState(false);
const { isWalletConnectEnabled, openWalletConnectModal } = useWalletConnect();
Expand Down Expand Up @@ -180,7 +180,10 @@ export function WalletList(props: WalletListProps) {

// Set up EIP-1193 provider event listeners for widget root instances
addProviderListenersForWidgetRoot(connectResult.provider);
await identifyUser(identify, connectResult.provider);

const userData = user ? await user() : undefined;
const anonymousId = userData?.anonymousId();
await identifyUser(identify, connectResult.provider, { anonymousId });

selectWeb3Provider(
web3Provider,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,9 @@ export function ReadyToConnect({ targetChainId, allowedChains }: ReadyToConnectP
const isPassport = isPassportProvider(provider);
const isMetaMask = isMetaMaskProvider(provider);

const { page, identify, track } = useAnalytics();
const {
page, identify, track, user,
} = useAnalytics();

useEffect(() => {
page({
Expand Down Expand Up @@ -145,7 +147,9 @@ export function ReadyToConnect({ targetChainId, allowedChains }: ReadyToConnectP
// Set up EIP-1193 provider event listeners for widget root instances
addProviderListenersForWidgetRoot(connectResult.provider);

await identifyUser(identify, connectResult.provider);
const userData = user ? await user() : undefined;
const anonymousId = userData?.anonymousId();
await identifyUser(identify, connectResult.provider, { anonymousId });

connectDispatch({
payload: {
Expand Down

0 comments on commit abc296a

Please sign in to comment.