Skip to content

[Patch] Analytics — app-switch-open:succeeded can be lost on app suspension #402

Merged
sshropshire merged 11 commits into
paypal:feature/app-switchfrom
NastassiaRodzik:analytics-fixes
Jul 20, 2026
Merged

[Patch] Analytics — app-switch-open:succeeded can be lost on app suspension #402
sshropshire merged 11 commits into
paypal:feature/app-switchfrom
NastassiaRodzik:analytics-fixes

Conversation

@NastassiaRodzik

@NastassiaRodzik NastassiaRodzik commented Jun 3, 2026

Copy link
Copy Markdown

Reason for changes

The problem: AnalyticsService.sendEvent() wraps its HTTP request in a Swift Task(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

  • Add sendEventWithBackgroundProtection method that protects Analytics events with an iOS background task.

Checklist

  • Added a changelog entry

Authors

List GitHub usernames for everyone who contributed to this pull request.

  • NastassiaRodzik

Evidence

When using sendEventWithBackgroundProtection:

App Switch flow:
https://github.com/user-attachments/assets/0f6d82b6-933c-4cd3-8fe7-d98d84564256

Analytics:

paypal-web-payments:checkout:started - sent ✅ 
paypal-web-payments:checkout:app-switch-open:succeeded - sent with BG Protection ✅ 

@NastassiaRodzik
NastassiaRodzik marked this pull request as ready for review June 5, 2026 16:53
@sshropshire

Copy link
Copy Markdown
Collaborator

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.

Comment thread Sources/PayPalWebPayments/PayPalWebCheckoutClient.swift Outdated
Comment thread CHANGELOG.md Outdated
@NastassiaRodzik
NastassiaRodzik marked this pull request as draft June 8, 2026 21:16
NastassiaRodzik and others added 2 commits June 9, 2026 15:56
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>
@NastassiaRodzik

Copy link
Copy Markdown
Author

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.

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
NastassiaRodzik marked this pull request as ready for review June 9, 2026 21:46
@sshropshire

Copy link
Copy Markdown
Collaborator

@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.

Comment thread Sources/CorePayments/Networking/AnalyticsService.swift
Comment on lines +70 to +71
var bgTaskID: UIBackgroundTaskIdentifier = .invalid
var eventTask: Task<Void, Never>?

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, it's because we need to capture them both by a closure on line 79 and 84

@NastassiaRodzik

Copy link
Copy Markdown
Author

@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.

Good catch! Added Unit tests to cover the change, let me know what you think

@NastassiaRodzik NastassiaRodzik changed the title Analytics — app-switch-open:succeeded can be lost on app suspension [Patch] Analytics — app-switch-open:succeeded can be lost on app suspension Jun 18, 2026
Comment thread UnitTests/PaymentsCoreTests/Mocks/MockTrackingEventsAPI.swift
@sshropshire
sshropshire merged commit 5b1d506 into paypal:feature/app-switch Jul 20, 2026
3 of 5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants