trySave method

Future<void> trySave({
  1. String iosRegion = '',
  2. Map<String, Object> installParamsExtra(
    1. Map<String, String> fields
    )?,
})

Call this method at the start of app

Implementation

Future<void> trySave({
  String iosRegion = '',
  Map<String, Object> Function(Map<String, String> fields)? installParamsExtra,
}) async {
  assert(!_isInitialized, 'Duplicate trySave call not needed');
  _isInitialized = true;

  try {
    final prefs = DSPrefs.I.internal;

    var referrer = prefs.getString(_referrerKey) ?? '';
    try {
      if (referrer.isEmpty && Platform.isAndroid) {
        // Get Android referrer
        try {
          referrer = await DSInternal.platform.invokeMethod('fetchInstallReferrer');
        } catch (e, stack) {
          Fimber.e('$e', stacktrace: stack);
          return;
        }
        if (referrer == 'null') return;
        await prefs.setString(_referrerKey, referrer);
      }

      if (referrer.isEmpty && Platform.isIOS) {
        assert(iosRegion.isNotEmpty, 'iosRegion should be assigned (get_referrer cloud function must be deployed)');
        // Get iOS referrer
        var referrer = 'null';
        try {
          final startTime = DateTime.timestamp();
          final res = await FirebaseFunctions.instanceFor(region: iosRegion).httpsCallable('get_referrer').call<
              String>();
          referrer = res.data;
          final loadTime = DateTime.timestamp().difference(startTime);
          DSMetrica.reportEvent('ios_referrer', attributes: {
            'value': referrer,
            'referrer_load_seconds': loadTime.inSeconds,
            'referrer_load_milliseconds': loadTime.inMilliseconds,
          });
          final p = referrer.indexOf('?');
          if (p >= 0) {
            referrer = referrer.substring(p + 1);
          }
        } catch (e, stack) {
          Fimber.e('ios_referrer $e', stacktrace: stack);
          referrer = 'err';
        }
        await prefs.setString(_referrerKey, referrer);
      }

      Fimber.i('ds_referrer=$referrer');

      if (!DSConstants.I.isInternalVersion) {
        if (isKnownReferrer()) {
          DSRemoteConfig.I.setPostfix('_r');
        } else {
          DSRemoteConfig.I.setPostfix('_e');
        }
      }

      // Send installs_full_referrer once. Only for Android platform
      if (prefs.getBool(_isSentKey) == true || !Platform.isAndroid) return;

      if (referrer == '') return;
      final data = Uri.splitQueryString(referrer);
      final utmSource = data['utm_source'];
      if (utmSource == null) return;

      final fid = await FirebaseInstallations.instance.getId();

      unawaited(FirebaseFirestore.instance.collection('installs_full_referrer').add({
        'bundle': DSConstants.I.packageInfo.packageName,
        'referrer': referrer,
        'referrer_len': referrer.length,
        'utm_source': utmSource,
        'firebase_id': fid,
        'timestamp': FieldValue.serverTimestamp(),
      }).then((value) {
        prefs.setBool(_isSentKey, true);
      }).timeout(const Duration(minutes: 1)));

      {
        final String utmSource = data['utm_source'] ?? '';
        final isValidFb = utmSource.contains('apps.facebook.com') || utmSource.contains('apps.instagram.com');

        DSMetrica.reportEvent('install_params', fbSend: true, fbAttributes: {
          'gclid': data['gclid'] ?? '',
          'ad_imp': data['adimp'] ?? '',
          'utm_source': utmSource,
          'utm_content': data['utm_content'] ?? '',
          'is_valid_fb_flow': isValidFb.toString(),
          'campaign': data['utm_campaign'] ?? 'unknown',
          'adjust_external_click_id': adjustExternalClickId,
          if (installParamsExtra != null)
            ...installParamsExtra(data)
        });
      }
    } catch (e, stack) {
      Fimber.e('$e: (referrer: $referrer)', stacktrace: stack);
    }
  } finally {
    final data = getReferrerFields();
    for (final callback in _changedCallbacks) {
      callback(data);
    }
  }
}