[Patch] Analytics — app-switch-open:succeeded can be lost on app suspension #402
Conversation
|
Hey @NastassiaRodzik do you have a link to some documentation related to this change? I'm not super familiar with background tasks. I think I used them a while ago with WatchOS, but I'd like to know more in the context of iOS. |
Co-authored-by: sshropshire <58225613+sshropshire@users.noreply.github.com>
Consolidate analytics delivery into a single public API so callers opt in via withBackgroundProtection instead of a separate method name that leaks implementation details. Co-authored-by: Cursor <cursoragent@cursor.com>
We use UIApplication.beginBackgroundTask — Apple’s API for finishing short work when the app is about to be suspended. Docs: beginBackgroundTask and background execution overview. |
|
@NastassiaRodzik thank you this is helpful. I feel like this may be difficult to add unit tests for, but I would like to try if possible. |
| var bgTaskID: UIBackgroundTaskIdentifier = .invalid | ||
| var eventTask: Task<Void, Never>? |
There was a problem hiding this comment.
| var bgTaskID: UIBackgroundTaskIdentifier = .invalid | |
| var eventTask: Task<Void, Never>? | |
| let bgTaskID: UIBackgroundTaskIdentifier | |
| let eventTask: Task<Void, Never> |
I'm just curious here: does this code snippet ^ break compilation?
There was a problem hiding this comment.
yes, it's because we need to capture them both by a closure on line 79 and 84
Good catch! Added Unit tests to cover the change, let me know what you think |
Reason for changes
The problem:
AnalyticsService.sendEvent()wraps its HTTP request in a SwiftTask(priority: .background).Task(priority: .background)sets Swift concurrency thread priority — it is not an iOS background task. When the app is suspended by the OS (which happens immediately after UIApplication.open() successfully launches the PayPal app), all Swift tasks are frozen and the network request is never sent.Analytics events at risk:
"paypal-web-payments:checkout:app-switch-open:succeeded"Summary of changes
sendEventWithBackgroundProtectionmethod that protects Analytics events with an iOS background task.Checklist
Authors
Evidence
When using
sendEventWithBackgroundProtection:App Switch flow:
https://github.com/user-attachments/assets/0f6d82b6-933c-4cd3-8fe7-d98d84564256
Analytics: