Usage Stats
The Usage Stats module keeps a history of the VPN connections your app reports and returns a summary of it: how often and how long the user connects, their streak of connected days and the locations they use. The history stays on the device. The SDK sends it nowhere; your app decides what to do with the summary.
The module needs a license that grants CategoryTracking, the same as the Tracking module.
Getting the Module
- Swift
- Kotlin
let stats = try manager.stats()
val stats = manager.stats()
All methods throw StatsException. In KapeClientSDK and client-sdk the methods are blocking and carry the plain names, such as connectionStart. In KapeClientSDKAsync and client-sdk-async the same methods are async and end in Async, such as connectionStartAsync. The examples on this page use the async names.
Consent
Nothing is recorded until you call opt_in. The choice is not stored: call opt_in after every start of your app, in every process that reports connections, before the first connection is reported. opt_out likewise applies only to the process it is called in: call it in every process that opted in, or that process keeps recording.
| Method | Description |
|---|---|
opt_in | Starts recording |
opt_out | Stops recording and deletes the recorded history |
is_opted_in | Whether connections are being recorded |
- Swift
- Kotlin
if userAllowsUsageStats {
stats.optIn()
} else {
try await stats.optOutAsync()
}
if (userAllowsUsageStats) {
stats.optIn()
} else {
stats.optOutAsync()
}
Reporting Connections
Tell the module when a VPN session starts, that it is still up, and when it ends.
| Method | Parameters | When to call it |
|---|---|---|
connection_start | location_id, country_code | A VPN session is established |
connection_alive | location_id, country_code | Periodically while connected, for example every few minutes |
connection_end | The session ends |
location_id is your identifier for the VPN location. country_code is its 2-letter ISO country code, such as US.
- Swift
- Kotlin
try await stats.connectionStartAsync(locationId: "usa-new-york", countryCode: "US")
try await stats.connectionAliveAsync(locationId: "usa-new-york", countryCode: "US")
try await stats.connectionEndAsync()
stats.connectionStartAsync(locationId = "usa-new-york", countryCode = "US")
stats.connectionAliveAsync(locationId = "usa-new-york", countryCode = "US")
stats.connectionEndAsync()
connection_alive is what keeps the history correct when an end is never reported, for example because the VPN process was stopped:
- a session that is not reported alive for 24 hours counts as ended at the last time it was;
- if no session is open, or the location changed,
connection_alivestarts a new one.
Pass the same location_id to connection_alive as to connection_start, or each call is taken for a change of location.
Reading the Stats
read_usage_stats returns the summary up to the previous day. Days are UTC days. It throws StatsException.OptedOut while the user is opted out.
- Swift
- Kotlin
let usage = try await stats.readUsageStatsAsync()
let streak = usage.connection.connectionDaysStreak
let topCountry = usage.location.topLocationCountryLast30Days
val usage = stats.readUsageStatsAsync()
val streak = usage.connection.connectionDaysStreak
val topCountry = usage.location.topLocationCountryLast30Days
UsageStats.connection
The first three values exist for the last 7, 14, 30, 60 and 90 days, for example days_connected_last_7_days.
| Name | Type | Description |
|---|---|---|
| days_connected_last_N_days | UInt32 | Days with at least one connection |
| avg_connections_last_N_days | UInt32 | Connections per connected day, rounded. 0 when there were none |
| avg_connection_length_last_N_days | UInt32 | Minutes per connection, rounded. 0 when there were none |
| connection_days_streak | UInt32 | Days in a row with a connection, up to the previous day. 0 if it had none |
UsageStats.location
| Name | Type | Description |
|---|---|---|
| locations_used_last_30_days | UInt32 | Different locations connected to |
| top_location_country_last_30_days | String? | Country connected to on the most days. A tie goes to the one used last. nil when there were none |
Deleting the History
The history is deleted when the user logs out, when another user logs in, and on opt_out. To delete it at another time and keep recording, call clear.
Connections older than 95 days are removed automatically. The streak is not affected by that.