-
Notifications
You must be signed in to change notification settings - Fork 14
Create Geofence events #437
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
9b5568c
8ea7928
d018f16
0bf2b67
5211541
5278606
dfcb9e5
b729d93
c18fcd4
915ab6a
9e58bb1
183bc4e
55049a2
47b7ed1
81c2f94
1d42799
4f2e5f7
5aa7e31
58e189a
fbd17f9
6da4b75
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -12,20 +12,20 @@ import KlaviyoCore | |
| import KlaviyoSwift | ||
| import OSLog | ||
|
|
||
| public class KlaviyoLocationManager: NSObject { | ||
| class KlaviyoLocationManager: NSObject { | ||
| static let shared = KlaviyoLocationManager() | ||
|
|
||
| private var locationManager: LocationManagerProtocol | ||
| private let geofenceManager: KlaviyoGeofenceManager | ||
| private let geofencePublisher: PassthroughSubject<String, Never> = .init() | ||
|
|
||
| internal init(locationManager: LocationManagerProtocol? = nil, geofenceManager: KlaviyoGeofenceManager? = nil) { | ||
| init(locationManager: LocationManagerProtocol? = nil, geofenceManager: KlaviyoGeofenceManager? = nil) { | ||
| self.locationManager = locationManager ?? CLLocationManager() | ||
| self.geofenceManager = geofenceManager ?? KlaviyoGeofenceManager(locationManager: self.locationManager) | ||
|
|
||
| super.init() | ||
| self.locationManager.delegate = self | ||
| self.locationManager.allowsBackgroundLocationUpdates = true | ||
| self.locationManager.startMonitoringSignificantLocationChanges() | ||
belleklaviyo marked this conversation as resolved.
Show resolved
Hide resolved
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Bug: Unconditional Background Location Causes App CrashhraseSetting
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this will be part of our documentation/setup requirements so.... |
||
| } | ||
|
|
||
| deinit { | ||
|
|
@@ -36,14 +36,14 @@ public class KlaviyoLocationManager: NSObject { | |
| } | ||
|
|
||
| @MainActor | ||
| internal func setupGeofencing() { | ||
| func setupGeofencing() { | ||
| if environment.getLocationAuthorizationStatus() == .authorizedAlways { | ||
| geofenceManager.setupGeofencing() | ||
| } | ||
| } | ||
|
|
||
| @MainActor | ||
| internal func destroyGeofencing() { | ||
| func destroyGeofencing() { | ||
| geofenceManager.destroyGeofencing() | ||
| } | ||
| } | ||
|
|
@@ -90,43 +90,53 @@ extension KlaviyoLocationManager: CLLocationManagerDelegate { | |
| // MARK: Geofencing | ||
|
|
||
| public func locationManager(_ manager: CLLocationManager, didEnterRegion region: CLRegion) { | ||
| guard let region = region as? CLCircularRegion else { return } | ||
| guard let region = region as? CLCircularRegion, | ||
| let klaviyoLocationId = region.klaviyoLocationId else { | ||
| if #available(iOS 14.0, *) { | ||
| Logger.geoservices.info("Received non-Klaviyo geofence notification. Skipping.") | ||
| } | ||
| return | ||
| } | ||
| if #available(iOS 14.0, *) { | ||
| Logger.geoservices.info("🌎 User entered region \"\(region.identifier)\"") | ||
| Logger.geoservices.info("🌎 User entered region \"\(klaviyoLocationId, privacy: .public)\"") | ||
| } | ||
|
|
||
| let enterEvent = Event( | ||
| name: .locationEvent(.enteredBoundary), | ||
| name: .locationEvent(.geofenceEnter), | ||
| properties: [ | ||
| "boundaryIdentifier": region.identifier | ||
| "geofence_id": klaviyoLocationId | ||
| ] | ||
| ) | ||
|
|
||
| Task { | ||
| await MainActor.run { | ||
| KlaviyoInternal.create(event: enterEvent) | ||
| geofencePublisher.send("Entered \(region.identifier)") | ||
| } | ||
| } | ||
| } | ||
|
|
||
| public func locationManager(_ manager: CLLocationManager, didExitRegion region: CLRegion) { | ||
| guard let region = region as? CLCircularRegion else { return } | ||
| guard let region = region as? CLCircularRegion, | ||
| let klaviyoLocationId = region.klaviyoLocationId else { | ||
| if #available(iOS 14.0, *) { | ||
| Logger.geoservices.warning("Received non-Klaviyo geofence notification. Skipping.") | ||
belleklaviyo marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
| return | ||
| } | ||
| if #available(iOS 14.0, *) { | ||
| Logger.geoservices.info("🌎 User exited region \"\(region.identifier)\"") | ||
| Logger.geoservices.info("🌎 User exited region \"\(klaviyoLocationId, privacy: .public)\"") | ||
| } | ||
|
|
||
| let exitEvent = Event( | ||
| name: .locationEvent(.exitedBoundary), | ||
| name: .locationEvent(.geofenceExit), | ||
| properties: [ | ||
| "boundaryIdentifier": region.identifier | ||
| "geofence_id": klaviyoLocationId | ||
| ] | ||
| ) | ||
|
|
||
| Task { | ||
| await MainActor.run { | ||
| KlaviyoInternal.create(event: exitEvent) | ||
| geofencePublisher.send("Exited \(region.identifier)") | ||
| } | ||
| } | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,6 +5,7 @@ | |
| // Created by Isobelle Lim on 8/27/25. | ||
| // | ||
|
|
||
| import CoreLocation | ||
| import Foundation | ||
| import KlaviyoSwift | ||
|
|
||
|
|
@@ -20,6 +21,11 @@ extension KlaviyoSDK { | |
| } | ||
| } | ||
|
|
||
| /// To be called in didFinishLaunchingWithOptions to ensure geofence events that happen in a backgrounded/terminated state are processed. | ||
| public func monitorGeofencesFromBackground() { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can't we do this within our SDK instead of having the developer add this in?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not really, from my understanding,
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can't we either hook into foregrounded life cycle event and or have something in initialize since that gets called when the app is launch and have a hook in location that observers something in KlaviyoSwift and does this?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hmm the foregrounded life cycle event only is fired when the app is actually opened, so this wouldn't work in the terminated state. As for the initialize hook, I've been keeping geofencing methods pretty separate from it with the late initialization case in mind. (Part of that will be making KlaviyoLocation observe companyId changes and update the geofences then but unrelated to this |
||
| _ = KlaviyoLocationManager.shared | ||
| } | ||
belleklaviyo marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| /// Unregisters app for geofencing. Stops monitoring for geofences and cleans up resources. | ||
| @MainActor | ||
| public func unregisterGeofencing() { | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So more context about this -- it's a sensible guard to put in anyways where we would expect there to be a valid API key to use with our
fetchGeofencescall. But there's some weirdness where when CLLocationManager() is instantiated (which we do on launch to ensure background/terminated support) it also triggersdidChangeAuthorizationeven if the actual auth level has not changed. This fires before initialization is able to complete, so as a result when we triggersetupGeofencesbecause we have the correct auth level, we may not have the actual API key. This results in fetching the geofences for a nil company which returns empty which then clears any existing geofences.tldr; we need this to make sure we can support being responsive to authorization level changes (such as if they revoke it and we need to unregister our geofences) while also carefully handling the timing issues between when CoreLocation is setting up and the app can be initialized