Skip to content

Commit b19d79c

Browse files
committed
Update tracking for when syncing fails due to inactive session
1 parent d80df4d commit b19d79c

File tree

4 files changed

+37
-19
lines changed

4 files changed

+37
-19
lines changed

WooCommerce/Classes/System/WatchDependenciesSynchronizer.swift

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -81,15 +81,12 @@ final class WatchDependenciesSynchronizer: NSObject, WCSessionDelegate {
8181
let configurationDependencies = Publishers.CombineLatest($enablesCrashReports, $account)
8282

8383
let watchDependencies = Publishers.CombineLatest(requiredDependencies, configurationDependencies)
84-
.map { [weak self] (required, configuration) -> WatchDependencies? in
84+
.map { (required, configuration) -> WatchDependencies? in
8585

8686
let (storeID, storeName, credentials, currencySettings) = required
8787
let (enablesCrashReports, account) = configuration
8888

89-
guard let storeID, let storeName, let credentials else {
90-
self?.analytics.track(.watchSyncingFailed, withError: SyncError.missingStoreDetailsOrCredentials)
91-
return nil
92-
}
89+
guard let storeID, let storeName, let credentials else { return nil }
9390

9491
return .init(storeID: storeID,
9592
storeName: storeName,
@@ -107,7 +104,15 @@ final class WatchDependenciesSynchronizer: NSObject, WCSessionDelegate {
107104

108105
// Do not update the context if the session is not active, the watch is not paired or the watch app is not installed.
109106
guard isSessionActive, watchSession.isPaired, watchSession.isWatchAppInstalled else {
110-
self?.analytics.track(.watchSyncingFailed, withError: SyncError.watchSessionInactiveOrNotPaired)
107+
self?.analytics.track(
108+
.watchSyncingFailed,
109+
properties: [
110+
"session_active": isSessionActive,
111+
"session_paired": watchSession.isPaired,
112+
"watch_app_installed": watchSession.isWatchAppInstalled
113+
],
114+
error: SyncError.watchSessionInactiveOrNotPaired
115+
)
111116
return
112117
}
113118

@@ -140,11 +145,6 @@ final class WatchDependenciesSynchronizer: NSObject, WCSessionDelegate {
140145
.store(in: &subscriptions)
141146
}
142147

143-
func session(_ session: WCSession, activationDidCompleteWith activationState: WCSessionActivationState, error: Error?) {
144-
DDLogInfo("🔵 WatchSession activated \(activationState)")
145-
self.isSessionActive = activationState == .activated
146-
}
147-
148148
func sessionDidBecomeInactive(_ session: WCSession) {
149149
// No op
150150
}
@@ -162,7 +162,6 @@ extension WatchDependenciesSynchronizer {
162162
/// This is in order to not duplicate tracks configuration which involve quite a lot of information to be transmitted to the watch.
163163
///
164164
func session(_ session: WCSession, didReceiveUserInfo userInfo: [String: Any] = [:]) {
165-
166165
// The user info could contain a track event. Send it if we found one.
167166
guard let rawEvent = userInfo[WooConstants.watchTracksKey] as? String,
168167
let analyticEvent = WooAnalyticsStat(rawValue: rawEvent) else {
@@ -183,11 +182,14 @@ extension WatchDependenciesSynchronizer {
183182
/// When one is identified we should try to re-sync credentials.
184183
///
185184
func session(_ session: WCSession, didReceiveMessage message: [String: Any]) {
186-
guard message[WooConstants.watchSyncKey] as? Bool == true else {
185+
if message[WooConstants.watchSyncKey] as? Bool == true {
186+
syncTrigger.toggle()
187+
} else if message[WooConstants.watchSessionActivatedKey] as? Bool == true {
188+
self.isSessionActive = true
189+
DDLogInfo("🔵 WatchSession activated)")
190+
} else {
187191
return DDLogError("⛔️ Unsupported sync request message: \(message)")
188192
}
189-
190-
syncTrigger.toggle()
191193
}
192194
}
193195

WooCommerce/Classes/System/WooConstants.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,10 @@ public enum WooConstants {
8585
///
8686
static let watchTracksKey = "watch-tracks-event"
8787

88+
/// Key used to identify when session is activated on the watch.
89+
///
90+
static let watchSessionActivatedKey = "watch_session_activated"
91+
8892
/// Key used to identify sync request attempt from the watch.
8993
///
9094
static let watchSyncKey = "watch-sync-event"

WooCommerce/Woo Watch App/ConnectView.swift

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ struct ConnectView: View {
1818
.multilineTextAlignment(.center)
1919
.fixedSize(horizontal: false, vertical: true)
2020

21+
Text(Localization.workaroundTip)
22+
.multilineTextAlignment(.center)
23+
.fixedSize(horizontal: false, vertical: true)
24+
2125
Image(systemName: "bolt.fill")
2226
.renderingMode(.original)
2327
.resizable()
@@ -44,10 +48,14 @@ extension ConnectView {
4448

4549
private enum Localization {
4650
static let connectMessage = AppLocalizedString(
47-
"watch.connect.messageWithWorkaround",
48-
value: "Open Woo on your iPhone, log into your store, and hold your Watch nearby. " +
49-
"If the issue persists, relaunch the app.",
50-
comment: "Info message when connecting your watch to the phone for the first time with a workaround option."
51+
"watch.connect.messageUpdated",
52+
value: "Open Woo on your iPhone, log into your store, and hold your Watch nearby.",
53+
comment: "Info message when connecting your watch to the phone for the first time."
54+
)
55+
static let workaroundTip = AppLocalizedString(
56+
"watch.connect.workaround",
57+
value: "If the error persists, relaunch the app.",
58+
comment: "Workaround when connecting the watch to the phone fails."
5159
)
5260
static let itsNotWorking = AppLocalizedString(
5361
"watch.connect.notworking.title",

WooCommerce/Woo Watch App/Dependencies/PhoneDependenciesSynchronizer.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,10 @@ final class PhoneDependenciesSynchronizer: NSObject, ObservableObject, WCSession
4242
/// Get the latest application context when the session activates
4343
///
4444
func session(_ session: WCSession, activationDidCompleteWith activationState: WCSessionActivationState, error: Error?) {
45+
if error == nil {
46+
WCSession.default.sendMessage([WooConstants.watchSessionActivatedKey: true], replyHandler: nil)
47+
}
48+
4549
DispatchQueue.main.async {
4650
self.storeDependencies(appContext: session.receivedApplicationContext)
4751
self.reloadDependencies()

0 commit comments

Comments
 (0)