logEvent method

  1. @override
Future<void> logEvent({
  1. String? name,
  2. String? category,
  3. Map<String, dynamic>? parameters,
  4. AnalyticsCallOptions? callOptions,
})
override

Logs a custom Flutter Analytics event with the given name and event parameters.

The event can have up to 25 parameters. Events with the same name must have the same parameters. Up to 500 event names are supported.

The name of the event. Should contain 1 to 40 alphanumeric characters or underscores. The name must start with an alphabetic character. Some event names are reserved. See FirebaseAnalytics.Event for the list of reserved event names. The "firebase_", "google_" and "ga_" prefixes are reserved and should not be used. Note that event names are case-sensitive and that logging two events whose names differ only in case will result in two distinct events.

The map of event parameters. Passing null indicates that the event has no parameters. Parameter names can be up to 40 characters long and must start with an alphabetic character and contain only alphanumeric characters and underscores. String, long and double param types are supported. String parameter values can be up to 100 characters long. The "firebase_", "google_" and "ga_" prefixes are reserved and should not be used for parameter names.

See also:

Implementation

@override
Future<void> logEvent({
  String? name,
  String? category,
  Map<String, dynamic>? parameters,
  AnalyticsCallOptions? callOptions,
}) async {
  name = name?.replaceAll(new RegExp(r' '), '_'); // name cannot use '-'
  category = category?.replaceAll(new RegExp(r' '), '_');

  final eventName =
      category == null || category == '' ? name! : "${name}_$category";

  // TODO: needs to get this from the config or app version or A/B testing version
  if (parameters == null) {
    parameters = <String, dynamic>{};
  }

  String buildStr = NSFirebase.instance.buildNumber;
  String versionStr = NSFirebase.instance.version;
  parameters.putIfAbsent(
      ConstKeys.version, () => versionStr + ConstKeys.dash + buildStr);

  if (_userInfo.isNotEmpty) {
    parameters.putIfAbsent(ConstKeys.id, () => _userInfo[ConstKeys.id]);
    parameters.putIfAbsent(ConstKeys.email, () => _userInfo[ConstKeys.email]);
  }

  Map<String, Object> newParameters = Map<String, Object>();
  // Remove null
  parameters.forEach((key, dynamic value) {
    if (value != null) {
      newParameters.putIfAbsent(key, () => value);
    }
  });
  appLogsNS("eventName:$eventName");
  newParameters.putIfAbsent(
      'date_time', () => DateTime.now().toIso8601String());
  // TODO: need to avoid parameters contains List value. This causes exception
  await _firebaseAnalytics.logEvent(
    name: eventName,
    parameters: newParameters,
    callOptions: callOptions,
  );
}