From afc6f392c6079ee0b079aa54d6be98545d4b38b7 Mon Sep 17 00:00:00 2001 From: aminecmi Date: Sat, 12 Nov 2022 22:58:42 +0100 Subject: [PATCH] Initial Matomo integration. --- .drone.yml | 12 +++++++++-- .github/CONTRIBUTING.md | 9 ++++---- androidApp/build.gradle.kts | 9 +++++--- .../android/HomeActivity.kt | 12 ++++++++++- .../android/LoginActivity.kt | 21 +++++++++++++++++++ .../apps/readerforselfossv2/android/MyApp.kt | 11 +++++----- .../android/settings/SettingsActivity.kt | 12 ++++++++++- .../src/main/res/values-ca-rES/strings.xml | 1 + .../src/main/res/values-de-rDE/strings.xml | 1 + .../src/main/res/values-es-rES/strings.xml | 1 + .../src/main/res/values-fa-rIR/strings.xml | 1 + .../src/main/res/values-fr-rFR/strings.xml | 1 + .../src/main/res/values-gl-rES/strings.xml | 1 + .../src/main/res/values-in-rID/strings.xml | 1 + .../src/main/res/values-it-rIT/strings.xml | 1 + .../src/main/res/values-ko-rKR/strings.xml | 1 + .../src/main/res/values-night/strings.xml | 4 ++++ .../src/main/res/values-nl-rNL/strings.xml | 1 + .../src/main/res/values-pt-rBR/strings.xml | 1 + .../src/main/res/values-pt-rPT/strings.xml | 1 + .../src/main/res/values-si-rLK/strings.xml | 1 + .../src/main/res/values-tr-rTR/strings.xml | 1 + .../src/main/res/values-zh-rCN/strings.xml | 1 + .../src/main/res/values-zh-rTW/strings.xml | 1 + androidApp/src/main/res/values/strings.xml | 1 + .../src/main/res/xml/pref_experimental.xml | 7 +++++++ .../service/AppSettingsService.kt | 15 +++++++++++++ 27 files changed, 112 insertions(+), 17 deletions(-) create mode 100644 androidApp/src/main/res/values-night/strings.xml diff --git a/.drone.yml b/.drone.yml index 9c41bda..1b750a2 100644 --- a/.drone.yml +++ b/.drone.yml @@ -8,7 +8,7 @@ steps: commands: - echo "---------------------------------------------------------" - echo "Configure gradle..." - - mkdir -p ~/.gradle && echo "org.gradle.daemon=false\nignoreGitVersion=true\nappLoginUrl=\"URL\"\nappLoginUsername=\"LOGIN\"\nappLoginPassword=\"PASS\"\npushCache=false\nsystemProp.org.gradle.internal.http.connectionTimeout=180000\nsystemProp.org.gradle.internal.http.socketTimeout=180000" >> ~/.gradle/gradle.properties + - mkdir -p ~/.gradle && echo "org.gradle.daemon=false\nignoreGitVersion=true\npushCache=false\nmatomoUrl=\"$MATOMO_URL\"\nmatomoSite=\"$MATOMO_SITE\"\nsystemProp.org.gradle.internal.http.connectionTimeout=180000\nsystemProp.org.gradle.internal.http.socketTimeout=180000" >> ~/.gradle/gradle.properties - echo "---------------------------------------------------------" - echo "Analysing..." - ./gradlew sonarqube -Dsonar.projectKey=RFS2 -Dsonar.host.url=$SONAR_HOST_URL -Dsonar.login=$SONAR_LOGIN @@ -24,6 +24,10 @@ steps: from_secret: sonarScannerHostUrl SONAR_LOGIN: from_secret: sonarScannerLogin + MATOMO_URL: + from_secret: matomoUrl + MATOMO_SITE: + from_secret: matomoSite trigger: event: - push @@ -90,7 +94,7 @@ steps: commands: - echo "---------------------------------------------------------" - echo "Configure gradle..." - - mkdir -p ~/.gradle && echo "org.gradle.daemon=false\nignoreGitVersion=true\nappLoginUrl=\"URL\"\nappLoginUsername=\"LOGIN\"\nappLoginPassword=\"PASS\"\npushCache=false\nsystemProp.org.gradle.internal.http.connectionTimeout=180000\nsystemProp.org.gradle.internal.http.socketTimeout=180000" >> ~/.gradle/gradle.properties + - mkdir -p ~/.gradle && echo "org.gradle.daemon=false\nignoreGitVersion=true\nmatomoUrl=\"$MATOMO_URL\"\nmatomoSite=\"$MATOMO_SITE\"\npushCache=false\nsystemProp.org.gradle.internal.http.connectionTimeout=180000\nsystemProp.org.gradle.internal.http.socketTimeout=180000" >> ~/.gradle/gradle.properties - echo "---------------------------------------------------------" - echo "Generate APK" - ./gradlew :androidApp:assembleGithubConfigRelease -P pushCache=false @@ -111,6 +115,10 @@ steps: from_secret: keyPass YOUR_KEY_ALIAS: from_secret: keyAlias + MATOMO_URL: + from_secret: matomoUrl + MATOMO_SITE: + from_secret: matomoSite - name: gitea_release image: plugins/gitea-release diff --git a/.github/CONTRIBUTING.md b/.github/CONTRIBUTING.md index 7158439..415e10f 100644 --- a/.github/CONTRIBUTING.md +++ b/.github/CONTRIBUTING.md @@ -55,19 +55,18 @@ You'll have to: - Define some parameters either in `~/.gradle/gradle.properties` or as gradle parameters (see the examples) - - appLoginUrl, appLoginUsername and appLoginPassword: url, username and password of a selfoss instance. **These are only used for tests. They can be empty if you don't test API calls.** + - matomoUrl and matomoSite: url and siteId for a matomo instance ### Examples: #### Inside ~/.gradle/gradle.properties ``` -appLoginUrl="URL" # It can be empty. -appLoginUsername="LOGIN" # It can be empty. -appLoginPassword="PASS" # It can be empty. +matomoUrl="URL" # It can be empty. +matomoSite="1" # It can be empty, but needs to be an integer ``` #### As gradle parameters ``` -./gradlew .... -P appLoginUrl="URL" -P appLoginUsername="LOGIN" -P appLoginPassword="PASS" +./gradlew .... -P matomoUrl="URL" -P matomoSite="1" ``` diff --git a/androidApp/build.gradle.kts b/androidApp/build.gradle.kts index 4e1c52d..68a45e8 100644 --- a/androidApp/build.gradle.kts +++ b/androidApp/build.gradle.kts @@ -83,6 +83,9 @@ android { // tests testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner" + + buildConfigField("String", "MATOMO_URL", properties["matomoUrl"] as String) + buildConfigField("String", "MATOMO_SITE", properties["matomoSite"] as String) } packagingOptions { resources { @@ -96,9 +99,6 @@ android { proguardFiles(getDefaultProguardFile("proguard-android.txt"), "proguard-rules.pro") } getByName("debug") { - buildConfigField("String", "LOGIN_URL", properties["appLoginUrl"] as String) - buildConfigField("String", "LOGIN_PASSWORD", properties["appLoginPassword"] as String) - buildConfigField("String", "LOGIN_USERNAME", properties["appLoginUsername"] as String) } } flavorDimensions.add("build") @@ -189,6 +189,9 @@ dependencies { testImplementation("io.mockk:mockk:1.12.0") testImplementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.6.0") implementation("org.jetbrains.kotlinx:kotlinx-datetime:0.4.0") + + // Matomo + implementation("com.github.matomo-org:matomo-sdk-android:4.1.4") } tasks.withType { diff --git a/androidApp/src/main/java/bou/amine/apps/readerforselfossv2/android/HomeActivity.kt b/androidApp/src/main/java/bou/amine/apps/readerforselfossv2/android/HomeActivity.kt index 3d4985b..d5c0824 100644 --- a/androidApp/src/main/java/bou/amine/apps/readerforselfossv2/android/HomeActivity.kt +++ b/androidApp/src/main/java/bou/amine/apps/readerforselfossv2/android/HomeActivity.kt @@ -35,7 +35,10 @@ import bou.amine.apps.readerforselfossv2.android.utils.bottombar.removeBadge import bou.amine.apps.readerforselfossv2.model.SelfossModel import bou.amine.apps.readerforselfossv2.repository.Repository import bou.amine.apps.readerforselfossv2.service.AppSettingsService -import bou.amine.apps.readerforselfossv2.utils.* +import bou.amine.apps.readerforselfossv2.utils.ItemType +import bou.amine.apps.readerforselfossv2.utils.getHtmlDecoded +import bou.amine.apps.readerforselfossv2.utils.getIcon +import bou.amine.apps.readerforselfossv2.utils.longHash import com.ashokvarma.bottomnavigation.BottomNavigationBar import com.ashokvarma.bottomnavigation.BottomNavigationItem import com.ashokvarma.bottomnavigation.TextBadgeItem @@ -59,6 +62,9 @@ import kotlinx.coroutines.launch import org.kodein.di.DIAware import org.kodein.di.android.closestDI import org.kodein.di.instance +import org.matomo.sdk.Tracker +import org.matomo.sdk.extra.DimensionQueue +import org.matomo.sdk.extra.TrackHelper import java.util.concurrent.TimeUnit @@ -95,12 +101,16 @@ class HomeActivity : AppCompatActivity(), SearchView.OnQueryTextListener, DIAwar override val di by closestDI() private val repository : Repository by instance() private val appSettingsService : AppSettingsService by instance() + private val tracker : Tracker by instance() + override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) binding = ActivityHomeBinding.inflate(layoutInflater) val view = binding.root + TrackHelper.track().screen("/home").with(tracker) + fromTabShortcut = intent.getIntExtra("shortcutTab", -1) != -1 repository.offlineOverride = intent.getBooleanExtra("startOffline", false) diff --git a/androidApp/src/main/java/bou/amine/apps/readerforselfossv2/android/LoginActivity.kt b/androidApp/src/main/java/bou/amine/apps/readerforselfossv2/android/LoginActivity.kt index 267043a..ac9ed95 100644 --- a/androidApp/src/main/java/bou/amine/apps/readerforselfossv2/android/LoginActivity.kt +++ b/androidApp/src/main/java/bou/amine/apps/readerforselfossv2/android/LoginActivity.kt @@ -25,6 +25,12 @@ import kotlinx.coroutines.launch import org.kodein.di.DIAware import org.kodein.di.android.closestDI import org.kodein.di.instance +import org.matomo.sdk.Tracker +import org.matomo.sdk.extra.DimensionQueue +import org.matomo.sdk.extra.DownloadTracker +import org.matomo.sdk.extra.TrackHelper +import java.security.MessageDigest + class LoginActivity : AppCompatActivity(), DIAware { @@ -36,10 +42,16 @@ class LoginActivity : AppCompatActivity(), DIAware { override val di by closestDI() private val repository : Repository by instance() private val appSettingsService : AppSettingsService by instance() + private val tracker : Tracker by instance() + override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) + TrackHelper.track().download().identifier(DownloadTracker.Extra.ApkChecksum(applicationContext)) + .with(tracker) + TrackHelper.track().screen("/login").with(tracker) + handleTheme() binding = ActivityLoginBinding.inflate(layoutInflater) @@ -102,6 +114,15 @@ class LoginActivity : AppCompatActivity(), DIAware { private fun goToMain() { CoroutineScope(Dispatchers.Main).launch { repository.updateApiVersion() + + val messageDigest: MessageDigest = MessageDigest.getInstance("SHA-256") + messageDigest.update(appSettingsService.getBaseUrl().toByteArray()) + tracker.userId = String(messageDigest.digest()) + + val mDimensionQueue = DimensionQueue(tracker) + mDimensionQueue.add(1, appSettingsService.getApiVersion().toString()) + + tracker.isOptOut = !appSettingsService.isAnalyticsEnabled() } val intent = Intent(this, HomeActivity::class.java) startActivity(intent) diff --git a/androidApp/src/main/java/bou/amine/apps/readerforselfossv2/android/MyApp.kt b/androidApp/src/main/java/bou/amine/apps/readerforselfossv2/android/MyApp.kt index 9795104..620cfea 100644 --- a/androidApp/src/main/java/bou/amine/apps/readerforselfossv2/android/MyApp.kt +++ b/androidApp/src/main/java/bou/amine/apps/readerforselfossv2/android/MyApp.kt @@ -3,21 +3,16 @@ package bou.amine.apps.readerforselfossv2.android import android.app.NotificationChannel import android.app.NotificationManager import android.content.Context -import android.content.res.Configuration import android.graphics.drawable.Drawable import android.net.Uri import android.os.Build import android.widget.ImageView import android.widget.Toast -import androidx.appcompat.app.AppCompatDelegate -import androidx.appcompat.app.AppCompatDelegate.* import androidx.lifecycle.DefaultLifecycleObserver import androidx.lifecycle.LifecycleOwner import androidx.lifecycle.ProcessLifecycleOwner import androidx.multidex.MultiDexApplication -import androidx.preference.PreferenceManager import bou.amine.apps.readerforselfossv2.DI.networkModule -import bou.amine.apps.readerforselfossv2.android.utils.network.isNetworkAccessible import bou.amine.apps.readerforselfossv2.android.viewmodel.AppViewModel import bou.amine.apps.readerforselfossv2.dao.DriverFactory import bou.amine.apps.readerforselfossv2.dao.ReaderForSelfossDB @@ -35,6 +30,9 @@ import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.flow.MutableStateFlow import kotlinx.coroutines.launch import org.kodein.di.* +import org.matomo.sdk.Matomo +import org.matomo.sdk.Tracker +import org.matomo.sdk.TrackerBuilder class MyApp : MultiDexApplication(), DIAware { @@ -45,12 +43,15 @@ class MyApp : MultiDexApplication(), DIAware { bind() with singleton { Repository(instance(), instance(), isConnectionAvailable, instance()) } bind() with singleton { ConnectivityStatus(applicationContext) } bind() with singleton { AppViewModel(repository = instance()) } + bind() with singleton { TrackerBuilder.createDefault(BuildConfig.MATOMO_URL, BuildConfig.MATOMO_SITE.toInt()).build( + Matomo.getInstance(applicationContext)) } } private val repository: Repository by instance() private val viewModel: AppViewModel by instance() private val connectivityStatus: ConnectivityStatus by instance() private val driverFactory: DriverFactory by instance() + private val tracker: Tracker by instance() // TODO: handle with the "previous" way private val isConnectionAvailable: MutableStateFlow = MutableStateFlow(true) diff --git a/androidApp/src/main/java/bou/amine/apps/readerforselfossv2/android/settings/SettingsActivity.kt b/androidApp/src/main/java/bou/amine/apps/readerforselfossv2/android/settings/SettingsActivity.kt index a99ce0e..8f3f14b 100644 --- a/androidApp/src/main/java/bou/amine/apps/readerforselfossv2/android/settings/SettingsActivity.kt +++ b/androidApp/src/main/java/bou/amine/apps/readerforselfossv2/android/settings/SettingsActivity.kt @@ -17,16 +17,26 @@ import androidx.preference.PreferenceFragmentCompat import bou.amine.apps.readerforselfossv2.android.R import bou.amine.apps.readerforselfossv2.android.databinding.ActivitySettingsBinding import bou.amine.apps.readerforselfossv2.service.AppSettingsService +import org.kodein.di.DIAware +import org.kodein.di.android.closestDI +import org.kodein.di.instance +import org.matomo.sdk.Tracker +import org.matomo.sdk.extra.TrackHelper private const val TITLE_TAG = "settingsActivityTitle" class SettingsActivity : AppCompatActivity(), - PreferenceFragmentCompat.OnPreferenceStartFragmentCallback { + PreferenceFragmentCompat.OnPreferenceStartFragmentCallback, DIAware { + override val di by closestDI() + + private val tracker : Tracker by instance() override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) val binding = ActivitySettingsBinding.inflate(layoutInflater) + TrackHelper.track().screen("/settings").with(tracker) + setContentView(binding.root) if (savedInstanceState == null) { supportFragmentManager diff --git a/androidApp/src/main/res/values-ca-rES/strings.xml b/androidApp/src/main/res/values-ca-rES/strings.xml index 2b3b632..350228f 100644 --- a/androidApp/src/main/res/values-ca-rES/strings.xml +++ b/androidApp/src/main/res/values-ca-rES/strings.xml @@ -133,4 +133,5 @@ Follow the system setting Light mode Error loading sources… + Enable analytics diff --git a/androidApp/src/main/res/values-de-rDE/strings.xml b/androidApp/src/main/res/values-de-rDE/strings.xml index 5dd30bf..b4c18d4 100644 --- a/androidApp/src/main/res/values-de-rDE/strings.xml +++ b/androidApp/src/main/res/values-de-rDE/strings.xml @@ -133,4 +133,5 @@ Follow the system setting Light mode Error loading sources… + Enable analytics diff --git a/androidApp/src/main/res/values-es-rES/strings.xml b/androidApp/src/main/res/values-es-rES/strings.xml index c7ebc34..cead2ba 100644 --- a/androidApp/src/main/res/values-es-rES/strings.xml +++ b/androidApp/src/main/res/values-es-rES/strings.xml @@ -133,4 +133,5 @@ Follow the system setting Light mode Error loading sources… + Enable analytics diff --git a/androidApp/src/main/res/values-fa-rIR/strings.xml b/androidApp/src/main/res/values-fa-rIR/strings.xml index 9b12fa3..1b7bd0b 100644 --- a/androidApp/src/main/res/values-fa-rIR/strings.xml +++ b/androidApp/src/main/res/values-fa-rIR/strings.xml @@ -133,4 +133,5 @@ Follow the system setting Light mode Error loading sources… + Enable analytics diff --git a/androidApp/src/main/res/values-fr-rFR/strings.xml b/androidApp/src/main/res/values-fr-rFR/strings.xml index 31f87cb..cabbdea 100644 --- a/androidApp/src/main/res/values-fr-rFR/strings.xml +++ b/androidApp/src/main/res/values-fr-rFR/strings.xml @@ -133,4 +133,5 @@ Utiliser les paramètres système Thème clair Error loading sources… + Enable analytics diff --git a/androidApp/src/main/res/values-gl-rES/strings.xml b/androidApp/src/main/res/values-gl-rES/strings.xml index 111efb8..f4de4ce 100644 --- a/androidApp/src/main/res/values-gl-rES/strings.xml +++ b/androidApp/src/main/res/values-gl-rES/strings.xml @@ -133,4 +133,5 @@ Follow the system setting Light mode Error loading sources… + Enable analytics diff --git a/androidApp/src/main/res/values-in-rID/strings.xml b/androidApp/src/main/res/values-in-rID/strings.xml index 9be67a8..2037014 100644 --- a/androidApp/src/main/res/values-in-rID/strings.xml +++ b/androidApp/src/main/res/values-in-rID/strings.xml @@ -133,4 +133,5 @@ Follow the system setting Light mode Error loading sources… + Enable analytics diff --git a/androidApp/src/main/res/values-it-rIT/strings.xml b/androidApp/src/main/res/values-it-rIT/strings.xml index 63b1f8b..7fcd6cf 100644 --- a/androidApp/src/main/res/values-it-rIT/strings.xml +++ b/androidApp/src/main/res/values-it-rIT/strings.xml @@ -133,4 +133,5 @@ Follow the system setting Light mode Error loading sources… + Enable analytics diff --git a/androidApp/src/main/res/values-ko-rKR/strings.xml b/androidApp/src/main/res/values-ko-rKR/strings.xml index 1ac49f8..d31e480 100644 --- a/androidApp/src/main/res/values-ko-rKR/strings.xml +++ b/androidApp/src/main/res/values-ko-rKR/strings.xml @@ -133,4 +133,5 @@ Follow the system setting Light mode Error loading sources… + Enable analytics diff --git a/androidApp/src/main/res/values-night/strings.xml b/androidApp/src/main/res/values-night/strings.xml new file mode 100644 index 0000000..0f00e58 --- /dev/null +++ b/androidApp/src/main/res/values-night/strings.xml @@ -0,0 +1,4 @@ + + + Enable analytics + \ No newline at end of file diff --git a/androidApp/src/main/res/values-nl-rNL/strings.xml b/androidApp/src/main/res/values-nl-rNL/strings.xml index c1fff12..350037a 100644 --- a/androidApp/src/main/res/values-nl-rNL/strings.xml +++ b/androidApp/src/main/res/values-nl-rNL/strings.xml @@ -133,4 +133,5 @@ Follow the system setting Light mode Error loading sources… + Enable analytics diff --git a/androidApp/src/main/res/values-pt-rBR/strings.xml b/androidApp/src/main/res/values-pt-rBR/strings.xml index b72186c..142f733 100644 --- a/androidApp/src/main/res/values-pt-rBR/strings.xml +++ b/androidApp/src/main/res/values-pt-rBR/strings.xml @@ -133,4 +133,5 @@ Follow the system setting Light mode Error loading sources… + Enable analytics diff --git a/androidApp/src/main/res/values-pt-rPT/strings.xml b/androidApp/src/main/res/values-pt-rPT/strings.xml index 098cffb..5554e77 100644 --- a/androidApp/src/main/res/values-pt-rPT/strings.xml +++ b/androidApp/src/main/res/values-pt-rPT/strings.xml @@ -133,4 +133,5 @@ Follow the system setting Light mode Error loading sources… + Enable analytics diff --git a/androidApp/src/main/res/values-si-rLK/strings.xml b/androidApp/src/main/res/values-si-rLK/strings.xml index 093df0d..699bed0 100644 --- a/androidApp/src/main/res/values-si-rLK/strings.xml +++ b/androidApp/src/main/res/values-si-rLK/strings.xml @@ -133,4 +133,5 @@ Follow the system setting Light mode Error loading sources… + Enable analytics diff --git a/androidApp/src/main/res/values-tr-rTR/strings.xml b/androidApp/src/main/res/values-tr-rTR/strings.xml index cab7d99..b7e5deb 100644 --- a/androidApp/src/main/res/values-tr-rTR/strings.xml +++ b/androidApp/src/main/res/values-tr-rTR/strings.xml @@ -133,4 +133,5 @@ Follow the system setting Light mode Error loading sources… + Enable analytics diff --git a/androidApp/src/main/res/values-zh-rCN/strings.xml b/androidApp/src/main/res/values-zh-rCN/strings.xml index 0f61ea8..62845fb 100644 --- a/androidApp/src/main/res/values-zh-rCN/strings.xml +++ b/androidApp/src/main/res/values-zh-rCN/strings.xml @@ -133,4 +133,5 @@ 遵循系统设置 浅色模式 Error loading sources… + Enable analytics diff --git a/androidApp/src/main/res/values-zh-rTW/strings.xml b/androidApp/src/main/res/values-zh-rTW/strings.xml index 95fbb4a..322e112 100644 --- a/androidApp/src/main/res/values-zh-rTW/strings.xml +++ b/androidApp/src/main/res/values-zh-rTW/strings.xml @@ -133,4 +133,5 @@ Follow the system setting Light mode Error loading sources… + Enable analytics diff --git a/androidApp/src/main/res/values/strings.xml b/androidApp/src/main/res/values/strings.xml index fd91486..e0f0311 100644 --- a/androidApp/src/main/res/values/strings.xml +++ b/androidApp/src/main/res/values/strings.xml @@ -136,4 +136,5 @@ Dark mode Follow the system setting Light mode + Enable analytics diff --git a/androidApp/src/main/res/xml/pref_experimental.xml b/androidApp/src/main/res/xml/pref_experimental.xml index bc7569e..990de19 100644 --- a/androidApp/src/main/res/xml/pref_experimental.xml +++ b/androidApp/src/main/res/xml/pref_experimental.xml @@ -1,5 +1,12 @@ + + +