init static method

Future<void> init({
  1. required String yandexKey,
  2. String uxCamKey = '',
  3. String amplitudeKey = '',
  4. DSMetricaUserIdType userIdType = DSMetricaUserIdType.none,
  5. 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,
  String uxCamKey = '',
  String amplitudeKey = '',
  DSMetricaUserIdType userIdType = DSMetricaUserIdType.none,
  bool debugModeSend = false,
}) async {
  if (_isInitialized) {
    Fimber.e('DSMetrica is already initialised', stacktrace: StackTrace.current);
    return;
  }

  DSAppState.internalInit();

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

  final waits = <Future>[];

  WidgetsFlutterBinding.ensureInitialized();

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

      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');
    }
  } ());
  if (_amplitudeKey.isNotEmpty) {
    waits.add(() async {
      await _amplitude.init(_amplitudeKey);
    }());
  }

  await Future.wait(waits);

  switch (_userIdType) {
    case DSMetricaUserIdType.none:
      break;
    case DSMetricaUserIdType.adjustId:
      break;
      // ignore: deprecated_member_use_from_same_package
      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);
      }
    }());
  }
}