Skip to main content

API Reference

B2MetricSDK

Main SDK class for tracking events.


getInstance(context: Context): B2MetricSDK

Returns the singleton SDK instance.

Parameters:

  • context: Application or Activity context
val sdk = B2MetricSDK.getInstance(applicationContext)

start(config: B2MetricAnalyticsConfig)

Initializes and configures the SDK.

Parameters:

  • config: Configuration object with SDK settings
sdk.start(
B2MetricAnalyticsConfig(
apiKey = "YOUR_API_KEY",
batchSize = 20
)
)

logEvent(name: String, properties: Map<String, Any>? = null)

Tracks a custom event with optional properties.

Parameters:

  • name: Event name (required)
  • properties: Event properties (optional)
sdk.logEvent(
name = "button_clicked",
properties = mapOf("screen" to "home")
)

registerPushToken(token: String, provider: PushProvider)

Registers a push notification token and logs a push_token event.

Parameters:

  • token: FCM or HMS token
  • provider: PushProvider.FCM or PushProvider.HMS
sdk.registerPushToken(fcmToken, PushProvider.FCM)

trackPushOpened(data: Map<String, String>)

Tracks push notification open and logs a push_opened event.

Parameters:

  • data: Push notification data payload
override fun onMessageReceived(remoteMessage: RemoteMessage) {
sdk.trackPushOpened(remoteMessage.data)
}

B2MetricAnalyticsConfig

Configuration data class.

ParameterTypeDefaultRangeDescription
apiKeyString--Required. Your B2Metric API key
batchSizeInt201-100Batch size for event sending
flushIntervalSecondsLong301-3600Time between automatic flushes
maxRetriesInt31-10Maximum retry attempts
assumedSessionTimeInMinutesInt301-10080Session timeout duration
b2MetricAnalyticsLogLevelLogLevelOFF-Logging level

PushProvider

Enum for push notification providers.

  • PushProvider.FCM — Firebase Cloud Messaging
  • PushProvider.HMS — Huawei Mobile Services

B2MetricAnalyticsLogLevel

Enum for log levels.

ValueDescription
OFFNo logging
ERRORErrors only
WARNINGErrors and warnings
INFOGeneral information
DEBUGDetailed debug information