Skip to content

Commit

Permalink
Add countly sessions (#712)
Browse files Browse the repository at this point in the history
**Background**

Add countly session

**Changes**

- Handle session by countly

**Test plan**

See countly dashboard
  • Loading branch information
LionZXY authored Oct 3, 2023
1 parent 4792054 commit de33c90
Show file tree
Hide file tree
Showing 8 changed files with 56 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
- [Feature] Check Self Update App in Options (only for github)
- [Feature] Fap Catalog save sort
- [Feature] Add metrics for faphub
- [Feature] Add countly sessions
- [FIX] Use by default dark theme in Wear OS
- [FIX] Use default splashscreen in Wear OS
- [FIX] Fix compose layout in Wear OS
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package com.flipperdevices.metric.api

import com.flipperdevices.metric.api.events.ComplexEvent
import com.flipperdevices.metric.api.events.SessionState
import com.flipperdevices.metric.api.events.SimpleEvent

interface MetricApi {
fun reportSimpleEvent(simpleEvent: SimpleEvent, arg: String? = null)
fun reportComplexEvent(complexEvent: ComplexEvent)
fun reportSessionState(state: SessionState)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.flipperdevices.metric.api.events

import android.app.Activity
import android.content.res.Configuration

sealed class SessionState {
data class StartSession(val activity: Activity) : SessionState()

data object StopSession : SessionState()

data class ConfigurationChanged(val configuration: Configuration) : SessionState()
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import com.flipperdevices.core.log.LogTagProvider
import com.flipperdevices.core.log.error
import com.flipperdevices.metric.api.MetricApi
import com.flipperdevices.metric.api.events.ComplexEvent
import com.flipperdevices.metric.api.events.SessionState
import com.flipperdevices.metric.api.events.SimpleEvent
import com.flipperdevices.metric.impl.clickhouse.ClickhouseApi
import com.flipperdevices.metric.impl.countly.CountlyApi
Expand Down Expand Up @@ -43,4 +44,8 @@ class MetricApiImpl @Inject constructor(
error(e) { "Failed to report to Clickhouse simple event: $complexEvent" }
}
}

override fun reportSessionState(state: SessionState) {
countlyApi.reportSessionState(state)
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package com.flipperdevices.metric.impl.countly

import com.flipperdevices.metric.api.events.SessionState

interface CountlyApi {
fun reportEvent(id: String, params: Map<String, Any?>? = null)
fun reportSessionState(state: SessionState)
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import com.flipperdevices.core.log.error
import com.flipperdevices.core.log.info
import com.flipperdevices.core.log.verbose
import com.flipperdevices.core.preference.pb.Settings
import com.flipperdevices.metric.api.events.SessionState
import com.flipperdevices.metric.impl.BuildConfig
import com.squareup.anvil.annotations.ContributesBinding
import kotlinx.coroutines.CoroutineScope
Expand Down Expand Up @@ -47,6 +48,14 @@ class CountlyApiImpl @Inject constructor(
}
}

override fun reportSessionState(state: SessionState) {
when (state) {
is SessionState.ConfigurationChanged -> countly.onConfigurationChanged(state.configuration)
is SessionState.StartSession -> countly.onStart(state.activity)
SessionState.StopSession -> countly.onStop()
}
}

private fun reportEventUnsafe(
id: String,
params: Map<String, Any?>?
Expand Down Expand Up @@ -91,6 +100,7 @@ private fun filterParams(params: Map<String, Any?>): Map<String, Any?> {
is Int,
is Double,
is Boolean -> mapValue

is Long -> mapValue.toIntSafe()
null -> null
else -> mapValue.toString()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package com.flipperdevices.metric.noop
import com.flipperdevices.core.di.AppGraph
import com.flipperdevices.metric.api.MetricApi
import com.flipperdevices.metric.api.events.ComplexEvent
import com.flipperdevices.metric.api.events.SessionState
import com.flipperdevices.metric.api.events.SimpleEvent
import com.squareup.anvil.annotations.ContributesBinding
import javax.inject.Inject
Expand All @@ -12,4 +13,6 @@ class NoopMetricApiImpl @Inject constructor() : MetricApi {
override fun reportSimpleEvent(simpleEvent: SimpleEvent, arg: String?) = Unit

override fun reportComplexEvent(complexEvent: ComplexEvent) = Unit

override fun reportSessionState(state: SessionState) = Unit
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.flipperdevices.singleactivity.impl

import android.content.Intent
import android.content.res.Configuration
import android.os.Bundle
import androidx.activity.compose.setContent
import androidx.appcompat.app.AppCompatActivity
Expand All @@ -24,6 +25,8 @@ import com.flipperdevices.core.ui.navigation.ComposableFeatureEntry
import com.flipperdevices.core.ui.navigation.LocalGlobalNavigationNavStack
import com.flipperdevices.core.ui.theme.FlipperTheme
import com.flipperdevices.deeplink.model.Deeplink
import com.flipperdevices.metric.api.MetricApi
import com.flipperdevices.metric.api.events.SessionState
import com.flipperdevices.selfupdater.api.SelfUpdaterApi
import com.flipperdevices.singleactivity.impl.composable.ComposableSingleActivityNavHost
import com.flipperdevices.singleactivity.impl.di.SingleActivityComponent
Expand Down Expand Up @@ -55,6 +58,9 @@ class SingleActivity :
@Inject
lateinit var appOpenMetricReported: AppOpenMetricReported

@Inject
lateinit var metricApi: MetricApi

private var globalNavController: NavHostController? = null

override fun onCreate(savedInstanceState: Bundle?) {
Expand Down Expand Up @@ -126,4 +132,18 @@ class SingleActivity :
}
}
}

override fun onStart() {
super.onStart()
metricApi.reportSessionState(SessionState.StartSession(this))
}

override fun onConfigurationChanged(newConfig: Configuration) {
super.onConfigurationChanged(newConfig)
metricApi.reportSessionState(SessionState.ConfigurationChanged(newConfig))
}
override fun onStop() {
super.onStop()
metricApi.reportSessionState(SessionState.StopSession)
}
}

0 comments on commit de33c90

Please sign in to comment.