2023-10-31 17:42:25 +00:00
|
|
|
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 22:20:53 +00:00
|
|
|
void init() {
|
|
|
|
FlutterError.onError = (details) {
|
|
|
|
if (details.silent) return;
|
|
|
|
logCrash(details.exception, details.stack);
|
|
|
|
};
|
|
|
|
PlatformDispatcher.instance.onError = (error, stack) {
|
|
|
|
logCrash(error, stack);
|
|
|
|
return true;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2023-10-31 17:42:25 +00:00
|
|
|
Future<void> logEvent(
|
2024-01-27 22:20:53 +00:00
|
|
|
String eventName, {
|
2023-10-31 17:42:25 +00:00
|
|
|
Map<String, dynamic>? parameters,
|
|
|
|
}) async {
|
2024-01-27 22:20:53 +00:00
|
|
|
if (!kReleaseMode) {
|
|
|
|
log('<LightmeterAnalytics> logEvent: $eventName / $parameters');
|
2023-10-31 17:42:25 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
return _api.logEvent(
|
2024-01-27 22:20:53 +00:00
|
|
|
eventName,
|
2023-10-31 17:42:25 +00:00
|
|
|
parameters: parameters,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2024-01-27 22:20:53 +00:00
|
|
|
Future<void> logCrash(
|
|
|
|
dynamic exception,
|
|
|
|
StackTrace? stackTrace, {
|
|
|
|
dynamic reason,
|
|
|
|
Iterable<Object> information = const [],
|
|
|
|
}) async {
|
|
|
|
log(exception.toString(), stackTrace: stackTrace);
|
|
|
|
if (!kReleaseMode) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
return _api.logCrash(
|
|
|
|
exception,
|
|
|
|
stackTrace,
|
|
|
|
reason: reason,
|
|
|
|
information: information,
|
2023-10-31 17:42:25 +00:00
|
|
|
);
|
|
|
|
}
|
2024-01-27 22:20:53 +00:00
|
|
|
|
|
|
|
Future<void> setCustomKey(String key, String value) async => _api.setCustomKey(key, value);
|
2023-10-31 17:42:25 +00:00
|
|
|
}
|