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 projectToken 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.projectToken.toJS, jsOptions);

  final ph = posthog;
  _config = config;
  _beforeSendCallbacks = config.beforeSend;

  // posthog-js is initialized by the host app, so a few config options never
  // reach it. Warn instead of silently ignoring them.
  if (!config.preloadFeatureFlags) {
    printIfDebug(
      'Warning: PostHogConfig.preloadFeatureFlags is not applied on Flutter web. '
      'posthog-js is initialized by your app, so set '
      'advanced_disable_feature_flags_on_first_load in your posthog.init({...}) call instead.',
    );
  }
  if (config.evaluationContexts?.isNotEmpty ?? false) {
    printIfDebug(
      'Warning: PostHogConfig.evaluationContexts is not applied on Flutter web. '
      'Set evaluation_contexts in your posthog.init({...}) call instead.',
    );
  }
  if (config.featureFlagRequestMaxRetries != 1) {
    printIfDebug(
      'Warning: PostHogConfig.featureFlagRequestMaxRetries is not applied on Flutter web. '
      'Set feature_flag_request_max_retries in your posthog.init({...}) call instead.',
    );
  }
  if (config.bootstrap != null) {
    printIfDebug(
      'Warning: PostHogConfig.bootstrap is not applied on Flutter web. '
      'Configure bootstrap in your posthog.init({...}) call instead.',
    );
  }
  if (config.disableGeoIp) {
    printIfDebug(
      'Warning: PostHogConfig.disableGeoIp is not applied on Flutter web. '
      r'Set $geoip_disable: true on events in your posthog.init({...}) before_send instead.',
    );
  }

  WebCanvasMaskProvider(config).register();

  // Skipped for a posthog-js stub without onFeatureFlags.
  if (ph != null && (ph as JSObject).has('onFeatureFlags')) {
    // Repeated setup would otherwise deliver each load twice.
    _featureFlagsUnsubscribe?.callAsFunction();
    _featureFlagsUnsubscribe = null;
    final generation = ++_featureFlagsGeneration;

    // JS SDK calls with: (flags: string[], variants: Record, context?: {errorsLoading})
    final jsCallback =
        (JSArray jsFlags, JSObject jsFlagVariants, [JSObject? jsContext]) {
      // The posthog-js loader snippet queues the registration and returns
      // no unsubscribe, so a later setup() or close() can't remove it.
      if (generation != _featureFlagsGeneration) return;
      final loaded = featureFlagsLoadedFromMap({
        'variants': jsFlagVariants.dartify(),
        'errorsLoading': (jsContext?.dartify() as Map?)?['errorsLoading'],
      });
      FeatureFlagsListeners.instance.deliver(loaded);
    }.toJS;

    // posthog-js replays loaded flags synchronously, and a callback may call
    // close() or setup() before this returns the unsubscribe.
    final unsubscribe = ph.onFeatureFlags(jsCallback);
    if (generation == _featureFlagsGeneration) {
      _featureFlagsUnsubscribe = unsubscribe;
    } else {
      unsubscribe?.callAsFunction();
    }
  }
}