Track button clicks with the Matomo Android SDK
User interactions, such as button clicks, are recorded when you explicitly track events in your app. These interactions help you understand how users engage with specific features and actions.
This guide explains how to track button clicks in your Android app using Matomo. The example uses a Jetpack Compose app, where interactions are handled within composable functions.
Before you start
Before continuing, ensure tracking is already set up and working in your app. Read more on setting up tracking with the Matomo SDK for Android.
Track a button click
The TrackHelper class is used to send individual tracking requests (hits), such as screen views, events, goals, and custom dimensions. To track a button click, you need to add the event tracking call inside the button’s onClick handler.
If you are tracking views, keep the screen tracking in a LaunchedEffect block so it runs when the screen becomes visible. The tracker is retrieved from the global instance defined in MatomoApp.kt, so all tracking in the app uses the same configuration.
- Go to the kotlin/ (or java/) directory and open the file that defines your screen composable, for example,
HomeScreen.kt. - When you add event tracking to record when a button is pressed, the code retrieves the Matomo tracker instance and the
TrackHelper.track().event()call sends an event to Matomo. - The example event includes a category (“
Button“), action (“Click“), and an optional name (“Download-btn“) to identify which button was pressed. - If the button triggers navigation, perform the navigation after sending the event to help ensure the tracking request is sent first.
@Composable
fun HomeScreen(modifier: Modifier = Modifier) {
val tracker: Tracker =
(LocalContext.current.applicationContext as MatomoApp).getTracker()
Column(modifier = modifier) {
Button(
onClick = {
TrackHelper.track()
.event("Button", "Click")
.name("Download-btn")
.with(tracker)
tracker.dispatch()
// Add navigation here if required
}
) {
Text("Download")
}
}
}
Example with screen view and button click tracking
@Composable
fun HomeScreen(modifier: Modifier = Modifier) {
// Retrieve the shared tracker instance from MatomoApp
val tracker: Tracker =
(LocalContext.current.applicationContext as MatomoApp).getTracker()
// --- Screen view tracking (See guide on how to track screen views) ---
// Runs when the composable is first displayed
LaunchedEffect(Unit) {
TrackHelper.track()
.screen("/home") // Unique screen path
.title("Home") // Screen title shown in Matomo
.with(tracker)
tracker.dispatch() // Send the tracking request
}
Column(modifier = modifier) {
Text("Home screen")
// --- Button click tracking ---
Button(
onClick = {
TrackHelper.track()
.event("Button", "Click")
.name("Download-btn")
.with(tracker)
tracker.dispatch() // Send the tracking request
// Add navigation here if required
}
) {
Text("Download")
}
}
}
Test button click tracking
After setting up tracking, test that the button click event is sent to Matomo.
-
Run the app and press the tracked button.
-
In your Matomo instance, go to the Real-time dashboard or Visitors > Visits Log and confirm the button click event appears in the report.
-
After the reports have processed, go to Behaviour > Events to analyse button interaction data.
Next steps
Explore additional Matomo SDK for Android guides to track goals, custom dimensions, and user IDs.