Skip to content

Commit

Permalink
Fetch enable analytics flag from Explorer when not set in the SDK
Browse files Browse the repository at this point in the history
  • Loading branch information
jakubuid committed Mar 5, 2024
1 parent 5062618 commit f0dd1d7
Show file tree
Hide file tree
Showing 13 changed files with 83 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,5 +39,6 @@ enum class AndroidCommonDITags {
DECRYPT_AUTH_MESSAGE,
DECRYPT_NOTIFY_MESSAGE,
DECRYPT_USE_CASES,
BUNDLE_ID
BUNDLE_ID,
ENABLE_ANALYTICS
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package com.walletconnect.android.internal.common.di
import com.walletconnect.android.BuildConfig
import com.walletconnect.android.internal.common.modal.Web3ModalApiRepository
import com.walletconnect.android.internal.common.modal.data.network.Web3ModalService
import com.walletconnect.android.internal.common.modal.domain.usecase.EnableAnalyticsUseCase
import com.walletconnect.android.internal.common.modal.domain.usecase.EnableAnalyticsUseCaseInterface
import com.walletconnect.android.internal.common.modal.domain.usecase.GetInstalledWalletsIdsUseCase
import com.walletconnect.android.internal.common.modal.domain.usecase.GetInstalledWalletsIdsUseCaseInterface
import com.walletconnect.android.internal.common.modal.domain.usecase.GetSampleWalletsUseCase
Expand Down Expand Up @@ -60,4 +62,5 @@ internal fun web3ModalModule() = module {
single<GetInstalledWalletsIdsUseCaseInterface> { GetInstalledWalletsIdsUseCase(web3ModalApiRepository = get()) }
single<GetWalletsUseCaseInterface> { GetWalletsUseCase(web3ModalApiRepository = get()) }
single<GetSampleWalletsUseCaseInterface> { GetSampleWalletsUseCase(context = get()) }
single<EnableAnalyticsUseCaseInterface> { EnableAnalyticsUseCase(repository = get()) }
}
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,6 @@ internal class JsonRpcInteractor(
return onFailure(e)
}

println("kobe: Request: $requestJson")

try {
if (jsonRpcHistory.setRequest(payload.id, topic, payload.method, requestJson)) {
val encryptedRequest = chaChaPolyCodec.encrypt(topic, requestJson, envelopeType, participants)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@ internal class Web3ModalApiRepository(
response.body()!!.data.toWalletsAppData().filter { it.isInstalled }
}

suspend fun getAnalyticsConfig(sdkType: String = "w3m") = runCatching {
web3ModalService.getAnalyticsConfig(sdkType = sdkType)
}.mapCatching { response ->
response.body()!!.isAnalyticsEnabled
}

suspend fun getWallets(
sdkType: String,
page: Int,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.walletconnect.android.internal.common.modal.data.network

import com.walletconnect.android.internal.common.modal.data.network.model.EnableAnalyticsDTO
import com.walletconnect.android.internal.common.modal.data.network.model.GetAndroidDataDTO
import com.walletconnect.android.internal.common.modal.data.network.model.GetWalletsDTO
import retrofit2.Response
Expand All @@ -23,4 +24,9 @@ internal interface Web3ModalService {
suspend fun getAndroidData(
@Header("x-sdk-type") sdkType: String,
): Response<GetAndroidDataDTO>

@GET("getAnalyticsConfig")
suspend fun getAnalyticsConfig(
@Header("x-sdk-type") sdkType: String,
): Response<EnableAnalyticsDTO>
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.walletconnect.android.internal.common.modal.data.network.model

import com.squareup.moshi.Json

data class EnableAnalyticsDTO(
@Json(name = "isAnalyticsEnabled")
val isAnalyticsEnabled: Boolean
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.walletconnect.android.internal.common.modal.domain.usecase

import com.walletconnect.android.internal.common.modal.Web3ModalApiRepository
import kotlinx.coroutines.runBlocking

interface EnableAnalyticsUseCaseInterface {
fun fetchAnalyticsConfig(): Boolean
}

internal class EnableAnalyticsUseCase(private val repository: Web3ModalApiRepository) : EnableAnalyticsUseCaseInterface {
override fun fetchAnalyticsConfig(): Boolean {
return runBlocking {
try {
val response = repository.getAnalyticsConfig()
if (response.isSuccess) {
response.getOrDefault(false)
} else {
false
}
} catch (e: Exception) {
false
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,30 +1,36 @@
package com.walletconnect.android.pulse.domain

import com.walletconnect.android.internal.common.di.AndroidCommonDITags
import com.walletconnect.android.internal.common.scope
import com.walletconnect.android.internal.common.wcKoinApp
import com.walletconnect.android.pulse.data.PulseService
import com.walletconnect.android.pulse.model.Event
import com.walletconnect.foundation.util.Logger
import kotlinx.coroutines.launch
import kotlinx.coroutines.supervisorScope
import org.koin.core.qualifier.named

abstract class SendEventUseCase(
private val pulseService: PulseService,
private val logger: Logger,
internal val bundleId: String
) {
private val enableAnalytics: Boolean by lazy { wcKoinApp.koin.get(named(AndroidCommonDITags.ENABLE_ANALYTICS)) }

operator fun invoke(event: Event) {
scope.launch {
supervisorScope {
try {
logger.log("kobe: Sending event: ${event.props.type}")
val response = pulseService.sendEvent(body = event)
if (!response.isSuccessful) {
logger.error("kobe: Failed to send event: ${event.props.type}")
} else {
logger.log("kobe: Event sent successfully: ${event.props.type}")
if (enableAnalytics) {
scope.launch {
supervisorScope {
try {
val response = pulseService.sendEvent(body = event)
if (!response.isSuccessful) {
logger.error("Failed to send event: ${event.props.type}")
} else {
logger.log("Event sent successfully: ${event.props.type}")
}
} catch (e: Exception) {
logger.error("Failed to send event: ${event.props.type}, error: $e")
}
} catch (e: Exception) {
logger.error("kobe: Failed to send event: ${event.props.type}, error: $e")
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ object Modal {
val core: CoreInterface,
val excludedWalletIds: List<String> = listOf(),
val recommendedWalletsIds: List<String> = listOf(),
val coinbaseEnabled: Boolean = true
val coinbaseEnabled: Boolean = true,
val enableAnalytics: Boolean? = true
) : Params()

data class Connect(
Expand Down Expand Up @@ -78,13 +79,13 @@ object Modal {
val metaData: Core.Model.AppMetaData?,
val namespaces: Map<String, Namespace.Session>,
val accounts: List<String>,
): ApprovedSession()
) : ApprovedSession()

data class CoinbaseSession(
val chain: String,
val networkId: String,
val address: String
): ApprovedSession()
) : ApprovedSession()
}

data class RejectedSession(val topic: String, val reason: String) : Model()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.walletconnect.web3.modal.client

import androidx.activity.ComponentActivity
import com.walletconnect.android.internal.common.di.AndroidCommonDITags
import com.walletconnect.android.internal.common.scope
import com.walletconnect.android.internal.common.wcKoinApp
import com.walletconnect.sign.client.Sign
Expand All @@ -20,6 +21,8 @@ import com.walletconnect.web3.modal.engine.Web3ModalEngine
import kotlinx.coroutines.flow.launchIn
import kotlinx.coroutines.flow.onEach
import org.jetbrains.annotations.ApiStatus.Experimental
import org.koin.core.qualifier.named
import org.koin.dsl.module

object Web3Modal {

Expand Down Expand Up @@ -106,6 +109,9 @@ object Web3Modal {
web3ModalEngine = wcKoinApp.koin.get()
web3ModalEngine.setup(init, onError)
web3ModalEngine.setInternalDelegate(Web3ModalDelegate)
wcKoinApp.modules(
module { single(named(AndroidCommonDITags.ENABLE_ANALYTICS)) { init.enableAnalytics ?: web3ModalEngine.fetchAnalyticsConfig() } }
)
}
.onFailure { error -> return@onInitializedClient onError(Modal.Model.Error(error)) }
.onSuccess {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ internal fun engineModule() = module {
sendConnectErrorUseCase = get(),
sendConnectSuccessUseCase = get(),
connectionEventRepository = get(),
enableAnalyticsUseCase = get(),
logger = get(named(AndroidCommonDITags.LOGGER)),
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ internal enum class Web3ModalDITags {
BALANCE_RPC_RETROFIT,
BLOCKCHAIN_RETROFIT,
MOSHI,
SESSION_DATA_STORE
SESSION_DATA_STORE,
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import android.content.Intent
import android.net.Uri
import androidx.activity.ComponentActivity
import androidx.activity.result.contract.ActivityResultContracts
import com.walletconnect.android.internal.common.modal.domain.usecase.EnableAnalyticsUseCaseInterface
import com.walletconnect.android.internal.common.scope
import com.walletconnect.android.internal.common.wcKoinApp
import com.walletconnect.android.pulse.domain.SendConnectErrorUseCase
Expand Down Expand Up @@ -55,8 +56,10 @@ internal class Web3ModalEngine(
private val sendConnectErrorUseCase: SendConnectErrorUseCase,
private val sendConnectSuccessUseCase: SendConnectSuccessUseCase,
private val connectionEventRepository: ConnectionEventRepository,
private val enableAnalyticsUseCase: EnableAnalyticsUseCaseInterface,
private val logger: Logger
) : SendModalLoadedUseCaseInterface by sendModalLoadedUseCase {
) : SendModalLoadedUseCaseInterface by sendModalLoadedUseCase,
EnableAnalyticsUseCaseInterface by enableAnalyticsUseCase {
internal var excludedWalletsIds: MutableList<String> = mutableListOf()
internal var recommendedWalletsIds: MutableList<String> = mutableListOf()

Expand Down

0 comments on commit f0dd1d7

Please sign in to comment.