From bda92a9ab451c313d9d19102c0f111be32ff1bfb Mon Sep 17 00:00:00 2001 From: NIkita Fedrunov Date: Fri, 18 Feb 2022 10:23:56 +0100 Subject: [PATCH 1/2] track screen event when user enters the screen instead of when user leaves the screen --- changelog.d/5256.misc | 1 + .../im/vector/app/core/platform/VectorBaseActivity.kt | 6 +++--- .../platform/VectorBaseBottomSheetDialogFragment.kt | 10 +++------- .../im/vector/app/core/platform/VectorBaseFragment.kt | 6 +++--- .../app/features/analytics/screen/ScreenEvent.kt | 6 +----- .../app/features/call/dialpad/DialPadFragment.kt | 7 +------ .../java/im/vector/app/features/home/HomeActivity.kt | 8 +------- .../features/home/room/detail/RoomDetailActivity.kt | 8 +------- .../features/settings/VectorSettingsBaseFragment.kt | 10 +++------- 9 files changed, 17 insertions(+), 45 deletions(-) create mode 100644 changelog.d/5256.misc diff --git a/changelog.d/5256.misc b/changelog.d/5256.misc new file mode 100644 index 0000000000..e20f52c7aa --- /dev/null +++ b/changelog.d/5256.misc @@ -0,0 +1 @@ +Analytics screen events are now tracked on screen enter instead of screen leave \ No newline at end of file diff --git a/vector/src/main/java/im/vector/app/core/platform/VectorBaseActivity.kt b/vector/src/main/java/im/vector/app/core/platform/VectorBaseActivity.kt index 5767acd44b..6c49a1ff01 100644 --- a/vector/src/main/java/im/vector/app/core/platform/VectorBaseActivity.kt +++ b/vector/src/main/java/im/vector/app/core/platform/VectorBaseActivity.kt @@ -98,7 +98,6 @@ abstract class VectorBaseActivity : AppCompatActivity(), Maver * ========================================================================================== */ protected var analyticsScreenName: MobileScreen.ScreenName? = null - private var screenEvent: ScreenEvent? = null protected lateinit var analyticsTracker: AnalyticsTracker @@ -337,7 +336,9 @@ abstract class VectorBaseActivity : AppCompatActivity(), Maver override fun onResume() { super.onResume() Timber.i("onResume Activity ${javaClass.simpleName}") - screenEvent = analyticsScreenName?.let { ScreenEvent(it) } + analyticsScreenName?.let { + ScreenEvent(it).send(analyticsTracker) + } configurationViewModel.onActivityResumed() if (this !is BugReportActivity && vectorPreferences.useRageshake()) { @@ -376,7 +377,6 @@ abstract class VectorBaseActivity : AppCompatActivity(), Maver override fun onPause() { super.onPause() - screenEvent?.send(analyticsTracker, analyticsScreenName) Timber.i("onPause Activity ${javaClass.simpleName}") rageShake.stop() diff --git a/vector/src/main/java/im/vector/app/core/platform/VectorBaseBottomSheetDialogFragment.kt b/vector/src/main/java/im/vector/app/core/platform/VectorBaseBottomSheetDialogFragment.kt index 869a12e871..11bfcdd9ef 100644 --- a/vector/src/main/java/im/vector/app/core/platform/VectorBaseBottomSheetDialogFragment.kt +++ b/vector/src/main/java/im/vector/app/core/platform/VectorBaseBottomSheetDialogFragment.kt @@ -54,7 +54,6 @@ abstract class VectorBaseBottomSheetDialogFragment : BottomShe * ========================================================================================== */ protected var analyticsScreenName: MobileScreen.ScreenName? = null - private var screenEvent: ScreenEvent? = null protected lateinit var analyticsTracker: AnalyticsTracker @@ -139,12 +138,9 @@ abstract class VectorBaseBottomSheetDialogFragment : BottomShe override fun onResume() { super.onResume() Timber.i("onResume BottomSheet ${javaClass.simpleName}") - screenEvent = analyticsScreenName?.let { ScreenEvent(it) } - } - - override fun onPause() { - super.onPause() - screenEvent?.send(analyticsTracker) + analyticsScreenName?.let { + ScreenEvent(it).send(analyticsTracker) + } } override fun onCreateDialog(savedInstanceState: Bundle?): Dialog { diff --git a/vector/src/main/java/im/vector/app/core/platform/VectorBaseFragment.kt b/vector/src/main/java/im/vector/app/core/platform/VectorBaseFragment.kt index 6bd62707f2..7b8eda76ff 100644 --- a/vector/src/main/java/im/vector/app/core/platform/VectorBaseFragment.kt +++ b/vector/src/main/java/im/vector/app/core/platform/VectorBaseFragment.kt @@ -59,7 +59,6 @@ abstract class VectorBaseFragment : Fragment(), MavericksView * ========================================================================================== */ protected var analyticsScreenName: MobileScreen.ScreenName? = null - private var screenEvent: ScreenEvent? = null protected lateinit var analyticsTracker: AnalyticsTracker @@ -145,14 +144,15 @@ abstract class VectorBaseFragment : Fragment(), MavericksView override fun onResume() { super.onResume() Timber.i("onResume Fragment ${javaClass.simpleName}") - screenEvent = analyticsScreenName?.let { ScreenEvent(it) } + analyticsScreenName?.let { + ScreenEvent(it).send(analyticsTracker) + } } @CallSuper override fun onPause() { super.onPause() Timber.i("onPause Fragment ${javaClass.simpleName}") - screenEvent?.send(analyticsTracker) } @CallSuper diff --git a/vector/src/main/java/im/vector/app/features/analytics/screen/ScreenEvent.kt b/vector/src/main/java/im/vector/app/features/analytics/screen/ScreenEvent.kt index 1ad4a1fa32..400bede415 100644 --- a/vector/src/main/java/im/vector/app/features/analytics/screen/ScreenEvent.kt +++ b/vector/src/main/java/im/vector/app/features/analytics/screen/ScreenEvent.kt @@ -16,7 +16,6 @@ package im.vector.app.features.analytics.screen -import android.os.SystemClock import im.vector.app.features.analytics.AnalyticsTracker import im.vector.app.features.analytics.plan.MobileScreen import timber.log.Timber @@ -25,8 +24,6 @@ import timber.log.Timber * Track a screen display. Unique usage. */ class ScreenEvent(val screenName: MobileScreen.ScreenName) { - private val startTime = SystemClock.elapsedRealtime() - // Protection to avoid multiple sending private var isSent = false @@ -42,8 +39,7 @@ class ScreenEvent(val screenName: MobileScreen.ScreenName) { isSent = true analyticsTracker.screen( MobileScreen( - screenName = screenNameOverride ?: screenName, - durationMs = (SystemClock.elapsedRealtime() - startTime).toInt() + screenName = screenNameOverride ?: screenName ) ) } diff --git a/vector/src/main/java/im/vector/app/features/call/dialpad/DialPadFragment.kt b/vector/src/main/java/im/vector/app/features/call/dialpad/DialPadFragment.kt index b33ce25f55..9d77680ff5 100644 --- a/vector/src/main/java/im/vector/app/features/call/dialpad/DialPadFragment.kt +++ b/vector/src/main/java/im/vector/app/features/call/dialpad/DialPadFragment.kt @@ -69,12 +69,7 @@ class DialPadFragment : Fragment(), TextWatcher { private var screenEvent: ScreenEvent? = null override fun onResume() { super.onResume() - screenEvent = ScreenEvent(MobileScreen.ScreenName.Dialpad) - } - - override fun onPause() { - super.onPause() - screenEvent?.send(analyticsTracker) + ScreenEvent(MobileScreen.ScreenName.Dialpad).send(analyticsTracker) } override fun onCreateView( diff --git a/vector/src/main/java/im/vector/app/features/home/HomeActivity.kt b/vector/src/main/java/im/vector/app/features/home/HomeActivity.kt index f6b32973a0..b7bfdef21c 100644 --- a/vector/src/main/java/im/vector/app/features/home/HomeActivity.kt +++ b/vector/src/main/java/im/vector/app/features/home/HomeActivity.kt @@ -163,14 +163,8 @@ class HomeActivity : } private val drawerListener = object : DrawerLayout.SimpleDrawerListener() { - private var drawerScreenEvent: ScreenEvent? = null override fun onDrawerOpened(drawerView: View) { - drawerScreenEvent = ScreenEvent(MobileScreen.ScreenName.Sidebar) - } - - override fun onDrawerClosed(drawerView: View) { - drawerScreenEvent?.send(analyticsTracker) - drawerScreenEvent = null + ScreenEvent(MobileScreen.ScreenName.Sidebar).send(analyticsTracker) } override fun onDrawerStateChanged(newState: Int) { diff --git a/vector/src/main/java/im/vector/app/features/home/room/detail/RoomDetailActivity.kt b/vector/src/main/java/im/vector/app/features/home/room/detail/RoomDetailActivity.kt index f5bf086e96..f40bee44db 100644 --- a/vector/src/main/java/im/vector/app/features/home/room/detail/RoomDetailActivity.kt +++ b/vector/src/main/java/im/vector/app/features/home/room/detail/RoomDetailActivity.kt @@ -158,14 +158,8 @@ class RoomDetailActivity : } private val drawerListener = object : DrawerLayout.SimpleDrawerListener() { - private var drawerScreenEvent: ScreenEvent? = null override fun onDrawerOpened(drawerView: View) { - drawerScreenEvent = ScreenEvent(MobileScreen.ScreenName.Breadcrumbs) - } - - override fun onDrawerClosed(drawerView: View) { - drawerScreenEvent?.send(analyticsTracker) - drawerScreenEvent = null + ScreenEvent(MobileScreen.ScreenName.Breadcrumbs).send(analyticsTracker) } override fun onDrawerStateChanged(newState: Int) { diff --git a/vector/src/main/java/im/vector/app/features/settings/VectorSettingsBaseFragment.kt b/vector/src/main/java/im/vector/app/features/settings/VectorSettingsBaseFragment.kt index 4185fde663..c907168954 100644 --- a/vector/src/main/java/im/vector/app/features/settings/VectorSettingsBaseFragment.kt +++ b/vector/src/main/java/im/vector/app/features/settings/VectorSettingsBaseFragment.kt @@ -44,7 +44,6 @@ abstract class VectorSettingsBaseFragment : PreferenceFragmentCompat(), Maverick * ========================================================================================== */ protected var analyticsScreenName: MobileScreen.ScreenName? = null - private var screenEvent: ScreenEvent? = null protected lateinit var analyticsTracker: AnalyticsTracker @@ -91,17 +90,14 @@ abstract class VectorSettingsBaseFragment : PreferenceFragmentCompat(), Maverick override fun onResume() { super.onResume() Timber.i("onResume Fragment ${javaClass.simpleName}") - screenEvent = analyticsScreenName?.let { ScreenEvent(it) } + analyticsScreenName?.let { + ScreenEvent(it).send(analyticsTracker) + } vectorActivity.supportActionBar?.setTitle(titleRes) // find the view from parent activity mLoadingView = vectorActivity.findViewById(R.id.vector_settings_spinner_views) } - override fun onPause() { - super.onPause() - screenEvent?.send(analyticsTracker) - } - abstract fun bindPref() abstract var titleRes: Int From 89e97748bc617a8cae0e9e48fa987b4393397112 Mon Sep 17 00:00:00 2001 From: NIkita Fedrunov Date: Tue, 22 Feb 2022 10:24:27 +0100 Subject: [PATCH 2/2] ScreenEvent removed --- .../app/core/platform/VectorBaseActivity.kt | 3 +- .../VectorBaseBottomSheetDialogFragment.kt | 3 +- .../app/core/platform/VectorBaseFragment.kt | 3 +- .../features/analytics/screen/ScreenEvent.kt | 46 ------------------- .../features/call/dialpad/DialPadFragment.kt | 4 +- .../vector/app/features/home/HomeActivity.kt | 3 +- .../home/room/detail/RoomDetailActivity.kt | 3 +- .../settings/VectorSettingsBaseFragment.kt | 3 +- 8 files changed, 7 insertions(+), 61 deletions(-) delete mode 100644 vector/src/main/java/im/vector/app/features/analytics/screen/ScreenEvent.kt diff --git a/vector/src/main/java/im/vector/app/core/platform/VectorBaseActivity.kt b/vector/src/main/java/im/vector/app/core/platform/VectorBaseActivity.kt index 6c49a1ff01..2c161feb37 100644 --- a/vector/src/main/java/im/vector/app/core/platform/VectorBaseActivity.kt +++ b/vector/src/main/java/im/vector/app/core/platform/VectorBaseActivity.kt @@ -68,7 +68,6 @@ import im.vector.app.features.MainActivity import im.vector.app.features.MainActivityArgs import im.vector.app.features.analytics.AnalyticsTracker import im.vector.app.features.analytics.plan.MobileScreen -import im.vector.app.features.analytics.screen.ScreenEvent import im.vector.app.features.configuration.VectorConfiguration import im.vector.app.features.consent.ConsentNotGivenHelper import im.vector.app.features.navigation.Navigator @@ -337,7 +336,7 @@ abstract class VectorBaseActivity : AppCompatActivity(), Maver super.onResume() Timber.i("onResume Activity ${javaClass.simpleName}") analyticsScreenName?.let { - ScreenEvent(it).send(analyticsTracker) + analyticsTracker.screen(MobileScreen(screenName = it)) } configurationViewModel.onActivityResumed() diff --git a/vector/src/main/java/im/vector/app/core/platform/VectorBaseBottomSheetDialogFragment.kt b/vector/src/main/java/im/vector/app/core/platform/VectorBaseBottomSheetDialogFragment.kt index 11bfcdd9ef..ddc281fdd1 100644 --- a/vector/src/main/java/im/vector/app/core/platform/VectorBaseBottomSheetDialogFragment.kt +++ b/vector/src/main/java/im/vector/app/core/platform/VectorBaseBottomSheetDialogFragment.kt @@ -39,7 +39,6 @@ import im.vector.app.core.extensions.toMvRxBundle import im.vector.app.core.utils.DimensionConverter import im.vector.app.features.analytics.AnalyticsTracker import im.vector.app.features.analytics.plan.MobileScreen -import im.vector.app.features.analytics.screen.ScreenEvent import kotlinx.coroutines.flow.launchIn import kotlinx.coroutines.flow.onEach import reactivecircus.flowbinding.android.view.clicks @@ -139,7 +138,7 @@ abstract class VectorBaseBottomSheetDialogFragment : BottomShe super.onResume() Timber.i("onResume BottomSheet ${javaClass.simpleName}") analyticsScreenName?.let { - ScreenEvent(it).send(analyticsTracker) + analyticsTracker.screen(MobileScreen(screenName = it)) } } diff --git a/vector/src/main/java/im/vector/app/core/platform/VectorBaseFragment.kt b/vector/src/main/java/im/vector/app/core/platform/VectorBaseFragment.kt index 7b8eda76ff..70b265ff9f 100644 --- a/vector/src/main/java/im/vector/app/core/platform/VectorBaseFragment.kt +++ b/vector/src/main/java/im/vector/app/core/platform/VectorBaseFragment.kt @@ -45,7 +45,6 @@ import im.vector.app.core.extensions.toMvRxBundle import im.vector.app.core.utils.ToolbarConfig import im.vector.app.features.analytics.AnalyticsTracker import im.vector.app.features.analytics.plan.MobileScreen -import im.vector.app.features.analytics.screen.ScreenEvent import im.vector.app.features.navigation.Navigator import im.vector.lib.ui.styles.dialogs.MaterialProgressDialog import kotlinx.coroutines.flow.launchIn @@ -145,7 +144,7 @@ abstract class VectorBaseFragment : Fragment(), MavericksView super.onResume() Timber.i("onResume Fragment ${javaClass.simpleName}") analyticsScreenName?.let { - ScreenEvent(it).send(analyticsTracker) + analyticsTracker.screen(MobileScreen(screenName = it)) } } diff --git a/vector/src/main/java/im/vector/app/features/analytics/screen/ScreenEvent.kt b/vector/src/main/java/im/vector/app/features/analytics/screen/ScreenEvent.kt deleted file mode 100644 index 400bede415..0000000000 --- a/vector/src/main/java/im/vector/app/features/analytics/screen/ScreenEvent.kt +++ /dev/null @@ -1,46 +0,0 @@ -/* - * Copyright (c) 2021 New Vector Ltd - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package im.vector.app.features.analytics.screen - -import im.vector.app.features.analytics.AnalyticsTracker -import im.vector.app.features.analytics.plan.MobileScreen -import timber.log.Timber - -/** - * Track a screen display. Unique usage. - */ -class ScreenEvent(val screenName: MobileScreen.ScreenName) { - // Protection to avoid multiple sending - private var isSent = false - - /** - * @param screenNameOverride can be used to override the screen name passed in constructor parameter - */ - fun send(analyticsTracker: AnalyticsTracker, - screenNameOverride: MobileScreen.ScreenName? = null) { - if (isSent) { - Timber.w("Event $screenName Already sent!") - return - } - isSent = true - analyticsTracker.screen( - MobileScreen( - screenName = screenNameOverride ?: screenName - ) - ) - } -} diff --git a/vector/src/main/java/im/vector/app/features/call/dialpad/DialPadFragment.kt b/vector/src/main/java/im/vector/app/features/call/dialpad/DialPadFragment.kt index 9d77680ff5..606ba1d1e9 100644 --- a/vector/src/main/java/im/vector/app/features/call/dialpad/DialPadFragment.kt +++ b/vector/src/main/java/im/vector/app/features/call/dialpad/DialPadFragment.kt @@ -41,7 +41,6 @@ import im.vector.app.R import im.vector.app.core.extensions.singletonEntryPoint import im.vector.app.features.analytics.AnalyticsTracker import im.vector.app.features.analytics.plan.MobileScreen -import im.vector.app.features.analytics.screen.ScreenEvent import im.vector.app.features.themes.ThemeUtils class DialPadFragment : Fragment(), TextWatcher { @@ -66,10 +65,9 @@ class DialPadFragment : Fragment(), TextWatcher { analyticsTracker = singletonEntryPoint.analyticsTracker() } - private var screenEvent: ScreenEvent? = null override fun onResume() { super.onResume() - ScreenEvent(MobileScreen.ScreenName.Dialpad).send(analyticsTracker) + analyticsTracker.screen(MobileScreen(screenName = MobileScreen.ScreenName.Dialpad)) } override fun onCreateView( diff --git a/vector/src/main/java/im/vector/app/features/home/HomeActivity.kt b/vector/src/main/java/im/vector/app/features/home/HomeActivity.kt index b7bfdef21c..47f1a9208b 100644 --- a/vector/src/main/java/im/vector/app/features/home/HomeActivity.kt +++ b/vector/src/main/java/im/vector/app/features/home/HomeActivity.kt @@ -49,7 +49,6 @@ import im.vector.app.features.MainActivity import im.vector.app.features.MainActivityArgs import im.vector.app.features.analytics.accountdata.AnalyticsAccountDataViewModel import im.vector.app.features.analytics.plan.MobileScreen -import im.vector.app.features.analytics.screen.ScreenEvent import im.vector.app.features.disclaimer.showDisclaimerDialog import im.vector.app.features.matrixto.MatrixToBottomSheet import im.vector.app.features.navigation.Navigator @@ -164,7 +163,7 @@ class HomeActivity : private val drawerListener = object : DrawerLayout.SimpleDrawerListener() { override fun onDrawerOpened(drawerView: View) { - ScreenEvent(MobileScreen.ScreenName.Sidebar).send(analyticsTracker) + analyticsTracker.screen(MobileScreen(screenName = MobileScreen.ScreenName.Sidebar)) } override fun onDrawerStateChanged(newState: Int) { diff --git a/vector/src/main/java/im/vector/app/features/home/room/detail/RoomDetailActivity.kt b/vector/src/main/java/im/vector/app/features/home/room/detail/RoomDetailActivity.kt index f40bee44db..aa4ee825dc 100644 --- a/vector/src/main/java/im/vector/app/features/home/room/detail/RoomDetailActivity.kt +++ b/vector/src/main/java/im/vector/app/features/home/room/detail/RoomDetailActivity.kt @@ -36,7 +36,6 @@ import im.vector.app.core.extensions.replaceFragment import im.vector.app.core.platform.VectorBaseActivity import im.vector.app.databinding.ActivityRoomDetailBinding import im.vector.app.features.analytics.plan.MobileScreen -import im.vector.app.features.analytics.screen.ScreenEvent import im.vector.app.features.home.room.breadcrumbs.BreadcrumbsFragment import im.vector.app.features.home.room.detail.arguments.TimelineArgs import im.vector.app.features.home.room.detail.timeline.helper.VoiceMessagePlaybackTracker @@ -159,7 +158,7 @@ class RoomDetailActivity : private val drawerListener = object : DrawerLayout.SimpleDrawerListener() { override fun onDrawerOpened(drawerView: View) { - ScreenEvent(MobileScreen.ScreenName.Breadcrumbs).send(analyticsTracker) + analyticsTracker.screen(MobileScreen(screenName = MobileScreen.ScreenName.Breadcrumbs)) } override fun onDrawerStateChanged(newState: Int) { diff --git a/vector/src/main/java/im/vector/app/features/settings/VectorSettingsBaseFragment.kt b/vector/src/main/java/im/vector/app/features/settings/VectorSettingsBaseFragment.kt index c907168954..dae234eecc 100644 --- a/vector/src/main/java/im/vector/app/features/settings/VectorSettingsBaseFragment.kt +++ b/vector/src/main/java/im/vector/app/features/settings/VectorSettingsBaseFragment.kt @@ -31,7 +31,6 @@ import im.vector.app.core.platform.VectorBaseActivity import im.vector.app.core.utils.toast import im.vector.app.features.analytics.AnalyticsTracker import im.vector.app.features.analytics.plan.MobileScreen -import im.vector.app.features.analytics.screen.ScreenEvent import kotlinx.coroutines.flow.launchIn import kotlinx.coroutines.flow.onEach import org.matrix.android.sdk.api.session.Session @@ -91,7 +90,7 @@ abstract class VectorSettingsBaseFragment : PreferenceFragmentCompat(), Maverick super.onResume() Timber.i("onResume Fragment ${javaClass.simpleName}") analyticsScreenName?.let { - ScreenEvent(it).send(analyticsTracker) + analyticsTracker.screen(MobileScreen(screenName = it)) } vectorActivity.supportActionBar?.setTitle(titleRes) // find the view from parent activity