( eventName: string, properties?: Record<string, any>, )
| 33 | } |
| 34 | |
| 35 | export function trackEvent( |
| 36 | eventName: string, |
| 37 | properties?: Record<string, any>, |
| 38 | ) { |
| 39 | try { |
| 40 | if (!posthog || typeof posthog.capture !== "function") { |
| 41 | console.warn(`PostHog not available for event: ${eventName}`); |
| 42 | return; |
| 43 | } |
| 44 | |
| 45 | posthog.capture(eventName, { ...properties, platform: "web" }); |
| 46 | |
| 47 | const metaEventMap: Record<string, string> = { |
| 48 | purchase_completed: "Purchase", |
| 49 | subscription_purchased: "Purchase", |
| 50 | user_signed_up: "CompleteRegistration", |
| 51 | }; |
| 52 | |
| 53 | const metaEventName = metaEventMap[eventName]; |
| 54 | if (metaEventName) { |
| 55 | const isSignup = eventName === "user_signed_up"; |
| 56 | const metaParameters = isSignup ? undefined : properties; |
| 57 | const eventId = isSignup |
| 58 | ? `signup_${posthog.get_distinct_id?.() ?? "unknown"}` |
| 59 | : undefined; |
| 60 | trackMetaEvent( |
| 61 | metaEventName, |
| 62 | metaParameters, |
| 63 | eventId ? { eventId } : undefined, |
| 64 | ); |
| 65 | } |
| 66 | } catch (error) { |
| 67 | console.error(`Error tracking event ${eventName}:`, error); |
| 68 | } |
| 69 | } |
no test coverage detected