Files
swiftcontrol/lib/utils/analytics/install_referrer_reporter.dart
Jonas Bark 351bb4ca63 fix(attribution): guard install-referrer report on settings init + document reporter ctor
Add core.settings.isInitialized to the Android install-referrer gate in
main.dart. Settings._prefs is a late SharedPreferences assigned during
Settings.init(); if SharedPreferences.getInstance() throws, _prefs is
never assigned. The referrer block previously read core.settings.prefs
unconditionally, so that failure would raise a LateInitializationError
from inside the guarded zone before runApp(BikeControlApp(error: error))
ran, skipping the intended error screen. Gating on isInitialized (already
used the same way in click_v2_onboarding.dart) skips the block instead.

Also add a one-line doc comment to the InstallReferrerReporter
constructor describing its injected source/prefs/send collaborators, to
match the class-level doc comment already present.

No logic changes in install_referrer_parser.dart.
2026-08-17 08:06:35 +02:00

66 lines
2.4 KiB
Dart

import 'package:bike_control/main.dart' show recordError;
import 'package:bike_control/utils/analytics/install_referrer_parser.dart';
import 'package:play_install_referrer/play_install_referrer.dart';
import 'package:shared_preferences/shared_preferences.dart';
/// Where the install referrer comes from. Abstracted so the reporter can be
/// tested without the Play Store plugin, which needs a real device.
abstract class InstallReferrerSource {
Future<String?> read();
}
/// Reads the referrer from the Google Play Install Referrer API.
class PlayInstallReferrerSource implements InstallReferrerSource {
@override
Future<String?> read() async {
try {
final details = await PlayInstallReferrer.installReferrer;
return details.installReferrer;
} catch (e, s) {
// Expected on sideloaded builds, emulators without Play Services, and
// when the referrer service is briefly unavailable. Recorded rather than
// swallowed, then treated as "no referrer" so the install still counts.
recordError(e, s, context: 'PlayInstallReferrerSource.read');
return null;
}
}
}
/// Reports the Play install referrer exactly once per installation.
///
/// Runs on Android only. The event lands in the same `analytics_events` table
/// the website writes to, so an install joins its originating ad click on
/// `utm_content`.
class InstallReferrerReporter {
/// [source] reads the raw referrer, [prefs] persists the once-only flag,
/// and [send] posts the resulting event body to analytics.
InstallReferrerReporter({required this.source, required this.prefs, required this.send});
final InstallReferrerSource source;
final SharedPreferences prefs;
/// Posts the event body to the `track-analytics` edge function. Injected so
/// tests do not need a Supabase client.
final Future<void> Function(Map<String, dynamic> body) send;
static const String reportedKey = 'install_referrer_reported';
Future<void> reportOnce() async {
if (prefs.getBool(reportedKey) ?? false) return;
final referrer = await source.read();
final utms = parseInstallReferrer(referrer);
await send(<String, dynamic>{
'eventType': 'app_install',
'downloadPlatform': 'android',
'referrerUrl': referrer,
...utms,
});
// Only after a successful send, so a failed report is retried next launch
// rather than being lost.
await prefs.setBool(reportedKey, true);
}
}