Spaces:
Running
on
A100
Running
on
A100
| import { CONSENT_COOKIE_NAME } from "./Analytics"; | |
| // Declare gtag global function | |
| declare global { | |
| interface Window { | |
| gtag: (...args: any[]) => void; | |
| } | |
| } | |
| // Check if analytics are enabled and user has consented | |
| const isAnalyticsEnabled = (): boolean => { | |
| if (import.meta.env.VITE_ENABLE_ANALYTICS !== 'true') { | |
| return false; | |
| } | |
| // Check localStorage first (works in iframes like HuggingFace), then cookies | |
| try { | |
| const localStorageValue = localStorage.getItem(CONSENT_COOKIE_NAME); | |
| if (localStorageValue === "true") return true; | |
| } catch (e) {} | |
| return document.cookie.includes(`${CONSENT_COOKIE_NAME}=true`); | |
| }; | |
| // Send GA4 event using gtag | |
| export const sendGAEvent = ( | |
| eventCategory: string, | |
| eventAction: string, | |
| eventLabel?: string, | |
| value?: number | |
| ) => { | |
| if (!isAnalyticsEnabled() || !window.gtag) { | |
| return; | |
| } | |
| try { | |
| // Use gtag event format for GA4 | |
| const eventName = `${eventCategory.toLowerCase()}_${eventAction.toLowerCase()}`; | |
| const eventParams: any = { | |
| event_category: eventCategory, | |
| event_label: eventLabel, | |
| }; | |
| if (value !== undefined) { | |
| eventParams.value = value; | |
| } | |
| window.gtag('event', eventName, eventParams); | |
| } catch (error) { | |
| console.error("GA Event Error:", error); | |
| } | |
| }; | |
| // Predefined event functions for common actions | |
| export const trackTranscriptionStart = (languageCode: string) => { | |
| sendGAEvent("Transcription", "start", languageCode); | |
| }; | |
| export const trackTranscriptionComplete = (languageCode: string, duration?: number) => { | |
| sendGAEvent("Transcription", "complete", languageCode, duration); | |
| }; | |
| export const trackTranscriptionError = (languageCode: string, errorMessage?: string) => { | |
| sendGAEvent("Transcription", "error", `${languageCode}${errorMessage ? ` - ${errorMessage}` : ''}`); | |
| }; | |
| export const trackFileUpload = (fileType: string, fileSizeMB?: number) => { | |
| sendGAEvent("File", "upload", fileType, fileSizeMB); | |
| }; | |
| export const trackLanguageChange = (languageCode: string) => { | |
| sendGAEvent("Language", "select", languageCode); | |
| }; | |
| export const trackDownloadSRT = (languageCode: string) => { | |
| sendGAEvent("Download", "srt", languageCode); | |
| }; | |
| export const trackDownloadVideoWithSubtitles = (languageCode: string) => { | |
| sendGAEvent("Download", "video_with_subtitles", languageCode); | |
| }; | |
| export const trackReset = () => { | |
| sendGAEvent("App", "reset"); | |
| }; | |
| export const trackWelcomeModalClose = () => { | |
| sendGAEvent("Modal", "welcome_close"); | |
| }; | |
| export const trackSegmentEdit = (languageCode: string, editType: "text" | "timing") => { | |
| sendGAEvent("Edit", editType, languageCode); | |
| }; | |
| export const trackSegmentDelete = (languageCode: string) => { | |
| sendGAEvent("Edit", "delete_segment", languageCode); | |
| }; | |