logEvent static method

Future<void> logEvent({
  1. required String name,
  2. Map<String, dynamic>? params,
  3. String? userId,
})

Log an event.

The following information are collected:

  • The date the event was logged
  • The current user ID, if allowed
  • The user Operational System
  • The user country

A PostgrestError is thrown if the event fails

Implementation

static Future<void> logEvent({
  required String name,
  Map<String, dynamic>? params,
  String? userId,
}) async {
  assert(
    name.length >= 2,
    'The name must be at least 2 characters long',
  );

  params ??= {};
  params.addAll({
    'country_code': userCountry ?? getUserCountry(),
    'os': operatingSystem,
  });

  final response =
      await SupabaseAddons.client.from(_analyticsTableName).insert({
    'name': name.replaceAll(' ', '_'),
    'params': params,
    'timestamp': '${DateTime.now().millisecondsSinceEpoch}',
    if (userId != null || _useLoggedUserInfo)
      'user_id': userId ?? SupabaseAuthAddons.auth.currentUser?.id,
  }, returning: ReturningOption.minimal).execute();

  if (response.error != null) {
    print(response.error!.message);
    throw response.error!;
  }
}