init static method

Future<void> init({
  1. required String yandexKey,
  2. required String uxCamKey,
  3. DSMetricaUserIdType userIdType = DSMetricaUserIdType.none,
  4. bool debugModeSend = false,
})

Initialize DSMetrica. Must call before the first use yandexKey - API key of Yandex App Metrica uxCamKey - API key of UXCam forceSend - send events in debug mode too

Implementation

static Future<void> init({
  required String yandexKey,
  required String uxCamKey,
  DSMetricaUserIdType userIdType = DSMetricaUserIdType.none,
  bool debugModeSend = false,
}) async {
  if (_isInitialized) {
    Fimber.e('DSMetrica is already initialised', stacktrace: StackTrace.current);
    return;
  }

  _uxCamKey = uxCamKey;
  _debugModeSend = debugModeSend;
  _userIdType = userIdType;

  final waits = <Future>[];

  WidgetsFlutterBinding.ensureInitialized();

  if (!kIsWeb && (Platform.isAndroid || Platform.isIOS)) {
    await m.AppMetrica.activate(m.AppMetricaConfig(yandexKey,
      sessionsAutoTrackingEnabled: !kDebugMode || _debugModeSend,
      dataSendingEnabled: !kDebugMode || _debugModeSend ? null : false,
      dispatchPeriodSeconds: 5,
    ));

    if (kDebugMode && !_debugModeSend) {
      await m.AppMetrica.pauseSession();
    }
  } else {
    assert(yandexKey == '', 'yandexKey supports mobile platform only. Remove yandexKey id');
    assert(uxCamKey == '', 'uxCamKey supports mobile platform only. Remove uxCamKey id');
  }

  await Future.wait(waits);

  switch (_userIdType) {
    case DSMetricaUserIdType.none:
      break;
    case DSMetricaUserIdType.adjustId:
      break;
    case DSMetricaUserIdType.deviceId:
      unawaited(() async {
        final id = await getDeviceId();
        await DSMetrica.setUserProfileID(id);
        Fimber.d('deviceId=$id');
      } ());
      break;
  }

  DSAdjust.registerAttributionCallback((data) {
    final adid = DSAdjust.getAdid();
    if (_userIdType == DSMetricaUserIdType.adjustId && adid != null) {
      unawaited(DSMetrica.setUserProfileID(adid));
    }
    Fimber.d('DSMetrica updated by Adjust adid=$adid');
    unawaited(m.AppMetrica.reportExternalAttribution(m.AppMetricaExternalAttribution.adjust(
      adid: adid,
      trackerName: data.trackerName,
      trackerToken: data.trackerToken,
      network: data.network,
      campaign: data.campaign,
      adgroup: data.adgroup,
      creative: data.creative,
      clickLabel: data.clickLabel,
      costType: data.costType,
      costAmount: data.costAmount,
      costCurrency: data.costCurrency,
      fbInstallReferrer: data.fbInstallReferrer,
    )));
  });

  _isInitialized = true;
  // allow to first start without internet connection
  if (!kIsWeb && (Platform.isAndroid || Platform.isIOS)) {
    unawaited(() async {
      // AppMetrica has lazy deviceId initialization after app install. Try to fix
      var exSent = false;
      for (var i = 0; i < 50; i++) {
        try {
          _yandexId = await m.AppMetrica.deviceId ?? '';
        } catch (e, stack) {
          if (!exSent) {
            exSent = true;
            Fimber.e('$e', stacktrace: stack);
          }
        }
        if (_yandexId.isNotEmpty) break;
        await Future.delayed(const Duration(milliseconds: 100));
      }
      Fimber.d('yandexId=$yandexId');
      if (_yandexId.isEmpty) {
        Fimber.e('yandexId was not initialized', stacktrace: StackTrace.current);
      }
    }());
  }
}