screen method

  1. @override
Future<void> screen({
  1. required String screenName,
  2. Map<String, Object>? properties,
})

Implementation

@override
Future<void> screen({
  required String screenName,
  Map<String, Object>? properties,
}) async {
  // The screen name argument wins over a properties `$screen_name`.
  // beforeSend still runs after this and can change the name.
  final propsWithScreenName = <String, Object>{
    ...?properties,
    PostHogPropertyName.screenName: screenName,
  };

  final processedEvent = await _runBeforeSend(
    PostHogEventName.screen,
    propsWithScreenName,
  );
  if (processedEvent == null) {
    printIfDebug('[PostHog] Screen event dropped by beforeSend: $screenName');
    return;
  }

  if (processedEvent.event != PostHogEventName.screen) {
    await capture(
      eventName: processedEvent.event,
      properties: processedEvent.properties?.cast<String, Object>(),
    );
    return;
  }

  final finalScreenName =
      processedEvent.properties?[PostHogPropertyName.screenName] as String? ??
          screenName;
  processedEvent.properties?.remove(PostHogPropertyName.screenName);
  final remaining = processedEvent.properties;

  return handleWebMethodCall(
    MethodCall('screen', {
      'screenName': finalScreenName,
      if (remaining != null && remaining.isNotEmpty) 'properties': remaining,
    }),
  );
}