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.
- Multiplatform Support: Android, iOS, Desktop (JVM), Web (JS/Wasm), macOS, tvOS, watchos.
- Persistence: Events are queued and persisted (using SQLDelight)
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 :
- apiSecret : Found under Admin > Data Streams > Choose your stream > Measurement Protocol > Create.
Private to your organization. Should be regularly updated to avoid excessive SPAM. - measurementId : Found under Admin > Data Streams > choose your stream > Measurement ID.
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.