Skip to main content

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.

info

The module needs a license that grants CategoryTracking, the same as the Tracking module.

Getting the Module​

let stats = try 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.

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.

MethodDescription
opt_inStarts recording
opt_outStops recording and deletes the recorded history
is_opted_inWhether connections are being recorded
if userAllowsUsageStats {
stats.optIn()
} else {
try await stats.optOutAsync()
}

Reporting Connections​

Tell the module when a VPN session starts, that it is still up, and when it ends.

MethodParametersWhen to call it
connection_startlocation_id, country_codeA VPN session is established
connection_alivelocation_id, country_codePeriodically while connected, for example every few minutes
connection_endThe session ends

location_id is your identifier for the VPN location. country_code is its 2-letter ISO country code, such as US.

try await stats.connectionStartAsync(locationId: "usa-new-york", countryCode: "US")
try await stats.connectionAliveAsync(locationId: "usa-new-york", countryCode: "US")
try await 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_alive starts 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.

let usage = try await stats.readUsageStatsAsync()
let streak = usage.connection.connectionDaysStreak
let 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.

NameTypeDescription
days_connected_last_N_daysUInt32Days with at least one connection
avg_connections_last_N_daysUInt32Connections per connected day, rounded. 0 when there were none
avg_connection_length_last_N_daysUInt32Minutes per connection, rounded. 0 when there were none
connection_days_streakUInt32Days in a row with a connection, up to the previous day. 0 if it had none

UsageStats.location​

NameTypeDescription
locations_used_last_30_daysUInt32Different locations connected to
top_location_country_last_30_daysString?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.