( userId: string, properties?: Record<string, unknown>, )
| 54 | } |
| 55 | |
| 56 | export function identifyUser( |
| 57 | userId: string, |
| 58 | properties?: Record<string, unknown>, |
| 59 | ) { |
| 60 | if (!key || !host) { |
| 61 | console.warn("Cannot identify user - missing key or host"); |
| 62 | return; |
| 63 | } |
| 64 | |
| 65 | try { |
| 66 | const currentId = posthog.get_distinct_id(); |
| 67 | const anonymousId = localStorage.getItem("anonymous_id"); |
| 68 | |
| 69 | if (currentId !== userId) { |
| 70 | if (anonymousId && currentId === anonymousId) { |
| 71 | console.log(`Aliasing user ${userId} from anonymous ID ${anonymousId}`); |
| 72 | posthog.alias(userId, anonymousId); |
| 73 | } |
| 74 | posthog.identify(userId); |
| 75 | if (properties) { |
| 76 | posthog.people.set(properties); |
| 77 | } |
| 78 | localStorage.removeItem("anonymous_id"); |
| 79 | console.log(`User identified: ${userId}`); |
| 80 | } else { |
| 81 | console.log(`User already identified as ${userId}`); |
| 82 | } |
| 83 | } catch (error) { |
| 84 | console.error("Error identifying user:", error); |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | export function trackEvent( |
| 89 | eventName: string, |
no test coverage detected