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,
}) {
  // Sanitize event name and category by replacing spaces with underscores
  name = name?.replaceAll(RegExp(r' '), '_');
  category = category?.replaceAll(RegExp(r' '), '_');

  // Combine name and category for full event name
  final eventName =
      category == null || category.isEmpty ? name! : "${name}_$category";

  // Initialize parameters if null
  parameters ??= <String, dynamic>{};

  // Add version and build information
  final buildStr = NSFirebase.instance.buildNumber;
  final versionStr = NSFirebase.instance.version;
  parameters.putIfAbsent(
      ConstKeys.version, () => '$versionStr${ConstKeys.dash}$buildStr');

  // Add user information if available
  if (_userInfo.isNotEmpty) {
    parameters.putIfAbsent(ConstKeys.id, () => _userInfo[ConstKeys.id]);
    parameters.putIfAbsent(ConstKeys.email, () => _userInfo[ConstKeys.email]);
  }

  // Create a new map to filter out null values
  final newParameters = <String, Object>{};
  parameters.forEach((key, dynamic value) {
    if (value != null) {
      newParameters[key] = value;
    }
  });

  // Log event name for debugging
  appLogsNS("eventName:$eventName");

  // Add timestamp to parameters
  newParameters['date_time'] = DateTime.now().toIso8601String();

  // Log the event to Firebase Analytics
  return _firebaseAnalytics.logEvent(
    name: eventName,
    parameters: newParameters,
    callOptions: callOptions,
  );
}