Skip to content

WaitingPlugin works not as expected #1308

Description

@vlasov-pepperstone

Steps to reproduce

  1. Create a Segment client and add a custom WaitingPlugin that simulates async init (sets an id after 10s, then calls resume()):
import { createClient } from '@segment/analytics-react-native';
import { AppsFlyerIntegrationPlugin } from './AppsFlyerPlugin';

export const segmentClient = createClient({
  writeKey: '...',
  trackAppLifecycleEvents: true,
  debug: true,
});

segmentClient.add({ plugin: new AppsFlyerIntegrationPlugin() });
import {
  PluginType,
  SegmentClient,
  type SegmentEvent,
  WaitingPlugin,
} from '@segment/analytics-react-native';

export class AppsFlyerIntegrationPlugin extends WaitingPlugin {
  type = PluginType.enrichment;

  private appsFlyerId = 'EMPTY';

  configure = (analytics: SegmentClient) => {
    super.configure(analytics);

    // simulate long delay
    delay(10_000).then(() => {
      console.log('[AppsFlyer] set id', 'CORRECT_APPSFLYER_ID');
      this.appsFlyerId = 'CORRECT_APPSFLYER_ID';
      void this.resume();
    });
  };

  execute = (event: SegmentEvent): SegmentEvent => {
    const name = 'event' in event ? event.event : event.type;
    console.log('[AppsFlyer] execute', this.appsFlyerId, event.type, name);

    return {
      ...event,
      integrations: {
        ...event.integrations,
        AppsFlyer: {
          appsFlyerId: this.appsFlyerId,
        },
      },
    };
  };
}

function delay(ms: number) {
  return new Promise((resolve) => setTimeout(resolve, ms));
}
  1. Launch the app (lifecycle events like Application Opened / identify fire automatically).
  2. After the plugin resumes, trigger any later event (e.g. a button click).

Expected behavior

Events should be buffered until resume() is called. After the id is set and resume() runs, buffered and subsequent events should execute with the correct id:

[AppsFlyer] set id CORRECT_APPSFLYER_ID
[AppsFlyer] execute CORRECT_APPSFLYER_ID track Application Opened
[AppsFlyer] execute CORRECT_APPSFLYER_ID identify identify
[AppsFlyer] execute CORRECT_APPSFLYER_ID track Button Clicked

Actual behavior

Events are processed immediately, before resume():

[AppsFlyer] execute EMPTY track Application Opened
[AppsFlyer] execute EMPTY identify identify
[AppsFlyer] set id CORRECT_APPSFLYER_ID
[AppsFlyer] execute CORRECT_APPSFLYER_ID track Button Clicked

Checked on both: 2.24.0 and 2.23.0

  • "@segment/analytics-react-native": "2.24.0",
  • "@segment/sovran-react-native": "1.1.4",
  • Integrations versions (if used): @segment/analytics-react-native-plugin-firebase 0.4.3 (not required for the repro)
  • React Native version: 0.81.5
  • iOS or Android or both? iOS

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugBug fixinvestigateAsk Claude to attempt reproduction and post a diagnosis report artifact

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions