Init analytics modules (#350)

This commit is contained in:
Yoan Pintas 2023-06-05 15:11:34 +02:00 committed by GitHub
parent 93456e8d44
commit f534ecda96
125 changed files with 2018 additions and 2130 deletions

View file

@ -0,0 +1,158 @@
/*
* Copyright (c) 2023 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 io.element.android.services.analytics.impl
import com.squareup.anvil.annotations.ContributesBinding
import im.vector.app.features.analytics.itf.VectorAnalyticsEvent
import im.vector.app.features.analytics.itf.VectorAnalyticsScreen
import im.vector.app.features.analytics.plan.UserProperties
import io.element.android.libraries.di.AppScope
import io.element.android.libraries.di.SingleIn
import io.element.android.libraries.sessionstorage.api.observer.SessionListener
import io.element.android.libraries.sessionstorage.api.observer.SessionObserver
import io.element.android.services.analytics.api.AnalyticsService
import io.element.android.services.analytics.impl.log.analyticsTag
import io.element.android.services.analytics.impl.store.AnalyticsStore
import io.element.android.services.analyticsproviders.api.AnalyticsProvider
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.launchIn
import kotlinx.coroutines.flow.onEach
import timber.log.Timber
import javax.inject.Inject
@SingleIn(AppScope::class)
@ContributesBinding(AppScope::class, boundType = AnalyticsService::class)
class DefaultAnalyticsService @Inject constructor(
private val analyticsProviders: Set<@JvmSuppressWildcards AnalyticsProvider>,
private val analyticsStore: AnalyticsStore,
// private val lateInitUserPropertiesFactory: LateInitUserPropertiesFactory,
private val coroutineScope: CoroutineScope,
private val sessionObserver: SessionObserver,
) : AnalyticsService, SessionListener {
// Cache for the store values
private var userConsent: Boolean? = null
// Cache for the properties to send
private var pendingUserProperties: UserProperties? = null
init {
observeUserConsent()
observeSessions()
}
override fun getAvailableAnalyticsProviders(): List<AnalyticsProvider> {
return analyticsProviders.sortedBy { it.index }
}
override fun getUserConsent(): Flow<Boolean> {
return analyticsStore.userConsentFlow
}
override suspend fun setUserConsent(userConsent: Boolean) {
Timber.tag(analyticsTag.value).d("setUserConsent($userConsent)")
analyticsStore.setUserConsent(userConsent)
}
override fun didAskUserConsent(): Flow<Boolean> {
return analyticsStore.didAskUserConsentFlow
}
override suspend fun setDidAskUserConsent() {
Timber.tag(analyticsTag.value).d("setDidAskUserConsent()")
analyticsStore.setDidAskUserConsent()
}
override fun getAnalyticsId(): Flow<String> {
return analyticsStore.analyticsIdFlow
}
override suspend fun setAnalyticsId(analyticsId: String) {
Timber.tag(analyticsTag.value).d("setAnalyticsId($analyticsId)")
analyticsStore.setAnalyticsId(analyticsId)
}
override suspend fun onSignOut() {
// stop all providers
analyticsProviders.onEach { it.stop() }
}
override suspend fun onSessionCreated(userId: String) {
// Nothing to do
}
override suspend fun onSessionDeleted(userId: String) {
// Delete the store
analyticsStore.reset()
}
private fun observeUserConsent() {
getUserConsent()
.onEach { consent ->
Timber.tag(analyticsTag.value).d("User consent updated to $consent")
userConsent = consent
initOrStop()
}
.launchIn(coroutineScope)
}
private fun observeSessions() {
sessionObserver.addListener(this)
}
private fun initOrStop() {
userConsent?.let { _userConsent ->
when (_userConsent) {
true -> {
pendingUserProperties?.let {
analyticsProviders.onEach { provider -> provider.updateUserProperties(it) }
pendingUserProperties = null
}
}
false -> {}
}
}
}
override fun capture(event: VectorAnalyticsEvent) {
Timber.tag(analyticsTag.value).d("capture($event)")
if (userConsent == true) {
analyticsProviders.onEach { it.capture(event) }
}
}
override fun screen(screen: VectorAnalyticsScreen) {
Timber.tag(analyticsTag.value).d("screen($screen)")
if (userConsent == true) {
analyticsProviders.onEach { it.screen(screen) }
}
}
override fun updateUserProperties(userProperties: UserProperties) {
if (userConsent == true) {
analyticsProviders.onEach { it.updateUserProperties(userProperties) }
} else {
pendingUserProperties = userProperties
}
}
override fun trackError(throwable: Throwable) {
if (userConsent == true) {
analyticsProviders.onEach { it.trackError(throwable) }
}
}
}

View file

@ -0,0 +1,21 @@
/*
* 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 io.element.android.services.analytics.impl.log
import io.element.android.libraries.core.log.logger.LoggerTag
val analyticsTag = LoggerTag("Analytics")

View file

@ -0,0 +1,86 @@
/*
* 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 io.element.android.services.analytics.impl.store
import android.content.Context
import androidx.datastore.core.DataStore
import androidx.datastore.preferences.core.Preferences
import androidx.datastore.preferences.core.booleanPreferencesKey
import androidx.datastore.preferences.core.edit
import androidx.datastore.preferences.core.stringPreferencesKey
import androidx.datastore.preferences.preferencesDataStore
import io.element.android.libraries.di.ApplicationContext
import io.element.android.libraries.core.bool.orFalse
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.distinctUntilChanged
import kotlinx.coroutines.flow.map
import javax.inject.Inject
/**
* Also accessed via reflection by the instrumentation tests @see [im.vector.app.ClearCurrentSessionRule].
*/
private val Context.dataStore: DataStore<Preferences> by preferencesDataStore(name = "vector_analytics")
/**
* Local storage for:
* - user consent (Boolean);
* - did ask user consent (Boolean);
* - analytics Id (String).
*/
class AnalyticsStore @Inject constructor(
@ApplicationContext private val context: Context
) {
private val userConsent = booleanPreferencesKey("user_consent")
private val didAskUserConsent = booleanPreferencesKey("did_ask_user_consent")
private val analyticsId = stringPreferencesKey("analytics_id")
val userConsentFlow: Flow<Boolean> = context.dataStore.data
.map { preferences -> preferences[userConsent].orFalse() }
.distinctUntilChanged()
val didAskUserConsentFlow: Flow<Boolean> = context.dataStore.data
.map { preferences -> preferences[didAskUserConsent].orFalse() }
.distinctUntilChanged()
val analyticsIdFlow: Flow<String> = context.dataStore.data
.map { preferences -> preferences[analyticsId].orEmpty() }
.distinctUntilChanged()
suspend fun setUserConsent(newUserConsent: Boolean) {
context.dataStore.edit { settings ->
settings[userConsent] = newUserConsent
}
}
suspend fun setDidAskUserConsent(newValue: Boolean = true) {
context.dataStore.edit { settings ->
settings[didAskUserConsent] = newValue
}
}
suspend fun setAnalyticsId(newAnalyticsId: String) {
context.dataStore.edit { settings ->
settings[analyticsId] = newAnalyticsId
}
}
suspend fun reset() {
context.dataStore.edit {
it.clear()
}
}
}