98 lines
3.4 KiB
Dart
98 lines
3.4 KiB
Dart
import 'dart:convert';
|
|
|
|
import 'progress.dart';
|
|
import '../domain/postcards.dart';
|
|
|
|
/// Local diagnostics only. No device identifier, upload or attribution claim.
|
|
/// Keep currencies separate: an SDK USD receipt is never treated as EUR.
|
|
class PlayMetrics {
|
|
PlayMetrics(this.store, {DateTime Function()? clock})
|
|
: now = clock ?? DateTime.now;
|
|
final Progress store;
|
|
final DateTime Function() now;
|
|
Map<String, dynamic> get _data {
|
|
final saved = store.text('play_metrics_v1');
|
|
return saved.isEmpty
|
|
? <String, dynamic>{}
|
|
: Map<String, dynamic>.from(jsonDecode(saved) as Map);
|
|
}
|
|
|
|
void _update(void Function(Map<String, dynamic>) edit) {
|
|
final data = _data;
|
|
data.putIfAbsent('firstObservedUtc', () => now().toUtc().toIso8601String());
|
|
edit(data);
|
|
store.set('play_metrics_v1', jsonEncode(data));
|
|
}
|
|
|
|
void visit() => _update((data) {
|
|
final stamp = now().toUtc();
|
|
final last = DateTime.tryParse(data['lastVisitUtc'] as String? ?? '');
|
|
if (last == null || stamp.difference(last).inMinutes >= 30) {
|
|
data['sessions'] = (data['sessions'] as int? ?? 0) + 1;
|
|
}
|
|
data['lastVisitUtc'] = stamp.toIso8601String();
|
|
final days = Map<String, dynamic>.from(data['days'] as Map? ?? {});
|
|
days.putIfAbsent(
|
|
stamp.toIso8601String().substring(0, 10),
|
|
() => <String, dynamic>{},
|
|
);
|
|
data['days'] = days;
|
|
});
|
|
|
|
void event(String name) => _update((data) {
|
|
final days = Map<String, dynamic>.from(data['days'] as Map? ?? {});
|
|
final day = now().toUtc().toIso8601String().substring(0, 10);
|
|
final counts = Map<String, dynamic>.from(days[day] as Map? ?? {});
|
|
counts[name] = (counts[name] as int? ?? 0) + 1;
|
|
days[day] = counts;
|
|
data['days'] = days;
|
|
});
|
|
|
|
void endVisit() => _update((data) {
|
|
data['lastVisitUtc'] = now().toUtc().toIso8601String();
|
|
});
|
|
|
|
void paid({
|
|
required String format,
|
|
required double micros,
|
|
required String currency,
|
|
required String precision,
|
|
required bool test,
|
|
}) {
|
|
if (!micros.isFinite || micros < 0 || currency.isEmpty) return;
|
|
_update((data) {
|
|
final revenue = Map<String, dynamic>.from(
|
|
data['revenueMicros'] as Map? ?? {},
|
|
);
|
|
final key =
|
|
'${test ? 'test' : 'configured'}:$format:$currency:$precision';
|
|
revenue[key] = (revenue[key] as num? ?? 0) + micros;
|
|
data['revenueMicros'] = revenue;
|
|
final byDay = Map<String, dynamic>.from(
|
|
data['dailyRevenueMicros'] as Map? ?? {},
|
|
);
|
|
final day = now().toUtc().toIso8601String().substring(0, 10);
|
|
final daily = Map<String, dynamic>.from(byDay[day] as Map? ?? {});
|
|
daily[key] = (daily[key] as num? ?? 0) + micros;
|
|
byDay[day] = daily;
|
|
data['dailyRevenueMicros'] = byDay;
|
|
});
|
|
event('${test ? 'test' : 'configured'}_${format}_paid_callbacks');
|
|
}
|
|
|
|
String export() => const JsonEncoder.withIndent(' ').convert({
|
|
'schema': 1,
|
|
'scope': 'This installation only; observation starts with this app version. No campaign or country attribution. Configured units may still be SDK test devices. SDK revenue can be estimated or absent; reconcile with AdMob.',
|
|
..._data,
|
|
'patternsCompleted': store.number('solved'),
|
|
'postcardsCompleted': islandPostcards
|
|
.where(
|
|
(card) => List.generate(
|
|
3,
|
|
(slot) => store.flag('done_${card.puzzleKey(slot)}'),
|
|
).every((done) => done),
|
|
)
|
|
.length,
|
|
});
|
|
}
|