()
| 8 | } |
| 9 | |
| 10 | export const useAnalytics = () => { |
| 11 | const { analyticsEnabled, trackEvent } = useConsentManager(); |
| 12 | const posthog = usePostHog(); |
| 13 | |
| 14 | const track = useCallback( |
| 15 | (event: AnalyticsEvent) => { |
| 16 | // Use the consent manager's trackEvent which handles two-tiered tracking |
| 17 | trackEvent(event.name, event.props); |
| 18 | }, |
| 19 | [trackEvent] |
| 20 | ); |
| 21 | |
| 22 | // Convenience methods for common events |
| 23 | const trackFileUpload = useCallback( |
| 24 | (result: any) => { |
| 25 | const eventData = { |
| 26 | source_file_type: result.sourceType, |
| 27 | is_streaming_import: result.isStreamingImport, |
| 28 | row_count: String(result.rowCount), |
| 29 | file_size: result.fileSize, |
| 30 | }; |
| 31 | |
| 32 | // Basic performance tracking (no consent needed) |
| 33 | track({ |
| 34 | name: "performance_metric", |
| 35 | props: { |
| 36 | metric_type: "file_upload", |
| 37 | ...eventData, |
| 38 | }, |
| 39 | }); |
| 40 | |
| 41 | // Enhanced tracking with consent |
| 42 | if (analyticsEnabled) { |
| 43 | track({ |
| 44 | name: "file_upload", |
| 45 | props: eventData, |
| 46 | }); |
| 47 | } |
| 48 | }, |
| 49 | [track, analyticsEnabled] |
| 50 | ); |
| 51 | |
| 52 | const trackQuery = useCallback( |
| 53 | (queryType: string, executionTime?: number) => { |
| 54 | // Basic performance metric (no consent needed) |
| 55 | track({ |
| 56 | name: "performance_metric", |
| 57 | props: { |
| 58 | metric_type: "query_execution", |
| 59 | query_type: queryType, |
| 60 | execution_time_ms: executionTime, |
| 61 | }, |
| 62 | }); |
| 63 | |
| 64 | // Enhanced tracking with consent |
| 65 | if (analyticsEnabled) { |
| 66 | track({ |
| 67 | name: "query_executed", |
no test coverage detected