From 817ab89164c3f34fca95f343cab99e50ac64ecf2 Mon Sep 17 00:00:00 2001 From: Vincent TE Date: Wed, 20 Nov 2024 10:28:35 +0100 Subject: [PATCH] chore: Add method to track destination --- .../src/main/java/com/infomaniak/matomo/Matomo.kt | 4 ++-- .../swisstransfer/ui/MatomoSwissTransfer.kt | 11 +++++++++++ .../swisstransfer/ui/screen/main/MainScreen.kt | 4 ++++ .../ui/screen/newtransfer/NewTransferScreen.kt | 9 +++++++++ 4 files changed, 26 insertions(+), 2 deletions(-) diff --git a/Core2/Matomo/src/main/java/com/infomaniak/matomo/Matomo.kt b/Core2/Matomo/src/main/java/com/infomaniak/matomo/Matomo.kt index ca4ec78b4..b631627d0 100644 --- a/Core2/Matomo/src/main/java/com/infomaniak/matomo/Matomo.kt +++ b/Core2/Matomo/src/main/java/com/infomaniak/matomo/Matomo.kt @@ -66,8 +66,8 @@ interface Matomo { TrackHelper.track().screen(this).title(this::class.java.simpleName).with(tracker) } - fun Context.trackScreen(path: String, title: String) { - TrackHelper.track().screen(path).title(title).with(tracker) + fun Context.trackScreen(path: String) { + TrackHelper.track().screen(path).with(tracker) } //endregion diff --git a/app/src/main/java/com/infomaniak/swisstransfer/ui/MatomoSwissTransfer.kt b/app/src/main/java/com/infomaniak/swisstransfer/ui/MatomoSwissTransfer.kt index c75da2694..d4183f9de 100644 --- a/app/src/main/java/com/infomaniak/swisstransfer/ui/MatomoSwissTransfer.kt +++ b/app/src/main/java/com/infomaniak/swisstransfer/ui/MatomoSwissTransfer.kt @@ -17,12 +17,23 @@ */ package com.infomaniak.swisstransfer.ui +import android.annotation.SuppressLint import android.content.Context +import androidx.navigation.NavDestination import com.infomaniak.matomo.Matomo +import com.infomaniak.swisstransfer.BuildConfig import org.matomo.sdk.Tracker object MatomoSwissTransfer : Matomo { override val Context.tracker: Tracker get() = buildTracker() // TODO: Fetch appSettings for opt-out override val siteId: Int = 24 + + // This `SuppressLint` is there so the CI can build + @SuppressLint("RestrictedApi") + fun Context.trackDestination(navDestination: NavDestination) = with(navDestination) { + route?.substringAfter("${BuildConfig.APPLICATION_ID}.ui.navigation.")?.let { path -> + trackScreen(path) + } + } } diff --git a/app/src/main/java/com/infomaniak/swisstransfer/ui/screen/main/MainScreen.kt b/app/src/main/java/com/infomaniak/swisstransfer/ui/screen/main/MainScreen.kt index a42714170..bed485dde 100644 --- a/app/src/main/java/com/infomaniak/swisstransfer/ui/screen/main/MainScreen.kt +++ b/app/src/main/java/com/infomaniak/swisstransfer/ui/screen/main/MainScreen.kt @@ -21,8 +21,10 @@ import androidx.compose.runtime.Composable import androidx.compose.runtime.derivedStateOf import androidx.compose.runtime.getValue import androidx.compose.runtime.remember +import androidx.compose.ui.platform.LocalContext import androidx.navigation.compose.currentBackStackEntryAsState import androidx.navigation.compose.rememberNavController +import com.infomaniak.swisstransfer.ui.MatomoSwissTransfer.trackDestination import com.infomaniak.swisstransfer.ui.components.BrandTopAppBar import com.infomaniak.swisstransfer.ui.navigation.MainNavigation import com.infomaniak.swisstransfer.ui.navigation.NavigationDestination.Companion.toDestination @@ -40,6 +42,8 @@ fun MainScreen() { derivedStateOf { navBackStackEntry?.toDestination() ?: MainNavigation.startDestination } } + navBackStackEntry?.destination?.let { LocalContext.current.trackDestination(it) } + MainScaffold( navController = navController, currentDestination = currentDestination, diff --git a/app/src/main/java/com/infomaniak/swisstransfer/ui/screen/newtransfer/NewTransferScreen.kt b/app/src/main/java/com/infomaniak/swisstransfer/ui/screen/newtransfer/NewTransferScreen.kt index e12920249..f308a7899 100644 --- a/app/src/main/java/com/infomaniak/swisstransfer/ui/screen/newtransfer/NewTransferScreen.kt +++ b/app/src/main/java/com/infomaniak/swisstransfer/ui/screen/newtransfer/NewTransferScreen.kt @@ -18,13 +18,22 @@ package com.infomaniak.swisstransfer.ui.screen.newtransfer import androidx.compose.runtime.Composable +import androidx.compose.runtime.getValue +import androidx.compose.ui.platform.LocalContext +import androidx.navigation.compose.currentBackStackEntryAsState import androidx.navigation.compose.rememberNavController +import com.infomaniak.swisstransfer.ui.MatomoSwissTransfer.trackDestination import com.infomaniak.swisstransfer.ui.theme.SwissTransferTheme import com.infomaniak.swisstransfer.ui.utils.PreviewAllWindows @Composable fun NewTransferScreen(closeActivity: () -> Unit) { val navController = rememberNavController() + + val navBackStackEntry by navController.currentBackStackEntryAsState() + + navBackStackEntry?.destination?.let { LocalContext.current.trackDestination(it) } + NewTransferNavHost(navController, closeActivity) }