m3_lightmeter/lib/data/analytics/analytics.dart

58 lines
1.3 KiB
Dart
Raw Normal View History

import 'dart:async';
import 'dart:developer';
import 'package:flutter/foundation.dart';
import 'package:lightmeter/data/analytics/api/analytics_api_interface.dart';
class LightmeterAnalytics {
final ILightmeterAnalyticsApi _api;
const LightmeterAnalytics({required ILightmeterAnalyticsApi api}) : _api = api;
2024-01-27 20:33:10 +00:00
void init() {
2024-01-27 21:03:00 +00:00
FlutterError.onError = (details) {
if (details.silent) return;
logCrash(details.exception, details.stack);
};
2024-01-27 20:33:10 +00:00
PlatformDispatcher.instance.onError = (error, stack) {
logCrash(error, stack);
return true;
};
}
Future<void> logEvent(
String eventName, {
Map<String, dynamic>? parameters,
}) async {
2024-01-27 20:33:10 +00:00
if (!kReleaseMode) {
log('<LightmeterAnalytics> logEvent: $eventName / $parameters');
return;
}
return _api.logEvent(
eventName,
parameters: parameters,
);
}
Future<void> logCrash(
dynamic exception,
2024-01-27 20:33:10 +00:00
StackTrace? stackTrace, {
dynamic reason,
Iterable<Object> information = const [],
}) async {
2024-01-27 20:33:10 +00:00
if (!kReleaseMode) {
return;
}
return _api.logCrash(
exception,
2024-01-27 20:33:10 +00:00
stackTrace,
reason: reason,
information: information,
);
}
2024-01-27 20:53:58 +00:00
Future<void> setCustomKey(String key, String value) async => _api.setCustomKey(key, value);
}