Skip to content
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -105,9 +105,9 @@ extension KlaviyoLocationManager: CLLocationManagerDelegate {
}
}

extension KlaviyoLocationManager {
// MARK: Dwell Timer Management
// MARK: Dwell Timer Management

extension KlaviyoLocationManager {
private func startDwellTimer(for klaviyoLocationId: String) {
cancelDwellTimer(for: klaviyoLocationId)
guard let dwellSeconds = geofenceDwellSettings[klaviyoLocationId] else {
Expand Down Expand Up @@ -166,4 +166,32 @@ extension KlaviyoLocationManager {
Logger.geoservices.info("🕐 Dwell event fired for region \(klaviyoLocationId)")
}
}

/// Check for expired timers and fire dwell events for them
/// Called on app launch/foreground as a best-effort recovery mechanism
@MainActor
func checkExpiredDwellTimers() {
let expiredTimers = dwellTimerTracker.checkExpiredTimers(activeTimerIds: Set(dwellTimers.keys))

// Fire dwell events for expired timers
for (geofenceId, duration) in expiredTimers {
let dwellEvent = Event(
name: .locationEvent(.geofenceDwell),
properties: [
"$geofence_id": geofenceId,
"$geofence_dwell_duration": duration
]
)

Task {
await MainActor.run {
KlaviyoInternal.create(event: dwellEvent)
}
}

if #available(iOS 14.0, *) {
Logger.geoservices.info("🕐 Fired expired dwell event for region \(geofenceId) (expired while app was terminated)")
}
Copy link
Contributor

Choose a reason for hiding this comment

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

I think this should go inside the Task, below the full await clause. Technically the current order is incorrect, as you're dispatching an async task that may or may not complete, and then you're immediately logging the event before waiting for the task to finish

}
}
}
Loading