Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[DO_NOT_REVIEW]Add success failure status for events #48

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,13 @@ AnalyticsLogger.report(
value = "{\"category\": \"Cart\", \"data\": \"TestData\" }", // Should be Json string.
platform = "EventPlatform",
)
// or
AnalyticsLogger.report(
key = "eventKey",
value = "{\"category\": \"Cart\", \"data\": \"TestData\" }", // Should be Json string.
platform = "EventPlatform",
isSuccess = true, // send your event result
)
```

### Setup
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,13 @@ object AnalyticsLogger {
fun report(key: String?, value: String?, platform: String?) {
// no-op
}

fun report(
key: String?,
value: String?,
platform: String?,
isSuccess: Boolean?,
) {
// no-op
}
}
2 changes: 1 addition & 1 deletion libraries/analytics-logger/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ android {

ext {
PUBLISH_GROUP_ID = 'com.trendyol.android.devtools'
PUBLISH_VERSION = '0.3.0'
PUBLISH_VERSION = '0.4.0'
PUBLISH_ARTIFACT_ID = 'analytics-logger'
PUBLISH_DESCRIPTION = "Android Analytics Event Logger"
PUBLISH_URL = "https://github.com/Trendyol/android-dev-tools"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,21 @@ object AnalyticsLogger {
fun report(key: String?, value: String?, platform: String?) {
instance?.reportEvent(key, value, platform) ?: Log.w(TAG, INIT_ERROR_MESSAGE)
}

/**
* Reports an event with the specified key, value, platform, and success status.
*
* @param key The key identifying the event. Can be null.
* @param value The value associated with the event. Can be null.
* @param platform The platform related to the event. Can be null.
* @param isSuccess Indicates whether the operation was successful. Can be null.
*/
fun report(
key: String?,
value: String?,
platform: String?,
isSuccess: Boolean?,
) {
instance?.reportEvent(key, value, platform, isSuccess) ?: Log.w(TAG, INIT_ERROR_MESSAGE)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,13 @@ internal class NotificationManager constructor(private var showNotification: Boo
key: String?,
value: String?,
platform: String?,
isSuccess: Boolean? = null,
) = scope.launch {
analyticsContainer.eventManager.insert(
key = key,
value = value,
platform = platform,
isSuccess = isSuccess,
)
lastEvent = key to platform
updateNotification(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@ import android.content.Context
import androidx.room.Database
import androidx.room.Room
import androidx.room.RoomDatabase
import androidx.room.migration.Migration
import androidx.sqlite.db.SupportSQLiteDatabase
import com.trendyol.android.devtools.analyticslogger.internal.data.dao.EventDao
import com.trendyol.android.devtools.analyticslogger.internal.data.model.EventEntity

@Database(entities = [EventEntity::class], version = 1)
@Database(entities = [EventEntity::class], version = 2)
internal abstract class EventDatabase : RoomDatabase() {

abstract fun eventDao(): EventDao
Expand All @@ -18,7 +20,17 @@ internal abstract class EventDatabase : RoomDatabase() {
context,
EventDatabase::class.java,
"analytics-logger-database-1",
).build()
)
.addMigrations(*migrations)
.build()
}

private val migrations = arrayOf(
object : Migration(1, 2) {
override fun migrate(db: SupportSQLiteDatabase) {
db.execSQL("ALTER TABLE event_entities ADD COLUMN isSuccess INTEGER")
}
},
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@ internal data class EventEntity(
@ColumnInfo(name = "value") val value: String?,
@ColumnInfo(name = "platform") val platform: String?,
@ColumnInfo(name = "date") val date: String?,
@ColumnInfo(name = "isSuccess") val isSuccess: Boolean?,
)
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ internal interface EventManager {

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

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

suspend fun deleteAll()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ internal class EventManagerImpl(
key: String?,
value: String?,
platform: String?,
isSuccess: Boolean?,
) {
val dateFormat = SimpleDateFormat("HH:mm:ss", Locale.getDefault())
val date = dateFormat.format(Calendar.getInstance().time)
Expand All @@ -38,6 +39,7 @@ internal class EventManagerImpl(
value = value,
platform = platform,
date = date,
isSuccess = isSuccess,
)
)
}
Expand Down Expand Up @@ -70,6 +72,7 @@ internal class EventManagerImpl(
json = eventEntity.value.beautify(moshi),
platform = eventEntity.platform,
date = eventEntity.date,
isSuccess = eventEntity.isSuccess,
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ internal data class Event(
val json: String?,
val platform: String?,
val date: String?,
val isSuccess: Boolean?,
)
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
package com.trendyol.android.devtools.analyticslogger.internal.ui

import android.content.Context
import android.content.res.ColorStateList
import android.graphics.drawable.Drawable
import android.graphics.drawable.GradientDrawable
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.core.content.ContextCompat
import androidx.paging.PagingDataAdapter
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.RecyclerView
import com.trendyol.android.devtools.analyticslogger.R
import com.trendyol.android.devtools.analyticslogger.databinding.AnalyticsLoggerItemEventBinding
import com.trendyol.android.devtools.analyticslogger.internal.domain.model.Event
import com.trendyol.android.devtools.analyticslogger.internal.factory.ColorFactory
Expand Down Expand Up @@ -59,6 +63,7 @@ internal class EventAdapter : PagingDataAdapter<Event, EventAdapter.EventViewHol
textViewPlatform.text = event.platform
textViewDate.text = event.date
textViewPlatform.background = createPlatformBackground(event.platform)
root.background = createStatusBackground(root.context, event.isSuccess)
}

private fun createPlatformBackground(platform: String?): GradientDrawable {
Expand All @@ -69,5 +74,17 @@ internal class EventAdapter : PagingDataAdapter<Event, EventAdapter.EventViewHol
)
}
}

private fun createStatusBackground(context: Context, isSuccess: Boolean?): Drawable? {
if (isSuccess == null) return null

val background = if (isSuccess) {
R.drawable.analytics_logger_success_background
} else {
R.drawable.analytics_logger_failure_background
}

return ContextCompat.getDrawable(context, background)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import android.view.MenuItem
import android.view.View
import android.view.ViewGroup
import android.widget.Toast
import androidx.core.content.ContextCompat
import androidx.fragment.app.Fragment
import androidx.fragment.app.activityViewModels
import androidx.lifecycle.lifecycleScope
Expand All @@ -21,7 +22,6 @@ import com.trendyol.android.devtools.analyticslogger.databinding.AnalyticsLogger
import com.trendyol.android.devtools.analyticslogger.internal.di.ContextContainer
import com.trendyol.android.devtools.analyticslogger.internal.factory.ColorFactory
import com.trendyol.android.devtools.analyticslogger.internal.ui.MainViewModel
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.launch

internal class DetailFragment : Fragment() {
Expand Down Expand Up @@ -64,6 +64,9 @@ internal class DetailFragment : Fragment() {
textViewDate.text = state.event.date
textViewPlatform.text = state.event.platform
textViewPlatform.background = createPlatformBackground(state.event.platform)
horizontalScrollView.background = ContextCompat.getDrawable(
root.context, getStatusBackgroundRes(state.event.isSuccess)
)
}
}

Expand All @@ -76,6 +79,14 @@ internal class DetailFragment : Fragment() {
}
}

private fun getStatusBackgroundRes(isSuccess: Boolean?): Int {
return when (isSuccess) {
true -> R.drawable.analytics_logger_success_background
false -> R.drawable.analytics_logger_failure_background
null -> R.drawable.analytics_logger_json_background
}
}

private fun copyToClipboard() {
val clipboard = context?.getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager
val data = (viewModel.detailState.value as? DetailState.Selected)?.event?.json.orEmpty()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="@color/analyticEventFailureColor"/>
<corners android:radius="4dp" />
</shape>
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="@color/analyticEventSuccessColor"/>
<corners android:radius="4dp" />
</shape>
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical">

<androidx.constraintlayout.widget.ConstraintLayout
Expand Down Expand Up @@ -47,13 +48,14 @@
app:layout_constraintEnd_toEndOf="parent" />

<HorizontalScrollView
android:id="@+id/horizontalScrollView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:background="@drawable/analytics_logger_json_background"
app:layout_constraintTop_toBottomOf="@id/textViewDate"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintVertical_bias="0">
app:layout_constraintVertical_bias="0"
tools:background="@drawable/analytics_logger_json_background">

<TextView
android:id="@+id/textViewValue"
Expand Down
2 changes: 2 additions & 0 deletions libraries/analytics-logger/src/main/res/values/colors.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,6 @@
<color name="colorAccent">#9b59b6</color>

<color name="analyticGridBackgroundColor">#e3e3e3</color>
<color name="analyticEventSuccessColor">#C4F0D8</color>
<color name="analyticEventFailureColor">#f6dde1</color>
</resources>
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,12 @@ class MainFragment : Fragment() {
key = "OnMainFragmentSeenEvent",
value = "{\"category\": \"Cart\", \"data\": \"TestData\" }",
platform = "Firebase",
isSuccess = true,
)
AnalyticsLogger.report(
key = "OnMainFragmentSeenFailEvent",
value = "{\"category\": \"Cart\", \"data\": \"TestData\" }",
platform = "Firebase",
)
}

Expand Down
Loading