Skip to content

Commit 2fcc966

Browse files
committed
Save anonymous ID to KeyChain for identifying a logged-out user potentially across installs since the app enables Keychain Sharing.
1 parent 3eed6ed commit 2fcc966

File tree

3 files changed

+20
-8
lines changed

3 files changed

+20
-8
lines changed

WooCommerce/Classes/Authentication/Keychain+Entries.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,10 @@ extension Keychain {
66
get { self[WooConstants.keychainAppleIDKey] }
77
set { self[WooConstants.keychainAppleIDKey] = newValue }
88
}
9+
10+
/// The anonymous ID used to identify a logged-out user potentially across installs in analytics and A/B experiments.
11+
var anonymousID: String? {
12+
get { self[WooConstants.anonymousIDKey] }
13+
set { self[WooConstants.anonymousIDKey] = newValue }
14+
}
915
}

WooCommerce/Classes/System/SessionManager.swift

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -119,14 +119,16 @@ final class SessionManager: SessionManagerProtocol {
119119
/// Anonymous UserID.
120120
///
121121
var anonymousUserID: String? {
122-
get {
123-
if let anonID = defaults[.defaultAnonymousID] as? String, !anonID.isEmpty {
124-
return anonID
125-
} else {
126-
let newValue = UUID().uuidString
127-
defaults[.defaultAnonymousID] = newValue
128-
return newValue
129-
}
122+
if let anonID = defaults[.defaultAnonymousID] as? String, !anonID.isEmpty {
123+
return anonID
124+
} else if let keychainAnonID = keychain.anonymousID, !keychainAnonID.isEmpty {
125+
defaults[.defaultAnonymousID] = keychainAnonID
126+
return keychainAnonID
127+
} else {
128+
let newValue = UUID().uuidString
129+
defaults[.defaultAnonymousID] = newValue
130+
keychain.anonymousID = newValue
131+
return newValue
130132
}
131133
}
132134

WooCommerce/Classes/System/WooConstants.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@ enum WooConstants {
2121
///
2222
static let keychainAppleIDKey = "AppleID"
2323

24+
/// Keychain Access's Key for anonymous ID
25+
///
26+
static let anonymousIDKey = "anonymousID"
27+
2428
/// Push Notifications ApplicationID
2529
///
2630
#if DEBUG

0 commit comments

Comments
 (0)