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 = "