Skip to content

Commit 9c5adb4

Browse files
authored
Feat/amplitude linking (#2568)
1 parent 8da0c60 commit 9c5adb4

2 files changed

Lines changed: 49 additions & 4 deletions

File tree

src/components/WalletConnection/ConnectWalletButton.tsx

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { Identify, identify, setUserId } from '@amplitude/analytics-browser';
12
import { Trans } from '@lingui/macro';
23
import { Button } from '@mui/material';
34
import { ConnectKitButton } from 'connectkit';
@@ -16,8 +17,8 @@ export interface ConnectWalletProps {
1617
}
1718

1819
export const ConnectWalletButton: React.FC<ConnectWalletProps> = ({ funnel, onClick }) => {
19-
const [trackEvent, walletType] = useRootStore(
20-
useShallow((store) => [store.trackEvent, store.walletType])
20+
const [trackEvent, walletType, account] = useRootStore(
21+
useShallow((store) => [store.trackEvent, store.walletType, store.account])
2122
);
2223

2324
// Track connection attempt duration
@@ -44,6 +45,18 @@ export const ConnectWalletButton: React.FC<ConnectWalletProps> = ({ funnel, onCl
4445
if (!isConnectingRef.current && previousConnectionState.current) {
4546
// Connection attempt ended
4647
const duration = connectionStartTime ? Date.now() - connectionStartTime : 0;
48+
49+
const walletAddress = account;
50+
if (walletAddress) {
51+
setUserId(walletAddress);
52+
53+
const identifyObj = new Identify()
54+
.set('wallet_connected', true)
55+
.set('wallet_type', walletType || 'unknown');
56+
57+
identify(identifyObj);
58+
}
59+
4760
if (isConnectedRef.current) {
4861
trackEvent(AUTH.WALLET_CONNECT_SUCCESS, {
4962
funnel,
@@ -62,7 +75,20 @@ export const ConnectWalletButton: React.FC<ConnectWalletProps> = ({ funnel, onCl
6275
}
6376

6477
previousConnectionState.current = isConnectingRef.current;
65-
}, [connectionStartTime, funnel, trackEvent, walletType]);
78+
}, [connectionStartTime, funnel, trackEvent, walletType, account]);
79+
80+
useEffect(() => {
81+
if (!account && walletType === undefined) {
82+
// Wallet disconnected - clear identity
83+
const identifyObj = new Identify().set('wallet_connected', false).unset('wallet_type');
84+
85+
identify(identifyObj);
86+
87+
trackEvent(AUTH.DISCONNECT_WALLET, {
88+
funnel,
89+
});
90+
}
91+
}, [account, walletType, funnel, trackEvent]);
6692

6793
return (
6894
<>

src/store/analyticsSlice.ts

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,24 @@
1-
import { init, setOptOut, track } from '@amplitude/analytics-browser';
1+
import { add, init, setOptOut, track } from '@amplitude/analytics-browser';
22
import { StateCreator } from 'zustand';
33

44
import { RootStore } from './root';
55

6+
// Plugin to ensure all events have app_context
7+
const createAppContextPlugin = (context: string) => ({
8+
name: 'app-context-plugin',
9+
type: 'enrichment' as const,
10+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
11+
execute: async (event: any) => {
12+
return {
13+
...event,
14+
event_properties: {
15+
...event.event_properties,
16+
app_context: context,
17+
},
18+
};
19+
},
20+
});
21+
622
const AMPLITUDE_API_KEY = process.env.NEXT_PUBLIC_AMPLITUDE_API_KEY || '';
723

824
export type TrackEventProperties = {
@@ -45,6 +61,7 @@ export const createAnalyticsSlice: StateCreator<
4561
walletAddress: get().account,
4662
market: properties.market ?? get().currentMarket,
4763
walletType: get().walletType,
64+
app_context: 'app', // Fallback in case plugin doesn't apply
4865
};
4966

5067
try {
@@ -77,6 +94,7 @@ export const createAnalyticsSlice: StateCreator<
7794
platform: true,
7895
},
7996
});
97+
add(createAppContextPlugin('app'));
8098
set({ eventsTrackingInitialized: true });
8199
}
82100

@@ -93,6 +111,7 @@ export const createAnalyticsSlice: StateCreator<
93111
platform: false,
94112
},
95113
});
114+
add(createAppContextPlugin('app'));
96115
set({ eventsTrackingInitialized: true });
97116
}
98117

0 commit comments

Comments
 (0)