
GA4 analytics via Measurement Protocol v2, sending events directly from app code; queues and persists events for reliability; supports custom events, page-view/search tracking, user properties and consent controls.
A Kotlin Multiplatform library for Google Analytics (GA4) that supports all major targets (Android, iOS, Desktop, Web, macOS, tvOS, watchos). This library uses the Measurement Protocol (v2) to send events directly to Google Analytics from your shared code.
[!IMPORTANT]
This library uses the Google Analytics API, not the Firebase API.As the GA API is optimized for the web, you need to follow this documentation to create events.
[!IMPORTANT]
Due to the nature of Google Analytics, you can't use the tracker locally, as a CORS error will be triggered.
// build.gradle.kts
commonMain {
dependencies {
implementation("io.github.frankois944:googleAnalyticsKMPTracker:LATEST_VERSION")
}
}Initialize the tracker in your shared code. On Android, a Context is required for disk persistence and device info.
Found in the Google Analytics UI :
val tracker = Tracker.create(
apiSecret = "YOUR_API_SECRET",
measurementId = "G-XXXXXXXXXX",
context = androidContext // Mandatory for Android, null otherwise
)You can track custom events with parameters:
tracker.trackEvent(
name = "button_click",
parameters = mapOf(
"button_id" to "login_submit",
"screen_name" to "LoginScreen"
)
)Track screen transitions easily:
// Simple view tracking
tracker.trackView("HomeScreen")
// Tracking nested navigation paths
tracker.trackView(listOf("Main", "Settings", "Profile"))tracker.trackSearch("Kotlin Multiplatform")// Set unique user ID
tracker.setUserId("user_123456")
// Set custom user properties
tracker.setUserProperty("membership_level", "premium")tracker.enableAdUserData(false)
tracker.enableAdPersonalization(false)This project is licensed under the MIT.
A Kotlin Multiplatform library for Google Analytics (GA4) that supports all major targets (Android, iOS, Desktop, Web, macOS, tvOS, watchos). This library uses the Measurement Protocol (v2) to send events directly to Google Analytics from your shared code.
[!IMPORTANT]
This library uses the Google Analytics API, not the Firebase API.As the GA API is optimized for the web, you need to follow this documentation to create events.
[!IMPORTANT]
Due to the nature of Google Analytics, you can't use the tracker locally, as a CORS error will be triggered.
// build.gradle.kts
commonMain {
dependencies {
implementation("io.github.frankois944:googleAnalyticsKMPTracker:LATEST_VERSION")
}
}Initialize the tracker in your shared code. On Android, a Context is required for disk persistence and device info.
Found in the Google Analytics UI :
val tracker = Tracker.create(
apiSecret = "YOUR_API_SECRET",
measurementId = "G-XXXXXXXXXX",
context = androidContext // Mandatory for Android, null otherwise
)You can track custom events with parameters:
tracker.trackEvent(
name = "button_click",
parameters = mapOf(
"button_id" to "login_submit",
"screen_name" to "LoginScreen"
)
)Track screen transitions easily:
// Simple view tracking
tracker.trackView("HomeScreen")
// Tracking nested navigation paths
tracker.trackView(listOf("Main", "Settings", "Profile"))tracker.trackSearch("Kotlin Multiplatform")// Set unique user ID
tracker.setUserId("user_123456")
// Set custom user properties
tracker.setUserProperty("membership_level", "premium")tracker.enableAdUserData(false)
tracker.enableAdPersonalization(false)This project is licensed under the MIT.