Join Login Create a Request

How to add Matomo analytics to an android Kotlin App (Jetpack Compose)

|
How to add Matomo analytics to an android Kotlin App (Jetpack Compose)

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")
    }
}

Tags.




Recent Posts

How to use robot.txt on a Django Website
How to use robot.txt on a Django Website
How to add a robots.txt file on a Django website so that you..
Read More
Top sites for remote jobs
Top sites for remote jobs
Where to find remote jobs for software developers in 2022...
Read More
Project ideas for a software developer portfolio
Project ideas for a software developer portfolio
Projects which can strengthen your portfolio as a software d..
Read More
My case for WordPress
My case for WordPress
WordPress strengths and weaknesses. When you should use Word..
Read More