Skip to content

Commit 8ca1fdf

Browse files
authored
Merge pull request #48 from Trendyol/feature/add-success-failure-status
Add success failure status for events
2 parents 02f1a02 + a9ecefe commit 8ca1fdf

File tree

17 files changed

+108
-7
lines changed

17 files changed

+108
-7
lines changed

README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,13 @@ AnalyticsLogger.report(
127127
value = "{\"category\": \"Cart\", \"data\": \"TestData\" }", // Should be Json string.
128128
platform = "EventPlatform",
129129
)
130+
// or
131+
AnalyticsLogger.report(
132+
key = "eventKey",
133+
value = "{\"category\": \"Cart\", \"data\": \"TestData\" }", // Should be Json string.
134+
platform = "EventPlatform",
135+
isSuccess = true, // send your event result
136+
)
130137
```
131138

132139
### Setup

libraries/analytics-logger-no-op/src/main/java/com/trendyol/android/devtools/analyticslogger/AnalyticsLogger.kt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,13 @@ object AnalyticsLogger {
2424
fun report(key: String?, value: String?, platform: String?) {
2525
// no-op
2626
}
27+
28+
fun report(
29+
key: String?,
30+
value: String?,
31+
platform: String?,
32+
isSuccess: Boolean?,
33+
) {
34+
// no-op
35+
}
2736
}

libraries/analytics-logger/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ android {
3737

3838
ext {
3939
PUBLISH_GROUP_ID = 'com.trendyol.android.devtools'
40-
PUBLISH_VERSION = '0.3.0'
40+
PUBLISH_VERSION = '0.4.0'
4141
PUBLISH_ARTIFACT_ID = 'analytics-logger'
4242
PUBLISH_DESCRIPTION = "Android Analytics Event Logger"
4343
PUBLISH_URL = "https://github.com/Trendyol/android-dev-tools"

libraries/analytics-logger/src/main/java/com/trendyol/android/devtools/analyticslogger/AnalyticsLogger.kt

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,21 @@ object AnalyticsLogger {
3131
fun report(key: String?, value: String?, platform: String?) {
3232
instance?.reportEvent(key, value, platform) ?: Log.w(TAG, INIT_ERROR_MESSAGE)
3333
}
34+
35+
/**
36+
* Reports an event with the specified key, value, platform, and success status.
37+
*
38+
* @param key The key identifying the event. Can be null.
39+
* @param value The value associated with the event. Can be null.
40+
* @param platform The platform related to the event. Can be null.
41+
* @param isSuccess Indicates whether the operation was successful. Can be null.
42+
*/
43+
fun report(
44+
key: String?,
45+
value: String?,
46+
platform: String?,
47+
isSuccess: Boolean?,
48+
) {
49+
instance?.reportEvent(key, value, platform, isSuccess) ?: Log.w(TAG, INIT_ERROR_MESSAGE)
50+
}
3451
}

libraries/analytics-logger/src/main/java/com/trendyol/android/devtools/analyticslogger/internal/NotificationManager.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,11 +60,13 @@ internal class NotificationManager constructor(private var showNotification: Boo
6060
key: String?,
6161
value: String?,
6262
platform: String?,
63+
isSuccess: Boolean? = null,
6364
) = scope.launch {
6465
analyticsContainer.eventManager.insert(
6566
key = key,
6667
value = value,
6768
platform = platform,
69+
isSuccess = isSuccess,
6870
)
6971
lastEvent = key to platform
7072
updateNotification(

libraries/analytics-logger/src/main/java/com/trendyol/android/devtools/analyticslogger/internal/data/database/EventDatabase.kt

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,12 @@ import android.content.Context
44
import androidx.room.Database
55
import androidx.room.Room
66
import androidx.room.RoomDatabase
7+
import androidx.room.migration.Migration
8+
import androidx.sqlite.db.SupportSQLiteDatabase
79
import com.trendyol.android.devtools.analyticslogger.internal.data.dao.EventDao
810
import com.trendyol.android.devtools.analyticslogger.internal.data.model.EventEntity
911

10-
@Database(entities = [EventEntity::class], version = 1)
12+
@Database(entities = [EventEntity::class], version = 2)
1113
internal abstract class EventDatabase : RoomDatabase() {
1214

1315
abstract fun eventDao(): EventDao
@@ -18,7 +20,17 @@ internal abstract class EventDatabase : RoomDatabase() {
1820
context,
1921
EventDatabase::class.java,
2022
"analytics-logger-database-1",
21-
).build()
23+
)
24+
.addMigrations(*migrations)
25+
.build()
2226
}
27+
28+
private val migrations = arrayOf(
29+
object : Migration(1, 2) {
30+
override fun migrate(db: SupportSQLiteDatabase) {
31+
db.execSQL("ALTER TABLE event_entities ADD COLUMN isSuccess INTEGER")
32+
}
33+
},
34+
)
2335
}
2436
}

libraries/analytics-logger/src/main/java/com/trendyol/android/devtools/analyticslogger/internal/data/model/EventEntity.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,5 @@ internal data class EventEntity(
1111
@ColumnInfo(name = "value") val value: String?,
1212
@ColumnInfo(name = "platform") val platform: String?,
1313
@ColumnInfo(name = "date") val date: String?,
14+
@ColumnInfo(name = "isSuccess") val isSuccess: Boolean?,
1415
)

libraries/analytics-logger/src/main/java/com/trendyol/android/devtools/analyticslogger/internal/domain/manager/EventManager.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ internal interface EventManager {
66

77
suspend fun find(query: String?, platform: String, page: Int, pageSize: Int): List<Event>
88

9-
suspend fun insert(key: String?, value: String?, platform: String?)
9+
suspend fun insert(key: String?, value: String?, platform: String?, isSuccess: Boolean? = null)
1010

1111
suspend fun deleteAll()
1212

libraries/analytics-logger/src/main/java/com/trendyol/android/devtools/analyticslogger/internal/domain/manager/EventManagerImpl.kt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ internal class EventManagerImpl(
2828
key: String?,
2929
value: String?,
3030
platform: String?,
31+
isSuccess: Boolean?,
3132
) {
3233
val dateFormat = SimpleDateFormat("HH:mm:ss", Locale.getDefault())
3334
val date = dateFormat.format(Calendar.getInstance().time)
@@ -38,6 +39,7 @@ internal class EventManagerImpl(
3839
value = value,
3940
platform = platform,
4041
date = date,
42+
isSuccess = isSuccess,
4143
)
4244
)
4345
}
@@ -70,6 +72,7 @@ internal class EventManagerImpl(
7072
json = eventEntity.value.beautify(moshi),
7173
platform = eventEntity.platform,
7274
date = eventEntity.date,
75+
isSuccess = eventEntity.isSuccess,
7376
)
7477
}
7578

libraries/analytics-logger/src/main/java/com/trendyol/android/devtools/analyticslogger/internal/domain/model/Event.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,5 @@ internal data class Event(
77
val json: String?,
88
val platform: String?,
99
val date: String?,
10+
val isSuccess: Boolean?,
1011
)

libraries/analytics-logger/src/main/java/com/trendyol/android/devtools/analyticslogger/internal/ui/EventAdapter.kt

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
11
package com.trendyol.android.devtools.analyticslogger.internal.ui
22

3+
import android.content.Context
34
import android.content.res.ColorStateList
5+
import android.graphics.drawable.Drawable
46
import android.graphics.drawable.GradientDrawable
57
import android.view.LayoutInflater
68
import android.view.ViewGroup
9+
import androidx.core.content.ContextCompat
710
import androidx.paging.PagingDataAdapter
811
import androidx.recyclerview.widget.DiffUtil
912
import androidx.recyclerview.widget.RecyclerView
13+
import com.trendyol.android.devtools.analyticslogger.R
1014
import com.trendyol.android.devtools.analyticslogger.databinding.AnalyticsLoggerItemEventBinding
1115
import com.trendyol.android.devtools.analyticslogger.internal.domain.model.Event
1216
import com.trendyol.android.devtools.analyticslogger.internal.factory.ColorFactory
@@ -59,6 +63,7 @@ internal class EventAdapter : PagingDataAdapter<Event, EventAdapter.EventViewHol
5963
textViewPlatform.text = event.platform
6064
textViewDate.text = event.date
6165
textViewPlatform.background = createPlatformBackground(event.platform)
66+
root.background = createStatusBackground(root.context, event.isSuccess)
6267
}
6368

6469
private fun createPlatformBackground(platform: String?): GradientDrawable {
@@ -69,5 +74,17 @@ internal class EventAdapter : PagingDataAdapter<Event, EventAdapter.EventViewHol
6974
)
7075
}
7176
}
77+
78+
private fun createStatusBackground(context: Context, isSuccess: Boolean?): Drawable? {
79+
if (isSuccess == null) return null
80+
81+
val background = if (isSuccess) {
82+
R.drawable.analytics_logger_success_background
83+
} else {
84+
R.drawable.analytics_logger_failure_background
85+
}
86+
87+
return ContextCompat.getDrawable(context, background)
88+
}
7289
}
7390
}

libraries/analytics-logger/src/main/java/com/trendyol/android/devtools/analyticslogger/internal/ui/detail/DetailFragment.kt

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import android.view.MenuItem
1313
import android.view.View
1414
import android.view.ViewGroup
1515
import android.widget.Toast
16+
import androidx.core.content.ContextCompat
1617
import androidx.fragment.app.Fragment
1718
import androidx.fragment.app.activityViewModels
1819
import androidx.lifecycle.lifecycleScope
@@ -21,7 +22,6 @@ import com.trendyol.android.devtools.analyticslogger.databinding.AnalyticsLogger
2122
import com.trendyol.android.devtools.analyticslogger.internal.di.ContextContainer
2223
import com.trendyol.android.devtools.analyticslogger.internal.factory.ColorFactory
2324
import com.trendyol.android.devtools.analyticslogger.internal.ui.MainViewModel
24-
import kotlinx.coroutines.flow.collect
2525
import kotlinx.coroutines.launch
2626

2727
internal class DetailFragment : Fragment() {
@@ -64,6 +64,10 @@ internal class DetailFragment : Fragment() {
6464
textViewDate.text = state.event.date
6565
textViewPlatform.text = state.event.platform
6666
textViewPlatform.background = createPlatformBackground(state.event.platform)
67+
horizontalScrollView.background = ContextCompat.getDrawable(
68+
/* context = */ root.context,
69+
/* id = */ getStatusBackgroundRes(state.event.isSuccess)
70+
)
6771
}
6872
}
6973

@@ -76,6 +80,14 @@ internal class DetailFragment : Fragment() {
7680
}
7781
}
7882

83+
private fun getStatusBackgroundRes(isSuccess: Boolean?): Int {
84+
return when (isSuccess) {
85+
true -> R.drawable.analytics_logger_success_background
86+
false -> R.drawable.analytics_logger_failure_background
87+
null -> R.drawable.analytics_logger_json_background
88+
}
89+
}
90+
7991
private fun copyToClipboard() {
8092
val clipboard = context?.getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager
8193
val data = (viewModel.detailState.value as? DetailState.Selected)?.event?.json.orEmpty()
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<shape xmlns:android="http://schemas.android.com/apk/res/android">
3+
<solid android:color="@color/analyticEventFailureColor"/>
4+
<corners android:radius="4dp" />
5+
</shape>
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<shape xmlns:android="http://schemas.android.com/apk/res/android">
3+
<solid android:color="@color/analyticEventSuccessColor"/>
4+
<corners android:radius="4dp" />
5+
</shape>

libraries/analytics-logger/src/main/res/layout/analytics_logger_fragment_detail.xml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
android:layout_width="match_parent"
44
android:layout_height="match_parent"
55
xmlns:app="http://schemas.android.com/apk/res-auto"
6+
xmlns:tools="http://schemas.android.com/tools"
67
android:orientation="vertical">
78

89
<androidx.constraintlayout.widget.ConstraintLayout
@@ -47,13 +48,14 @@
4748
app:layout_constraintEnd_toEndOf="parent" />
4849

4950
<HorizontalScrollView
51+
android:id="@+id/horizontalScrollView"
5052
android:layout_width="match_parent"
5153
android:layout_height="wrap_content"
5254
android:layout_marginTop="16dp"
53-
android:background="@drawable/analytics_logger_json_background"
5455
app:layout_constraintTop_toBottomOf="@id/textViewDate"
5556
app:layout_constraintBottom_toBottomOf="parent"
56-
app:layout_constraintVertical_bias="0">
57+
app:layout_constraintVertical_bias="0"
58+
tools:background="@drawable/analytics_logger_json_background">
5759

5860
<TextView
5961
android:id="@+id/textViewValue"

libraries/analytics-logger/src/main/res/values/colors.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,6 @@
77
<color name="colorAccent">#9b59b6</color>
88

99
<color name="analyticGridBackgroundColor">#e3e3e3</color>
10+
<color name="analyticEventSuccessColor">#C4F0D8</color>
11+
<color name="analyticEventFailureColor">#f6dde1</color>
1012
</resources>

sample/src/main/java/com/trendyol/android/devtools/ui/main/MainFragment.kt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,12 @@ class MainFragment : Fragment() {
7575
key = "OnMainFragmentSeenEvent",
7676
value = "{\"category\": \"Cart\", \"data\": \"TestData\" }",
7777
platform = "Firebase",
78+
isSuccess = true,
79+
)
80+
AnalyticsLogger.report(
81+
key = "OnMainFragmentSeenFailEvent",
82+
value = "{\"category\": \"Cart\", \"data\": \"TestData\" }",
83+
platform = "Firebase",
7884
)
7985
}
8086

0 commit comments

Comments
 (0)