InAppAnalytics class. All methods are Kotlin suspend functions and must be called from a coroutine scope.import horizon.platform.inappanalytics.InAppAnalytics import horizon.platform.inappanalytics.InAppAnalyticsException val inAppAnalytics = InAppAnalytics()
try {
val segment = inAppAnalytics.openSegment("gameplay_round")
Log.d(TAG, "Segment opened: segId=${segment.segId}, segName=${segment.segName}")
} catch (e: InAppAnalyticsException) {
Log.e(TAG, "Failed to open segment: ${e.displayableMessage}")
}
try {
val segment = inAppAnalytics.closeSegment("gameplay_round")
Log.d(TAG, "Segment closed: duration=${segment.durationS}s")
} catch (e: InAppAnalyticsException) {
Log.e(TAG, "Failed to close segment: ${e.displayableMessage}")
}
try {
val closedSegments = inAppAnalytics.closeAllOpenSegments()
Log.d(TAG, "Closed ${closedSegments.size} segment(s)")
} catch (e: InAppAnalyticsException) {
Log.e(TAG, "Failed to close segments: ${e.displayableMessage}")
}
try {
val segments = inAppAnalytics.getAllSegments()
for (segment in segments) {
Log.d(TAG, "Active segment: ${segment.segName}")
}
} catch (e: InAppAnalyticsException) {
Log.e(TAG, "Failed to get segments: ${e.displayableMessage}")
}
sendEvent with a metric name and a numeric value:try {
inAppAnalytics.sendEvent("player_score", 1500.0f)
} catch (e: InAppAnalyticsException) {
Log.e(TAG, "Failed to send event: ${e.displayableMessage}")
}
queueMetricEvent with a MetricEventInput:import horizon.platform.inappanalytics.enums.MetricType
import horizon.platform.inappanalytics.options.MetricEventInput
try {
val event = MetricEventInput.builder()
.withMetricName("lap_time")
.withValue(62.5f)
.withMetricType(MetricType.Action)
.build()
inAppAnalytics.queueMetricEvent(event)
} catch (e: InAppAnalyticsException) {
Log.e(TAG, "Failed to queue event: ${e.displayableMessage}")
}
SegmentEventInput:import horizon.platform.inappanalytics.enums.SegmentEventType
import horizon.platform.inappanalytics.enums.SegmentType
import horizon.platform.inappanalytics.options.SegmentEventInput
try {
val event = SegmentEventInput.builder()
.withSegName("tutorial_step_3")
.withSegType(SegmentType.Tutorial)
.withEventType(SegmentEventType.Start)
.build()
inAppAnalytics.queueSegmentEvent(event)
} catch (e: InAppAnalyticsException) {
Log.e(TAG, "Failed to queue segment event: ${e.displayableMessage}")
}
try {
// Create a counter (starts at value 1)
inAppAnalytics.createEventCounter("enemies_defeated")
// Increment as events occur
inAppAnalytics.incrementEventCounter("enemies_defeated", 1.0f)
inAppAnalytics.incrementEventCounter("enemies_defeated", 3.0f)
// Check the current value
val counter = inAppAnalytics.getEventCounter("enemies_defeated")
Log.d(TAG, "Current count: ${counter.value}")
// Send the counter as a metric event (removes it from tracking)
inAppAnalytics.sendEventCounter("enemies_defeated")
} catch (e: InAppAnalyticsException) {
Log.e(TAG, "Counter error: ${e.displayableMessage}")
}
manualFlush = true when creating a counter to disable auto-flush. You are then responsible for calling sendEventCounter explicitly:inAppAnalytics.createEventCounter("coins_collected", manualFlush = true)
try {
val counterNames = inAppAnalytics.getAllEventCounterNames()
for (name in counterNames.names) {
Log.d(TAG, "Tracking counter: $name")
}
} catch (e: InAppAnalyticsException) {
Log.e(TAG, "Failed to get counter names: ${e.displayableMessage}")
}
| Method | Description | Returns |
|---|---|---|
openSegment(segName) | Opens a named segment and records a START event. | SegmentEvent |
closeSegment(segName) | Closes a named segment and records an END event with duration. | SegmentEvent |
closeAllOpenSegments() | Closes all active segments. | List<SegmentEvent> |
getAllSegments() | Returns all currently active segments without closing them. | List<SegmentEvent> |
sendEvent(metricName, value) | Sends a single metric event immediately. | Void |
queueMetricEvent(event) | Queues a metric event for batch processing. | Void |
queueSegmentEvent(event) | Queues a segment event for batch processing. | Void |
createEventCounter(counterName, manualFlush?) | Creates a counter with an initial value of 1. | MetricEvent |
incrementEventCounter(counterName, amount) | Increments an existing counter by the given amount. | MetricEvent |
sendEventCounter(counterName) | Sends the counter value and removes the counter. | MetricEvent |
getEventCounter(counterName) | Gets the current counter value without sending. | MetricEvent |
getAllEventCounterNames() | Returns the names of all tracked counters. | EventCounterNames |
suspend functions and throw InAppAnalyticsException on failure.