From 3108114b605666307a45103a9bf4d7081b59d119 Mon Sep 17 00:00:00 2001 From: William Brawner Date: Sun, 23 Oct 2022 23:44:02 -0600 Subject: [PATCH] Improve analytics - Automatic page view tracking wasn't helpful since it was based on the Activities viewed, and SimpleMarkdown is a single-Activity app, so page views are now tracked manually - User preferences are now reported so I can remove preferences that aren't used and focus my time on features that are actively used - Opting out of crash reports is no longer possible. I need crash reports to be able to improve the app. It would also simplify the code a bit to not need to take that into account. Existing opt-outs will be respected but moving forward, new users will need to download the app from Fdroid if they'd like to avoid interactions with Google. --- .../simplemarkdown/utility/AnalyticsHelper.kt | 19 +++++++++++++++ .../simplemarkdown/utility/AnalyticsHelper.kt | 8 +++++++ .../view/activity/MainActivity.kt | 24 +++++++++++++++++++ .../view/fragment/MainFragment.kt | 3 +++ .../view/fragment/MarkdownInfoFragment.kt | 13 ++++++---- .../fragment/SettingsContainerFragment.kt | 7 ++++++ .../view/fragment/SupportFragment.kt | 9 ++++++- app/src/main/res/xml/pref_general.xml | 6 ----- app/src/play/AndroidManifest.xml | 3 +++ .../utility/FirebaseAnalyticsHelper.kt | 19 +++++++++++++++ 10 files changed, 100 insertions(+), 11 deletions(-) create mode 100644 app/src/free/java/com/wbrawner/simplemarkdown/utility/AnalyticsHelper.kt create mode 100644 app/src/main/java/com/wbrawner/simplemarkdown/utility/AnalyticsHelper.kt create mode 100644 app/src/play/java/com/wbrawner/simplemarkdown/utility/FirebaseAnalyticsHelper.kt diff --git a/app/src/free/java/com/wbrawner/simplemarkdown/utility/AnalyticsHelper.kt b/app/src/free/java/com/wbrawner/simplemarkdown/utility/AnalyticsHelper.kt new file mode 100644 index 0000000..d332bcc --- /dev/null +++ b/app/src/free/java/com/wbrawner/simplemarkdown/utility/AnalyticsHelper.kt @@ -0,0 +1,19 @@ +package com.wbrawner.simplemarkdown.utility + +import android.content.Context +import com.wbrawner.simplemarkdown.BuildConfig +import timber.log.Timber + +class NoopAnalyticsHelper(context: Context): AnalyticsHelper() { + override fun setUserProperty(name: String, value: String) { + if (BuildConfig.DEBUG) { + Timber.tag("NoopAnalyticsHelper").d("setting user property $name to $value") + } + } + + override fun trackPageView(name: String) { + Timber.tag("NoopAnalyticsHelper").d("user viewed $name page") + } +} + +fun AnalyticsHelper.Companion.init(context: Context) = NoopAnalyticsHelper(context) diff --git a/app/src/main/java/com/wbrawner/simplemarkdown/utility/AnalyticsHelper.kt b/app/src/main/java/com/wbrawner/simplemarkdown/utility/AnalyticsHelper.kt new file mode 100644 index 0000000..cba8d70 --- /dev/null +++ b/app/src/main/java/com/wbrawner/simplemarkdown/utility/AnalyticsHelper.kt @@ -0,0 +1,8 @@ +package com.wbrawner.simplemarkdown.utility + +abstract class AnalyticsHelper { + abstract fun setUserProperty(name: String, value: String) + abstract fun trackPageView(name: String) + + companion object +} diff --git a/app/src/main/java/com/wbrawner/simplemarkdown/view/activity/MainActivity.kt b/app/src/main/java/com/wbrawner/simplemarkdown/view/activity/MainActivity.kt index c9950f5..01a3a74 100644 --- a/app/src/main/java/com/wbrawner/simplemarkdown/view/activity/MainActivity.kt +++ b/app/src/main/java/com/wbrawner/simplemarkdown/view/activity/MainActivity.kt @@ -1,16 +1,30 @@ package com.wbrawner.simplemarkdown.view.activity +import android.content.Context import android.os.Bundle +import androidx.annotation.StringRes import androidx.appcompat.app.AppCompatActivity import androidx.core.app.ActivityCompat import androidx.navigation.findNavController +import androidx.preference.PreferenceManager import com.wbrawner.simplemarkdown.R +import com.wbrawner.simplemarkdown.utility.AnalyticsHelper +import com.wbrawner.simplemarkdown.utility.init class MainActivity : AppCompatActivity(), ActivityCompat.OnRequestPermissionsResultCallback { + private val analyticsHelper = AnalyticsHelper.init(this) override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) + val sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this) + analyticsHelper.setUserProperty("autosave", sharedPreferences.getBoolean("autosave", true).toString()) + val usingCustomCss = !getStringPref(R.string.pref_custom_css, null).isNullOrBlank() + analyticsHelper.setUserProperty("using_custom_css", usingCustomCss.toString()) + val darkModeSetting = getStringPref(R.string.pref_key_dark_mode, "auto").toString() + analyticsHelper.setUserProperty("dark_mode", darkModeSetting) + analyticsHelper.setUserProperty("error_reports_enabled", getBooleanPref(R.string.pref_key_error_reports_enabled, true).toString()) + analyticsHelper.setUserProperty("readability_enabled", getBooleanPref(R.string.readability_enabled, false).toString()) } override fun onBackPressed() { @@ -19,3 +33,13 @@ class MainActivity : AppCompatActivity(), ActivityCompat.OnRequestPermissionsRes } } } + +fun Context.getBooleanPref(@StringRes key: Int, defaultValue: Boolean) = PreferenceManager.getDefaultSharedPreferences(this).getBoolean( + getString(key), + defaultValue +) + +fun Context.getStringPref(@StringRes key: Int, defaultValue: String?) = PreferenceManager.getDefaultSharedPreferences(this).getString( + getString(key), + defaultValue +) \ No newline at end of file diff --git a/app/src/main/java/com/wbrawner/simplemarkdown/view/fragment/MainFragment.kt b/app/src/main/java/com/wbrawner/simplemarkdown/view/fragment/MainFragment.kt index 4f6111c..2185fb4 100644 --- a/app/src/main/java/com/wbrawner/simplemarkdown/view/fragment/MainFragment.kt +++ b/app/src/main/java/com/wbrawner/simplemarkdown/view/fragment/MainFragment.kt @@ -23,8 +23,10 @@ import androidx.navigation.ui.AppBarConfiguration import androidx.navigation.ui.setupWithNavController import androidx.preference.PreferenceManager import com.wbrawner.simplemarkdown.R +import com.wbrawner.simplemarkdown.utility.AnalyticsHelper import com.wbrawner.simplemarkdown.utility.ErrorHandler import com.wbrawner.simplemarkdown.utility.errorHandlerImpl +import com.wbrawner.simplemarkdown.utility.init import com.wbrawner.simplemarkdown.view.adapter.EditPagerAdapter import com.wbrawner.simplemarkdown.viewmodel.MarkdownViewModel import kotlinx.android.synthetic.main.fragment_main.* @@ -144,6 +146,7 @@ class MainFragment : Fragment(), ActivityCompat.OnRequestPermissionsResultCallba override fun onStart() { super.onStart() + AnalyticsHelper.init(requireContext()).trackPageView("Edit") lifecycleScope.launch { withContext(Dispatchers.IO) { val enableErrorReports = PreferenceManager.getDefaultSharedPreferences(requireContext()) diff --git a/app/src/main/java/com/wbrawner/simplemarkdown/view/fragment/MarkdownInfoFragment.kt b/app/src/main/java/com/wbrawner/simplemarkdown/view/fragment/MarkdownInfoFragment.kt index d3a439b..5355aae 100644 --- a/app/src/main/java/com/wbrawner/simplemarkdown/view/fragment/MarkdownInfoFragment.kt +++ b/app/src/main/java/com/wbrawner/simplemarkdown/view/fragment/MarkdownInfoFragment.kt @@ -13,15 +13,13 @@ import androidx.lifecycle.lifecycleScope import androidx.navigation.fragment.findNavController import androidx.navigation.ui.setupWithNavController import com.wbrawner.simplemarkdown.R -import com.wbrawner.simplemarkdown.utility.ErrorHandler -import com.wbrawner.simplemarkdown.utility.errorHandlerImpl -import com.wbrawner.simplemarkdown.utility.readAssetToString -import com.wbrawner.simplemarkdown.utility.toHtml +import com.wbrawner.simplemarkdown.utility.* import kotlinx.android.synthetic.main.fragment_markdown_info.* import kotlinx.coroutines.launch class MarkdownInfoFragment : Fragment() { private val errorHandler: ErrorHandler by errorHandlerImpl() + private lateinit var analyticsHelper: AnalyticsHelper override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) @@ -74,6 +72,13 @@ class MarkdownInfoFragment : Fragment() { return super.onOptionsItemSelected(item) } + override fun onStart() { + super.onStart() + arguments?.getString(EXTRA_FILE)?.let { + AnalyticsHelper.init(requireContext()).trackPageView(it) + } + } + companion object { const val FORMAT_CSS = "