trackEvent method

Future<void> trackEvent(
  1. TrackingEvent event
)

Implementation

Future<void> trackEvent(TrackingEvent event) async {
  if (_baseUrl == null || _companyId == null) {
    throw Exception('Service not initialized. Call initialize() first.');
  }

  try {
    // Get base URL without the templates endpoint
    final baseUri = Uri.parse(_baseUrl!);
    final baseUrl = baseUri.toString().replaceAll('/api/notifications/templates', '');

    // Construct tracking URL
    final trackingUrl = '$baseUrl$_trackingEndpoint';
    final uri = Uri.parse(trackingUrl);

    debugPrint('Tracking event: ${uri.toString()}');
    debugPrint('Event data: ${event.toJson()}');

    final response = await http.post(
      uri,
      headers: {'Content-Type': 'application/json'},
      body: jsonEncode(event.toJson()),
    );

    if (response.statusCode != 200) {
      throw Exception('Failed to track event: ${response.statusCode}');
    }
  } catch (e) {
    debugPrint('Error tracking event: $e');
  }
}