segmentio / analytics-react-native

The hassle-free way to add analytics to your React-Native app.
https://segment.com/docs/sources/mobile/react-native/
MIT License
354 stars 182 forks source link

BackgroundFlushPolicy doesn't flush the Application Backgrounded event #868

Open sregg opened 11 months ago

sregg commented 11 months ago
    "@segment/analytics-react-native": "2.15.0",
    "@segment/analytics-react-native-plugin-braze": "^0.5.4",
    "@segment/analytics-react-native-plugin-mixpanel": "0.3.3",
    "@segment/sovran-react-native": "1.0.4",

Steps to reproduce

  1. Add BackgroundFlushPolicy to client flush policies (e.g. segmentClient.addFlushPolicy(new BackgroundFlushPolicy());
  2. Open the app
  3. Trigger an event
  4. Close the app

Expected behavior

Actual behavior

sregg commented 11 months ago

I tried this custom flush policy but I'm still not seeing the Application Backgrounded until re-opening the app.

class FlushAppBackgroundedPolicy extends FlushPolicyBase {
  start() {
    // no-op
  }

  onEvent(event: SegmentEvent): void {
    if ('event' in event && event.event === 'Application Backgrounded') {
      console.log({ event }); // is logged properly on background
      this.shouldFlush.value = true;
    }
  }

  end(): void {
    // no-op
  }
}
oscb commented 11 months ago

@sregg Thanks for the report! Is this happening in iOS or Android (or both) ?

sregg commented 11 months ago

@sregg Thanks for the report! Is this happening in iOS or Android (or both) ?

I've only tried on iOS. I'll try Android tomorrow.

sregg commented 11 months ago

It's also happening on Android.

ekallevig commented 10 months ago

@sregg I'm wondering about this too. we upgraded from 1.5.2 to 2.10.1 a while back and have noticed a significant increase in delta between event and event_received. i can't seem to find any information about what default flush behavior is for startup and background events. should it be flushing by default in those cases or do you need to set BackgroundFlushPolicy explicitly for that to happen?

sregg commented 10 months ago

To flush on background you have to add the background policy explicitly I think. That seems to flush all events except the background event itself.

sregg commented 9 months ago

Any update on this from the Segment team?