-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(ios): track and export network change events
- Loading branch information
Adwin Ronald Ross
committed
Dec 24, 2024
1 parent
cd54d5e
commit 6edc541
Showing
15 changed files
with
343 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
# Feature - Network Changes | ||
|
||
Measure SDK captures changes to network connection state of the device. This includes changes to the type of network connection (WiFi, Mobile, etc.), and the network generation (2G, 3G, 4G, etc.). | ||
|
||
## How it works | ||
|
||
To detect network changes, Measure relies on NWPathMonitor's `pathUpdateHandler`. The `pathUpdateHandler` provides a callback whenever the network changes between cellular and Wi-Fi. Additionally, we perform independent checks to detect if the connection is accessed via VPN or if no internet is available. | ||
|
||
We rely on `CTTelephonyNetworkInfo` to get carrier data. However, starting from iOS 16.4, access to the carrier name is no longer available. | ||
|
||
## Data collected | ||
|
||
Checkout the data collected by Measure for each network change in the [Network Event](../../api/sdk/README.md#networkchange) section. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
// | ||
// NetworkChangeCallback.swift | ||
// MeasureSDK | ||
// | ||
// Created by Adwin Ross on 23/12/24. | ||
// | ||
|
||
import Foundation | ||
|
||
final class NetworkChangeCallback { | ||
var onNetworkChangeCallback: ((_ data: NetworkChangeData) -> Void)? | ||
|
||
func onNetworkChange(_ data: NetworkChangeData) { | ||
onNetworkChangeCallback?(data) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
// | ||
// NetworkChangeCollector.swift | ||
// MeasureSDK | ||
// | ||
// Created by Adwin Ross on 20/12/24. | ||
// | ||
|
||
import Foundation | ||
|
||
protocol NetworkChangeCollector { | ||
func enable() | ||
} | ||
|
||
final class BaseNetworkChangeCollector: NetworkChangeCollector { | ||
private let logger: Logger | ||
private let eventProcessor: EventProcessor | ||
private let timeProvider: TimeProvider | ||
private let networkChangeDetector: NetworkChangeDetector | ||
private let networkChangeCallback: NetworkChangeCallback | ||
|
||
init(logger: Logger, eventProcessor: EventProcessor, timeProvider: TimeProvider) { | ||
self.logger = logger | ||
self.eventProcessor = eventProcessor | ||
self.timeProvider = timeProvider | ||
self.networkChangeCallback = NetworkChangeCallback() | ||
self.networkChangeDetector = BaseNetworkChangeDetector(networkChangeCallback: self.networkChangeCallback) | ||
self.networkChangeCallback.onNetworkChangeCallback = onNetworkChangeCallback(_:) | ||
} | ||
|
||
func enable() { | ||
logger.internalLog(level: .debug, message: "GestureCollector enabled", error: nil, data: nil) | ||
networkChangeDetector.start() | ||
} | ||
|
||
func onNetworkChangeCallback(_ data: NetworkChangeData) { | ||
eventProcessor.track(data: data, | ||
timestamp: timeProvider.now(), | ||
type: .networkChange, | ||
attributes: nil, | ||
sessionId: nil, | ||
attachments: nil) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
// | ||
// NetworkChangeData.swift | ||
// MeasureSDK | ||
// | ||
// Created by Adwin Ross on 20/12/24. | ||
// | ||
|
||
import Foundation | ||
|
||
/// Represents the data related to network changes. | ||
class NetworkChangeData: Codable { | ||
/// The [NetworkType] of the network that was previously active. This is null if there was no previously active network. | ||
let previousNetworkType: NetworkType | ||
|
||
/// The [NetworkType] of the network that is now active. | ||
let networkType: NetworkType | ||
|
||
/// The [NetworkGeneration] of the network that was previously active. Only set for cellular networks. | ||
let previousNetworkGeneration: NetworkGeneration | ||
|
||
/// The [NetworkGeneration] of the network that is now active. | ||
let networkGeneration: NetworkGeneration | ||
|
||
/// The name of the network provider that is now active. Only set for cellular networks. | ||
let networkProvider: String | ||
|
||
/// Coding keys to map snake_case JSON keys to camelCase properties. | ||
private enum CodingKeys: String, CodingKey { | ||
case previousNetworkType = "previous_network_type" | ||
case networkType = "network_type" | ||
case previousNetworkGeneration = "previous_network_generation" | ||
case networkGeneration = "network_generation" | ||
case networkProvider = "network_provider" | ||
} | ||
|
||
init(previousNetworkType: NetworkType, networkType: NetworkType, previousNetworkGeneration: NetworkGeneration, networkGeneration: NetworkGeneration, networkProvider: String) { | ||
self.previousNetworkType = previousNetworkType | ||
self.networkType = networkType | ||
self.previousNetworkGeneration = previousNetworkGeneration | ||
self.networkGeneration = networkGeneration | ||
self.networkProvider = networkProvider | ||
} | ||
} |
Oops, something went wrong.