reportEventWithMap static method

Future<void> reportEventWithMap(
  1. String eventName,
  2. Map<String, Object>? attributes, {
  3. bool fbSend = false,
  4. Map<String, Object>? fbAttributes,
  5. int stackSkip = 1,
  6. EventSendingType eventSendingType = EventSendingType.everyTime,
})

Report event to AppMetrica and UserX (disabled in debug mode)

Implementation

static Future<void> reportEventWithMap(
    String eventName,
    Map<String, Object>? attributes, {
      bool fbSend = false,
      Map<String, Object>? fbAttributes,
      int stackSkip = 1,
      EventSendingType eventSendingType = EventSendingType.everyTime,
    }) async {
  if (kIsWeb || !Platform.isAndroid && !Platform.isIOS) return;

  if (eventSendingType == EventSendingType.oncePerAppLifetime && _isEventAlreadySendPerLifetime(eventName)) {
    logDebug(
        'Analytics event $eventName with type ${EventSendingType.oncePerAppLifetime.name} has already been dispatched, current report is skipped',
        stackSkip: stackSkip,
        stackDeep: 1);
    return;
  }

  _eventId++;
  try {
    final baseAttrs = <String, Object>{};
    baseAttrs.addAll(_persistentAttrs);

    baseAttrs.addAll(_attrsHandlerOld?.call() ?? {});
    for (final handler in _attrsHandlers) {
      baseAttrs.addAll(handler());
    }

    if (DSReferrer.isInitialized) {
      // Add referrer's attributes
      final data = DSReferrer.I.getReferrerFields();
      void addFromData(String key) {
        data[key]?.let((value) {
          baseAttrs[key] = value;
        });
      }
      if (Platform.isIOS) {
        addFromData('partner');
      } else {
        addFromData('utm_source');
        addFromData('utm_campaign');
        addFromData('utm_medium');
        addFromData('gclid');
      }
    }

    DSPrefs.I.setAppLastUsed(DateTime.now());
    final sessionId = DSPrefs.I.getSessionId();

    baseAttrs['session_id'] = sessionId;
    baseAttrs['event_id'] = _eventId;
    baseAttrs['user_time'] = DateTime.now().toIso8601String();

    final Map<String, Object> attrs;
    if (attributes == null) {
      attrs = baseAttrs;
    } else {
      attrs = Map<String, Object>.from(baseAttrs);
      attrs.addAll(attributes);
    }

    UserX.addEvent(eventName, attrs.map<String, String>((key, value) => MapEntry(key, '$value')));

    logDebug('$eventName $attrs', stackSkip: stackSkip, stackDeep: 5);

    if (kDebugMode && !_debugModeSend) {
      if (eventSendingType == EventSendingType.oncePerAppLifetime) _setEventSendPerLifetime(eventName);
      return;
    }

    if (fbSend) {
      unawaited(() async {
        try {
          await FirebaseAnalytics.instance.logEvent(name: eventName, parameters: () {
            if (fbAttributes != null) {
              final fbAttrs = Map<String, Object>.from(baseAttrs);
              fbAttrs.addAll(fbAttributes);
              return fbAttrs;
            } else {
              return attrs;
            }
          }());
        } catch (e, stack) {
          if (!_reportEventErrorFB) {
            _reportEventErrorFB = true;
            Fimber.e('$e', stacktrace: stack);
          }
        }
      }());
    }
    await m.AppMetrica.reportEventWithMap(eventName, attrs);

    if (eventSendingType == EventSendingType.oncePerAppLifetime) _setEventSendPerLifetime(eventName);
  } catch (e, stack) {
    if (!_reportEventError) {
      _reportEventError = true;
      Fimber.e('$e', stacktrace: stack);
    }
  }
}