Skip to content

Commit 3081ec4

Browse files
QuantumlyyencryptedDegen
authored andcommitted
fix(analytics): self-heal posthog identify on distinct_id drift
The effect previously fired identify exactly once per lastAddress ref change. If posthog ever lost or rewrote its persistence (reset from elsewhere, re-init, SDK quirk) the refs would still hold the prior address and skip re-identifying. Compare posthog.get_distinct_id() against the wallet address on every run and re-identify when they diverge. Replace the unconditional __loaded early-return with a one-shot posthog.onFeatureFlags retry so an early effect tick doesn't silently drop the identify intent.
1 parent 6c367b3 commit 3081ec4

1 file changed

Lines changed: 37 additions & 22 deletions

File tree

src/components/posthog/posthog-identify.tsx

Lines changed: 37 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -10,35 +10,50 @@ export default function PostHogIdentify() {
1010
const lastConnector = useRef<string | undefined>(undefined)
1111

1212
useEffect(() => {
13-
if (!posthog.__loaded) return
14-
15-
if (isConnected && address) {
16-
const distinctId = address.toLowerCase()
17-
const connectorName = connector?.name
18-
19-
if (lastAddress.current !== distinctId) {
20-
if (lastAddress.current) posthog.reset()
21-
posthog.identify(distinctId, {
22-
wallet_address: distinctId,
23-
wallet_connector: connectorName,
24-
})
25-
lastAddress.current = distinctId
26-
lastConnector.current = connectorName
13+
const run = () => {
14+
if (isConnected && address) {
15+
const distinctId = address.toLowerCase()
16+
const connectorName = connector?.name
17+
const phId = posthog.get_distinct_id?.()
18+
const needsIdentify = lastAddress.current !== distinctId || phId !== distinctId
19+
20+
if (needsIdentify) {
21+
if (lastAddress.current && lastAddress.current !== distinctId) {
22+
posthog.reset()
23+
}
24+
posthog.identify(distinctId, {
25+
wallet_address: distinctId,
26+
wallet_connector: connectorName,
27+
})
28+
lastAddress.current = distinctId
29+
lastConnector.current = connectorName
30+
return
31+
}
32+
33+
if (lastConnector.current !== connectorName) {
34+
posthog.setPersonProperties({ wallet_connector: connectorName })
35+
lastConnector.current = connectorName
36+
}
2737
return
2838
}
2939

30-
if (lastConnector.current !== connectorName) {
31-
posthog.setPersonProperties({ wallet_connector: connectorName })
32-
lastConnector.current = connectorName
40+
if (lastAddress.current) {
41+
posthog.reset()
42+
lastAddress.current = undefined
43+
lastConnector.current = undefined
3344
}
34-
return
3545
}
3646

37-
if (lastAddress.current) {
38-
posthog.reset()
39-
lastAddress.current = undefined
40-
lastConnector.current = undefined
47+
if (posthog.__loaded) {
48+
run()
49+
return
4150
}
51+
52+
const off = posthog.onFeatureFlags(() => {
53+
off?.()
54+
run()
55+
})
56+
return () => off?.()
4257
}, [address, isConnected, connector?.name])
4358

4459
return null

0 commit comments

Comments
 (0)