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 181 forks source link

Starting app first time without network connection prevents events being pushed to Segment #901

Open simon-westman-uba opened 7 months ago

simon-westman-uba commented 7 months ago

I have noticed that if I install the app, but open it the very first time without any internet connection, so that the Segment SDK gets initialized without internet connection, no events will ever be pushed to Segment for that session, even after I regain internet connection again. It is first after I restart the app that events are getting into Segment again, but only for the new session after the restart. The events that should have been created and pushed during the very first session that started without internet connection are lost for ever.

I can see these logs when running locally on iOS, which might be related to this issue:

Connection 13: received failure notification
Connection 13: failed to connect 1:50, reason 18 446 744 073 709 551 615
Connection 13: encountered error(1:50)
Task <9D428989-9D90-4661-8C10-18A09448F79B>.<2> HTTP load failed, 0/0 bytes (error code: 18 446 744 073 709 550 607 [1:50])
Task <9D428989-9D90-4661-8C10-18A09448F79B>.<2> finished with error [18 446 744 073 709 550 607] Error Domain=NSURLErrorDomain Code=-1009 "The Internet connection appears to be offline." UserInfo={_kCFStreamErrorCodeKey=50, NSUnderlyingError=0x280501ad0 {Error Domain=kCFErrorDomainCFNetwork Code=-1009 "(null)" UserInfo={_kCFStreamErrorDomainKey=1, _kCFStreamErrorCodeKey=50, _NSURLErrorNWResolutionReportKey=Resolved 0 endpoints in 2ms using unknown from cache, _NSURLErrorNWPathKey=unsatisfied (No network route)}}, _NSURLErrorFailingURLSessionTaskErrorKey=LocalDataTask <9D428989-9D90-4661-8C10-18A09448F79B>.<2>, _NSURLErrorRelatedURLSessionTaskErrorKey=(
    "LocalDataTask <9D428989-9D90-4661-8C10-18A09448F79B>.<2>"
), NSLocalizedDescription=The Internet connection appears to be offline., NSErrorFailingURLStringKey=https://cdn-settings.segment.com/v1/projects/Ui9dSUFlcu71Jnur5FOfyLWvI3xbIKwm/settings, NSErrorFailingURLKey=https://cdn-settings.segment.com/v1/projects/Ui9dSUFlcu71Jnur5FOfyLWvI3xbIKwm/settings, _kCFStreamErrorDomainKey=1}
'An internal error occurred: ', '{"type":3,"innerError":{},"statusCode":-1}'
'Could not receive settings from Segment. Device mode destinations will be ignored unless you specify default settings in the client config.'

The workaround I currently implemented for this is to check for internet connection before initializing the Segment SDK, and if it is the very first start of the app I do not initialize the SDK until internet connection is available through a network listener. Any subsequent start of the app, after the Segment SDK has been initialized successfully with internet connection, does work as expected even without internet connection.

Also, I could not find anything about this in the documentation. If this was a known issue I would at least expect the documentation to state "Please make sure to have internet connection the first time you initialize the SDK".

Steps to reproduce

  1. Install your app containing the Segment SDK (but don't open it)
  2. Enable airplane mode
  3. Open your app
  4. Wait for Segment SDK to be initialized in your code
  5. Disable airplane mode (and wait for phone to connect to internet)
  6. No events will be pushed to Segment

Expected behavior

I would expect to see events being pushed to Segment after step 5 since internet connection will be available again at that point.

Actual behavior

No events at all are being pushed to Segment. I have to restart the app, and only any new events will be pushed after the restart.

oscb commented 7 months ago

@simon-westman-uba Hey Simon, this is super detailed and good report, I appreciate the time to test this out.

We already have an idea of what's going on, it's not an intended behavior. I put this up on our sprint to get this fixed

simon-westman-uba commented 7 months ago

Thank you very much. I will keep an eye out for any upcoming updates and hope you will be able to solve this rather easy.

alanjcharles commented 2 weeks ago

Hi @simon-westman-uba apologies for the confusion and/or delay here. As @oscb said, thank you for the detailed report, we really do appreciate it. Can you let me know if this is an issue you're still experiencing? If so, we will prioritize it over the next sprint. Thank you for your patience

simon-westman-uba commented 1 week ago

Hi @alanjcharles I do still see this issue as explained above. Thank you for looking into this.