Matomo analytics is a google analytics alternative. You can self-host matomo and do all kinds of things you wish with it since it'll be on your server. This piece helps you set up matomo and use it to log events in your jetpack compose screens. I'm going to assume you already have a matomo server running and created a site. You need the site id and token
Dependencies
Before you start logging the events, you need the matomo SDK installed. First, add the maven repository and then the matomo SDK in your dependencies.
repositories {
//--------
maven { url "https://jitpack.io" }
//-----
}
App build.gradle
Add the matomo SDK into your app build.gradle.
dependencies {
//---------
implementation "org.matomo.sdk:tracker:4.1.1"
//----
}
Initialize tracker
Create App.java in the root src directory.
package com.example;
import org.matomo.sdk.TrackerBuilder;
import org.matomo.sdk.extra.MatomoApplication;
public class App extends MatomoApplication {
@Override
public TrackerBuilder onCreateTrackerConfig() {
// takes the matomo endpoint and the site
return TrackerBuilder.createDefault("https://stats.example.com/matomo.php", 1);
}
}
After instantiating your matomo tracker in the App.java. Add it into the manifest file androidManifest.xml by adding this line inside the application tag.
<?xml version="1.0" encoding="utf-8"?>
<manifest>
<application
android:name=".App"
>
</application>
</manifest>
Tracking the events in the app.
This is how you can use it in your jetpack compose screens.
import org.matomo.sdk.extra.MatomoApplication
import org.matomo.sdk.extra.TrackHelper
import androidx.compose.ui.platform.LocalContext
@Composable
fun Home(){
val context = LocalContext.current
LaunchedEffect(Unit) {
val tracker = (getApplication(context.applicationContext) as MatomoApplication).tracker
TrackHelper.track().screen("/").title("Home")
.with(tracker)
}
Column(){
Text(text="Hello World")
}
}