tryStartUserX static method

Future<void> tryStartUserX()

Initialize UserX if it is allowed by RemoteConfig

Implementation

static Future<void> tryStartUserX() async {
  assert(DSConstants.isInitialized);
  assert(DSRemoteConfig.I.isInitialized);

  if (kDebugMode && !_debugModeSend) return;
  if (kIsWeb || !Platform.isAndroid && !Platform.isIOS) return;

  if (DSConstants.I.isInternalVersion) {
    await startUserX();
    return;
  }

  var val = DSRemoteConfig.I.getUserXPercent();
  if (val == 0) {
    await DSRemoteConfig.I.waitForFullInit(maxWait: const Duration(seconds: 20));
    val = DSRemoteConfig.I.getUserXPercent();
    if (val == 0) return;
  }
  final sessions = DSRemoteConfig.I.getUserXSessions();
  if (sessions != 0 && sessions < DSPrefs.I.getSessionId()) {
    return;
  }

  // if yandexId is empty (or non-valid) use simple random
  final yid = int.tryParse(yandexId.let((s) => s.length >= 2 ? s.substring(s.length - 2) : s)) ?? Random().nextInt(100);
  if ((yid % 100).toInt() < val) {
    await DSMetrica.startUserX();
  }
}