API Reference
B2MetricAnalytics
Main SDK class for tracking events.
start(apiKey:batchSize:flushInterval:)
Initializes and configures the SDK. Must be called before tracking any events.
Parameters:
apiKey(String): Required. Your B2Metric API keybatchSize(Int): Number of events to batch before sending. Default:20flushInterval(TimeInterval): Seconds between automatic flushes. Default:30
B2MetricAnalytics.shared.start(
apiKey: "YOUR_API_KEY",
batchSize: 20,
flushInterval: 30
)
logEvent(name:properties:)
Tracks a custom event with optional properties.
Parameters:
name(String): Event name (required)properties([String: AnyCodable]?): Event properties (optional)
Supported property types: String, Int, Double, Bool, Date
B2MetricAnalytics.shared.logEvent(
name: "button_clicked",
properties: [
"screen": AnyCodable("home"),
"button_text": AnyCodable("Sign Up")
]
)
registerDeviceToken(_:)
Registers an APNs device token and logs a push_token event.
Parameters:
deviceToken(Data): APNs device token
func application(
_ application: UIApplication,
didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data
) {
B2MetricAnalytics.shared.registerDeviceToken(deviceToken)
}
trackPushOpened(userInfo:)
Tracks push notification open and logs a push_opened event.
Parameters:
userInfo([AnyHashable: Any]): Push notification payload
func application(
_ application: UIApplication,
didReceiveRemoteNotification userInfo: [AnyHashable: Any]
) {
B2MetricAnalytics.shared.trackPushOpened(userInfo: userInfo)
}
AutoTracker
Automatic screen view tracking for UIKit.
start()
Enables automatic screen view tracking for UIViewController subclasses.
// In AppDelegate
AutoTracker.shared.start()
View Extensions (SwiftUI)
.trackScreen(_:)
View modifier for tracking screen views in SwiftUI.
Parameters:
customName(String?): Optional custom screen name
struct ContentView: View {
var body: some View {
Text("Welcome")
.trackScreen("HomeScreen")
}
}
LogLevel
Enum controlling SDK logging verbosity.
| Level | Description |
|---|---|
.off | No logging |
.error | Only critical errors |
.warning | Errors and warnings |
.info | General SDK operations |
.debug | Detailed debugging info |
B2MetricAnalytics.shared.logLevel = .debug