File size: 2,757 Bytes
ae238b3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
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);
};