setup method

  1. @override
Future<void> setup(
  1. PostHogConfig config
)

Implementation

@override
Future<void> setup(PostHogConfig config) async {
  // It's assumed posthog-js is initialized by the user in their HTML.
  // This setup primarily hooks into the existing posthog-js instance.

  // If apiKey and host are in config, and posthog.init is to be handled by plugin:
  // This is an example if we wanted the plugin to also call posthog.init()
  // final jsOptions = <String, dynamic>{
  //   'api_host': config.host,
  //   // Add other relevant options from PostHogConfig if needed for JS init
  // }.jsify();
  // posthog?.callMethod('init'.toJS, config.apiKey.toJS, jsOptions);

  final ph = posthog;
  if (config.onFeatureFlags != null && ph != null) {
    final dartCallback = config.onFeatureFlags!;

    // JS SDK calls with: (flags: string[], variants: Record, context?: {errorsLoading})
    // We ignore the JS parameters and just invoke the void callback
    final jsCallback =
        (JSArray jsFlags, JSObject jsFlagVariants, [JSObject? jsContext]) {
      dartCallback();
    }.toJS;

    ph.onFeatureFlags(jsCallback);
  }
}