mirror of
https://github.com/jonasbark/swiftcontrol.git
synced 2026-11-29 09:11:17 +01:00
Uri.splitQueryString throws ArgumentError, not FormatException, for malformed percent-encoding (e.g. a truncated '%' or invalid escape) -- verified against the project's Dart SDK. The doc comment and Task 3's brief both promise FormatException so callers can catch a single type and route it through recordError. Catch the mismatch and rethrow as FormatException; add a test proving the contract holds.
47 lines
1.7 KiB
Dart
47 lines
1.7 KiB
Dart
/// The only UTM keys this product captures, matching `UTM_KEYS` in the
|
|
/// website's `src/utils/utm.ts`.
|
|
const List<String> utmKeys = <String>['utm_source', 'utm_medium', 'utm_campaign', 'utm_content'];
|
|
|
|
/// Matches `MAX_VALUE_LENGTH` on the website and `MAX_UTM_LENGTH` in the
|
|
/// track-analytics edge function, which truncates anything longer anyway.
|
|
const int _maxValueLength = 200;
|
|
|
|
/// Pulls UTM parameters out of a Google Play install referrer.
|
|
///
|
|
/// The referrer is a bare query string such as
|
|
/// `utm_source=facebook&utm_medium=paid`. Organic installs get Play's own
|
|
/// value (`utm_source=google-play&utm_medium=organic`), which is worth keeping
|
|
/// — it is how paid and organic installs are told apart.
|
|
///
|
|
/// Throws [FormatException] if the referrer contains malformed percent-encoding.
|
|
/// Callers must catch and route that through `recordError`.
|
|
Map<String, String> parseInstallReferrer(String? referrer) {
|
|
if (referrer == null || referrer.trim().isEmpty) return const <String, String>{};
|
|
|
|
final Map<String, String> params;
|
|
try {
|
|
params = Uri.splitQueryString(referrer);
|
|
} on FormatException {
|
|
rethrow;
|
|
} catch (e) {
|
|
// Uri.splitQueryString throws ArgumentError (not FormatException) for
|
|
// malformed percent-encoding, e.g. a truncated '%' or an invalid escape.
|
|
// Normalise to FormatException so callers can rely on a single type.
|
|
throw FormatException('Malformed install referrer: $referrer ($e)');
|
|
}
|
|
final result = <String, String>{};
|
|
|
|
for (final key in utmKeys) {
|
|
final raw = params[key];
|
|
if (raw == null) continue;
|
|
|
|
var value = raw.trim();
|
|
if (value.length > _maxValueLength) {
|
|
value = value.substring(0, _maxValueLength);
|
|
}
|
|
if (value.isNotEmpty) result[key] = value;
|
|
}
|
|
|
|
return result;
|
|
}
|