trackTranscription method

void trackTranscription({
  1. required String modelId,
  2. required int audioDurationMs,
  3. required int latencyMs,
  4. String? modelName,
  5. int? wordCount,
  6. double? confidence,
  7. String? language,
  8. bool isStreaming = false,
})

Track transcription

Implementation

void trackTranscription({
  required String modelId,
  required int audioDurationMs,
  required int latencyMs,
  String? modelName,
  int? wordCount,
  double? confidence,
  String? language,
  bool isStreaming = false,
}) {
  // Calculate real-time factor (RTF) - how fast transcription is vs audio length
  // RTF < 1 means faster than real-time
  final realTimeFactor = audioDurationMs > 0
      ? latencyMs / audioDurationMs
      : null;

  // Infer language from model ID if not provided (e.g., "whisper-tiny.en" → "en")
  String? detectedLanguage = language;
  if (detectedLanguage == null || detectedLanguage.isEmpty) {
    // Try to extract language from model ID (e.g., ".en", "-en", "_en")
    final langMatch = RegExp(r'[._-](en|zh|de|fr|es|ja|ko|ru|pt|it|nl|pl|ar|tr|sv|da|no|fi|cs|el|he|hu|id|ms|ro|th|uk|vi)$', caseSensitive: false).firstMatch(modelId);
    if (langMatch != null) {
      detectedLanguage = langMatch.group(1)?.toLowerCase();
    }
  }

  // Preserve original confidence value - don't fabricate estimates
  // Track source to let analytics distinguish model-provided vs unknown confidence
  final double? effectiveConfidence = confidence;
  // 'model' = model returned a value (including 0.0), 'unknown' = null/not provided
  final String confidenceSource = confidence != null ? 'model' : 'unknown';

  track(
    'transcription_completed',
    category: TelemetryCategory.stt,
    properties: {
      'model_id': modelId,
      'model_name': modelName,
      'audio_duration_ms': audioDurationMs,
      'latency_ms': latencyMs,
      'word_count': wordCount,
      'confidence': effectiveConfidence,
      'confidence_source': confidenceSource,
      'language': detectedLanguage,
      'real_time_factor': realTimeFactor,
      'is_streaming': isStreaming,
    },
  );
}