@@ -9,14 +9,24 @@ import io.customer.sdk.util.EventNames
99
1010/* *
1111 * Coordinates all location state management: persistence, restoration,
12- * profile enrichment, and sending location track events.
12+ * identify context enrichment, and sending location track events.
1313 *
14- * Implements [IdentifyContextProvider] to enrich identify event context
15- * with the latest location. Sends location track events directly via
16- * [DataPipeline], applying the userId gate and sync filter locally.
14+ * Location reaches the backend through two independent paths:
1715 *
18- * Tracks the last known userId to detect profile switches and reset
19- * the sync filter accordingly.
16+ * 1. **Identify context enrichment** — implements [IdentifyContextProvider].
17+ * Every identify() call enriches the event context with the latest
18+ * location coordinates. This is unfiltered — a new user always gets
19+ * the device's current location on their profile immediately.
20+ *
21+ * 2. **"Location Update" track event** — sent via [DataPipeline.track].
22+ * Gated by a userId check and a sync filter (24h / 1km threshold)
23+ * to avoid redundant events. This creates a discrete event in the
24+ * user's activity timeline for journey/segment triggers.
25+ *
26+ * Profile switch handling is intentionally not tracked here.
27+ * On clearIdentify(), [onReset] clears all state (cache, persistence,
28+ * sync filter). On identify(), the new user's profile receives the
29+ * location via path 1 regardless of the sync filter's state.
2030 */
2131internal class LocationTracker (
2232 private val dataPipeline : DataPipeline ? ,
@@ -28,9 +38,6 @@ internal class LocationTracker(
2838 @Volatile
2939 private var lastLocation: LocationCoordinates ? = null
3040
31- @Volatile
32- private var lastKnownUserId: String? = null
33-
3441 override fun getIdentifyContext (): Map <String , Any > {
3542 val location = lastLocation ? : return emptyMap()
3643 return mapOf (
@@ -65,41 +72,35 @@ internal class LocationTracker(
6572 }
6673
6774 /* *
68- * Called when the user identity changes (identify or clearIdentify).
69- * Detects profile switches to reset the sync filter, then attempts
70- * to sync the cached location for the new user.
75+ * Called when a user is identified. Attempts to sync the cached
76+ * location as a track event for the newly identified user.
77+ *
78+ * The identify event itself already carries location via
79+ * [getIdentifyContext] — this method handles the supplementary
80+ * "Location Update" track event, subject to the sync filter.
7181 */
72- fun onUserChanged (userId : String? , anonymousId : String ) {
73- val previousUserId = lastKnownUserId
74- lastKnownUserId = userId
75-
76- // Detect profile switch: previous user existed and differs from new user
77- if (previousUserId != null && previousUserId != userId) {
78- logger.debug(" Profile switch detected ($previousUserId -> $userId ), clearing sync filter" )
79- locationSyncFilter.clearSyncedData()
80- }
81-
82- if (! userId.isNullOrEmpty()) {
83- syncCachedLocationIfNeeded()
84- }
82+ fun onUserIdentified () {
83+ syncCachedLocationIfNeeded()
8584 }
8685
8786 /* *
8887 * Clears all location state on identity reset (clearIdentify).
89- * Resets in-memory cache, persisted location, and sync filter.
88+ * Resets in-memory cache, persisted location, and sync filter —
89+ * similar to how device tokens and other per-user state are
90+ * cleared on reset.
9091 */
9192 fun onReset () {
9293 lastLocation = null
93- lastKnownUserId = null
9494 locationPreferenceStore.clearCachedLocation()
9595 locationSyncFilter.clearSyncedData()
9696 logger.debug(" Location state reset" )
9797 }
9898
9999 /* *
100100 * Re-evaluates the cached location for sending.
101- * Called on identify (via [onUserChanged]) and on cold start to handle
102- * cases where a location was cached but not yet sent for the current user.
101+ * Called on identify (via [onUserIdentified]) and on cold start
102+ * (via replayed UserChangedEvent) to handle cases where a location
103+ * was cached but not yet sent for the current user.
103104 */
104105 internal fun syncCachedLocationIfNeeded () {
105106 val lat = locationPreferenceStore.getCachedLatitude() ? : return
0 commit comments