40 lines
1.2 KiB
Dart
40 lines
1.2 KiB
Dart
import 'dart:convert';
|
|
|
|
import 'package:hive_flutter/hive_flutter.dart';
|
|
import 'package:intl/intl.dart';
|
|
|
|
import '../local/hive_boxes.dart';
|
|
|
|
/// Lightweight local analytics sink for notification events.
|
|
class NotificationAnalyticsService {
|
|
NotificationAnalyticsService._();
|
|
static final NotificationAnalyticsService instance =
|
|
NotificationAnalyticsService._();
|
|
|
|
Box get _box => Hive.box(HiveBoxes.notificationRuntime);
|
|
|
|
Future<void> track(
|
|
String event, {
|
|
Map<String, dynamic> dimensions = const <String, dynamic>{},
|
|
}) async {
|
|
final date = DateFormat('yyyy-MM-dd').format(DateTime.now());
|
|
final counterKey = 'analytics.$date.$event';
|
|
final current = (_box.get(counterKey) as int?) ?? 0;
|
|
await _box.put(counterKey, current + 1);
|
|
|
|
// Keep a small rolling audit buffer for debug support.
|
|
final raw = (_box.get('analytics.recent') ?? '[]').toString();
|
|
final decoded = json.decode(raw);
|
|
final list = decoded is List ? decoded : <dynamic>[];
|
|
list.add({
|
|
'event': event,
|
|
'at': DateTime.now().toIso8601String(),
|
|
'dimensions': dimensions,
|
|
});
|
|
while (list.length > 100) {
|
|
list.removeAt(0);
|
|
}
|
|
await _box.put('analytics.recent', json.encode(list));
|
|
}
|
|
}
|