|
| 1 | +// PostHog Analytics Utility |
| 2 | +// This provides type-safe wrapper functions for PostHog analytics |
| 3 | + |
| 4 | +declare global { |
| 5 | + interface Window { |
| 6 | + posthog: any; |
| 7 | + } |
| 8 | +} |
| 9 | + |
| 10 | +/** |
| 11 | + * Track a custom event in PostHog |
| 12 | + * @param eventName - Name of the event to track |
| 13 | + * @param properties - Optional properties to attach to the event |
| 14 | + */ |
| 15 | +export const trackEvent = (eventName: string, properties?: Record<string, any>) => { |
| 16 | + if (typeof window !== 'undefined' && window.posthog) { |
| 17 | + window.posthog.capture(eventName, properties); |
| 18 | + } |
| 19 | +}; |
| 20 | + |
| 21 | +/** |
| 22 | + * Identify a user in PostHog |
| 23 | + * @param userId - Unique identifier for the user |
| 24 | + * @param properties - Optional user properties |
| 25 | + */ |
| 26 | +export const identifyUser = (userId: string, properties?: Record<string, any>) => { |
| 27 | + if (typeof window !== 'undefined' && window.posthog) { |
| 28 | + window.posthog.identify(userId, properties); |
| 29 | + } |
| 30 | +}; |
| 31 | + |
| 32 | +/** |
| 33 | + * Reset the user identity (e.g., on logout) |
| 34 | + */ |
| 35 | +export const resetUser = () => { |
| 36 | + if (typeof window !== 'undefined' && window.posthog) { |
| 37 | + window.posthog.reset(); |
| 38 | + } |
| 39 | +}; |
| 40 | + |
| 41 | +/** |
| 42 | + * Set user properties |
| 43 | + * @param properties - User properties to set |
| 44 | + */ |
| 45 | +export const setUserProperties = (properties: Record<string, any>) => { |
| 46 | + if (typeof window !== 'undefined' && window.posthog) { |
| 47 | + window.posthog.setPersonProperties(properties); |
| 48 | + } |
| 49 | +}; |
| 50 | + |
| 51 | +/** |
| 52 | + * Check if a feature flag is enabled |
| 53 | + * @param flagKey - The feature flag key |
| 54 | + * @returns boolean indicating if the flag is enabled |
| 55 | + */ |
| 56 | +export const isFeatureEnabled = (flagKey: string): boolean => { |
| 57 | + if (typeof window !== 'undefined' && window.posthog) { |
| 58 | + return window.posthog.isFeatureEnabled(flagKey); |
| 59 | + } |
| 60 | + return false; |
| 61 | +}; |
| 62 | + |
| 63 | +/** |
| 64 | + * Get feature flag value |
| 65 | + * @param flagKey - The feature flag key |
| 66 | + * @returns The feature flag value |
| 67 | + */ |
| 68 | +export const getFeatureFlag = (flagKey: string): any => { |
| 69 | + if (typeof window !== 'undefined' && window.posthog) { |
| 70 | + return window.posthog.getFeatureFlag(flagKey); |
| 71 | + } |
| 72 | + return null; |
| 73 | +}; |
| 74 | + |
| 75 | +// Common event names for consistency |
| 76 | +export const Events = { |
| 77 | + // Authentication |
| 78 | + USER_LOGGED_IN: 'user_logged_in', |
| 79 | + USER_LOGGED_OUT: 'user_logged_out', |
| 80 | + USER_REGISTERED: 'user_registered', |
| 81 | + |
| 82 | + // Comments |
| 83 | + COMMENT_CREATED: 'comment_created', |
| 84 | + COMMENT_DELETED: 'comment_deleted', |
| 85 | + COMMENT_EDITED: 'comment_edited', |
| 86 | + COMMENT_REPLIED: 'comment_replied', |
| 87 | + |
| 88 | + // Page views |
| 89 | + PAGE_VIEWED: 'page_viewed', |
| 90 | + DASHBOARD_VIEWED: 'dashboard_viewed', |
| 91 | + |
| 92 | + // Interactions |
| 93 | + BUTTON_CLICKED: 'button_clicked', |
| 94 | + FORM_SUBMITTED: 'form_submitted', |
| 95 | + MODAL_OPENED: 'modal_opened', |
| 96 | + MODAL_CLOSED: 'modal_closed', |
| 97 | +} as const; |
0 commit comments