From dea9b839031fdf9c5c0a47f6fb3179859fb79539 Mon Sep 17 00:00:00 2001 From: Aman S Date: Thu, 6 Jul 2023 17:07:39 +0530 Subject: [PATCH] TP-35117 | Alfred/session id screen name fix (#7166) * TP-32650 | screenName issue fix * TP-000000 | session id validation in events api * TP-00000 | screen name fix for crash and anr events * TP-00000 | added session id empty check * TP-00000 | resolved pr comment * TP-35117 | events api failure handling --- .../java/com/naviapp/app/NaviApplication.kt | 68 +++++++++---------- .../navi/analytics/alfred/AlfredManager.kt | 39 ++++++----- .../analytics/alfred/utils/AlfredConstants.kt | 1 + 3 files changed, 54 insertions(+), 54 deletions(-) diff --git a/app/src/main/java/com/naviapp/app/NaviApplication.kt b/app/src/main/java/com/naviapp/app/NaviApplication.kt index 26dece097f..0caf2d77a5 100644 --- a/app/src/main/java/com/naviapp/app/NaviApplication.kt +++ b/app/src/main/java/com/naviapp/app/NaviApplication.kt @@ -44,8 +44,6 @@ import com.naviapp.analytics.utils.NaviAnalytics.Companion.STACK_TRACE import com.naviapp.analytics.utils.NaviSDKHelper import com.naviapp.common.transformer.AppLoadTimerMapper import com.naviapp.registration.SplashActivity -import com.naviapp.utils.Constants.ANR_REGEX_1 -import com.naviapp.utils.Constants.ANR_REGEX_2 import com.naviapp.utils.DEV import com.naviapp.utils.isDifferentPackage import dagger.hilt.android.HiltAndroidApp @@ -96,26 +94,23 @@ open class NaviApplication : SplitCompatApplication(), Application.ActivityLifec return@setANRListener } val className = it.cause?.stackTrace?.get(0)?.className.orEmpty() - if (className.contains(ANR_REGEX_1) || className.contains(ANR_REGEX_2)) { - val anrEventProperties = mutableMapOf( - SCREEN_NAME to className, - METHOD_NAME to it.cause?.stackTrace?.get(0)?.methodName.orEmpty(), - LINE_NUMBER to it.cause?.stackTrace?.get(0)?.lineNumber.toString(), - APP_IN_FOREGROUND to isAppInForeground().toString() + val anrEventProperties = mutableMapOf( + SCREEN_NAME to (NaviTrackEvent.currentScreenName ?: className), + METHOD_NAME to it.cause?.stackTrace?.get(0)?.methodName.orEmpty(), + LINE_NUMBER to it.cause?.stackTrace?.get(0)?.lineNumber.toString(), + APP_IN_FOREGROUND to isAppInForeground().toString() + ) + NaviTrackEvent.trackEventOnClickStream( + GLOBAL_ANR, anrEventProperties + ) + if (isDifferentPackage.not() && (AlfredManager.config.getAlfredStatus() && AlfredManager.config.getAnrEnableStatus())) { + anrEventProperties[STACK_TRACE] = it.cause?.stackTrace?.get(0).toString() + val anrView = + LayoutInflater.from(applicationContext).inflate(R.layout.anr_screen, null) + measureInflatedView(anrView) + AlfredManager.handleAnrEvent( + anrEventProperties, anrView, NaviTrackEvent.currentScreenName ?: className ) - NaviTrackEvent.trackEventOnClickStream( - GLOBAL_ANR, anrEventProperties - ) - if (isDifferentPackage.not()) { - if (AlfredManager.config.getAlfredStatus() && AlfredManager.config.getAnrEnableStatus()) { - anrEventProperties[STACK_TRACE] = it.cause?.stackTrace?.get(0).toString() - val anrView = - LayoutInflater.from(applicationContext) - .inflate(R.layout.anr_screen, null) - measureInflatedView(anrView) - AlfredManager.handleAnrEvent(anrEventProperties, anrView, className) - } - } } }.start() @@ -128,7 +123,8 @@ open class NaviApplication : SplitCompatApplication(), Application.ActivityLifec } try { val crashEventProperties = mutableMapOf( - SCREEN_NAME to exception.stackTrace[0]?.className.orEmpty(), + SCREEN_NAME to (NaviTrackEvent.currentScreenName + ?: exception.stackTrace[0]?.className.orEmpty()), METHOD_NAME to exception.stackTrace[0]?.methodName.orEmpty(), LINE_NUMBER to exception.stackTrace[0]?.lineNumber.toString(), APP_IN_FOREGROUND to isAppInForeground().toString() @@ -136,21 +132,19 @@ open class NaviApplication : SplitCompatApplication(), Application.ActivityLifec NaviTrackEvent.trackEventOnClickStream( GLOBAL_APP_CRASH, crashEventProperties ) - if (isDifferentPackage.not()) { - if (AlfredManager.config.getAlfredStatus() && AlfredManager.config.getCrashEnableStatus()) { - exception.stackTrace[0]?.let { stackTraceElement -> - crashEventProperties[STACK_TRACE] = stackTraceElement.toString() - } - val crashView = - LayoutInflater.from(applicationContext) - .inflate(R.layout.crash_screen, null) - measureInflatedView(crashView) - AlfredManager.handleCrashEvent( - crashEventProperties, - crashView.rootView, - exception.stackTrace[0]?.className.orEmpty() - ) + if (isDifferentPackage.not() && (AlfredManager.config.getAlfredStatus() && AlfredManager.config.getCrashEnableStatus())) { + exception.stackTrace[0]?.let { stackTraceElement -> + crashEventProperties[STACK_TRACE] = stackTraceElement.toString() } + val crashView = + LayoutInflater.from(applicationContext).inflate(R.layout.crash_screen, null) + measureInflatedView(crashView) + AlfredManager.handleCrashEvent( + crashEventProperties, + crashView.rootView, + NaviTrackEvent.currentScreenName + ?: exception.stackTrace[0]?.className.orEmpty() + ) } } finally { defaultHandler?.uncaughtException(thread, exception) @@ -163,7 +157,7 @@ open class NaviApplication : SplitCompatApplication(), Application.ActivityLifec } override fun onActivityCreated(activity: Activity, savedInstanceState: Bundle?) { - if(activity is SplashActivity){ + if (activity is SplashActivity) { AppLaunchUtils.resetAppOpenOnLaunch() } } diff --git a/navi-analytics/src/main/java/com/navi/analytics/alfred/AlfredManager.kt b/navi-analytics/src/main/java/com/navi/analytics/alfred/AlfredManager.kt index 822d66381c..c827a59a73 100644 --- a/navi-analytics/src/main/java/com/navi/analytics/alfred/AlfredManager.kt +++ b/navi-analytics/src/main/java/com/navi/analytics/alfred/AlfredManager.kt @@ -36,6 +36,7 @@ import com.navi.analytics.alfred.network.AlfredRetrofitProvider import com.navi.analytics.alfred.network.model.CruiseResponse import com.navi.analytics.alfred.utils.* import com.navi.analytics.alfred.utils.AlfredConstants.API_METRICS +import com.navi.analytics.alfred.utils.AlfredConstants.CODE_API_BAD_REQUEST import com.navi.analytics.alfred.utils.AlfredConstants.CODE_API_SUCCESS import com.navi.analytics.alfred.utils.AlfredConstants.THIRD_PARTY_MODULE import com.navi.analytics.alfred.worker.AddEventTask @@ -558,7 +559,7 @@ object AlfredManager { AlfredConstants.DEFAULT_INGEST_METRIC_URL, metricRequestBody = request ) - return if (response.isSuccessful && response.code() == CODE_API_SUCCESS) { + return if ((response.isSuccessful && response.code() == CODE_API_SUCCESS) or (response.code() == CODE_API_BAD_REQUEST)) { apiMetricDao.deleteApiMetric(metricEventList.map { it.id }) true } else { @@ -619,7 +620,7 @@ object AlfredManager { val response = repository.sendEvents( config.getPostUrl(), request ) - return if (response.isSuccessful && response.code() == CODE_API_SUCCESS) { + return if ((response.isSuccessful && response.code() == CODE_API_SUCCESS) or (response.code() == CODE_API_BAD_REQUEST)) { analyticsDao.deleteEvents(analyticsEvents.map { it.eventId }) true } else { @@ -876,13 +877,15 @@ object AlfredManager { anrEventProperties: Map, anrView: View, screenName: String? = null ) { startAnrCrashZipUpload(anrView) - coroutineDispatcher.executor.execute { - val event = buildEvent( - AlfredConstants.ANR_EVENT, anrEventProperties as HashMap, - screenName = screenName, - moduleName = currentModuleName - ) - AlfredDispatcher.addTaskToQueue(AddEventTask(event, this.applicationContext)) + if (config.getAlfredSessionId().isNotEmpty()) { + coroutineDispatcher.executor.execute { + val event = buildEvent( + AlfredConstants.ANR_EVENT, anrEventProperties as HashMap, + screenName = screenName, + moduleName = currentModuleName + ) + AlfredDispatcher.addTaskToQueue(AddEventTask(event, this.applicationContext)) + } } } @@ -910,14 +913,16 @@ object AlfredManager { screenName: String? = null ) { startAnrCrashZipUpload(crashView) - coroutineDispatcher.executor.execute { - val event = buildEvent( - AlfredConstants.CRASH_ANALYTICS_EVENT, - crashEventProperties as HashMap, - screenName = screenName, - moduleName = currentModuleName - ) - AlfredDispatcher.addTaskToQueue(AddEventTask(event, applicationContext)) + if (config.getAlfredSessionId().isNotEmpty()) { + coroutineDispatcher.executor.execute { + val event = buildEvent( + AlfredConstants.CRASH_ANALYTICS_EVENT, + crashEventProperties as HashMap, + screenName = screenName, + moduleName = currentModuleName + ) + AlfredDispatcher.addTaskToQueue(AddEventTask(event, applicationContext)) + } } } diff --git a/navi-analytics/src/main/java/com/navi/analytics/alfred/utils/AlfredConstants.kt b/navi-analytics/src/main/java/com/navi/analytics/alfred/utils/AlfredConstants.kt index 2e6e5bf384..62b8d5509d 100644 --- a/navi-analytics/src/main/java/com/navi/analytics/alfred/utils/AlfredConstants.kt +++ b/navi-analytics/src/main/java/com/navi/analytics/alfred/utils/AlfredConstants.kt @@ -9,6 +9,7 @@ package com.navi.analytics.alfred.utils object AlfredConstants { const val CODE_API_SUCCESS = 200 + const val CODE_API_BAD_REQUEST = 400 const val UNDERSCORE = "_" const val ADD_EVENT_TASK = "AddEventTask" const val ADD_API_METRIC_TASK = "AddMetricTask"