Skip to content

Commit 0e06263

Browse files
authored
Merge pull request #115 from Web3Auth/feat/segment_analytics
feat: Segment analytics initialization and events added
2 parents 1df095b + 521ab45 commit 0e06263

File tree

6 files changed

+353
-25
lines changed

6 files changed

+353
-25
lines changed

core/src/main/java/com/web3auth/core/Web3Auth.kt

Lines changed: 278 additions & 15 deletions
Large diffs are not rendered by default.
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package com.web3auth.core.analytics
2+
3+
object AnalyticsEvents {
4+
const val SDK_INITIALIZATION_COMPLETED = "SDK Initialization Completed"
5+
const val SDK_INITIALIZATION_FAILED = "SDK Initialization Failed"
6+
const val CONNECTION_STARTED = "Connection Started"
7+
const val CONNECTION_COMPLETED = "Connection Completed"
8+
const val CONNECTION_FAILED = "Connection Failed"
9+
const val MFA_ENABLEMENT_STARTED = "MFA Enablement Started"
10+
const val MFA_ENABLEMENT_COMPLETED = "MFA Enablement Completed"
11+
const val MFA_ENABLEMENT_FAILED = "MFA Enablement Failed"
12+
const val MFA_MANAGEMENT_STARTED = "MFA Management Started"
13+
const val MFA_MANAGEMENT_FAILED = "MFA Management Failed"
14+
const val MFA_MANAGEMENT_COMPLETED = "MFA Management Completed"
15+
const val WALLET_UI_CLICKED = "Wallet UI Clicked"
16+
const val WALLET_SERVICES_FAILED = "Wallet Services Failed"
17+
const val LOGOUT_STARTED = "Logout Started"
18+
const val LOGOUT_COMPLETED = "Logout Completed"
19+
const val LOGOUT_FAILED = "Logout Failed"
20+
const val REQUEST_FUNCTION_STARTED = "Request Function Started"
21+
const val REQUEST_FUNCTION_COMPLETED = "Request Function Completed"
22+
const val REQUEST_FUNCTION_FAILED = "Request Function Failed"
23+
24+
const val SDK_VERSION = "10.0.0"
25+
}
26+
27+
object AnalyticsSdkType {
28+
const val ANDROID = "android"
29+
}
30+
31+
object SegmentKeys {
32+
const val SEGMENT_WRITE_KEY = "f6LbNqCeVRf512ggdME4b6CyflhF1tsX" // Production key
33+
const val SEGMENT_WRITE_KEY_DEV = "rpE5pCcpA6ME2oFu2TbuVydhOXapjHs3" // Development key
34+
}
35+
36+

core/src/main/java/com/web3auth/core/analytics/AnalyticsManager.kt

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,17 @@ import android.content.Context
44
import com.segment.analytics.Analytics
55
import com.segment.analytics.Properties
66
import com.segment.analytics.Traits
7+
import com.web3auth.core.BuildConfig
78

89
internal object AnalyticsManager {
910

10-
private const val SEGMENT_WRITE_KEY = "YOUR_SECRET_WRITE_KEY"
11+
private const val SEGMENT_WRITE_KEY =
12+
SegmentKeys.SEGMENT_WRITE_KEY // Use (SEGMENT_WRITE_KEY) in production builds and (SEGMENT_WRITE_KEY_DEV) in development/testing builds
1113
private var analytics: Analytics? = null
1214
private var isInitialized = false
1315

16+
private val globalProperties: MutableMap<String, Any> = mutableMapOf()
17+
1418
fun initialize(context: Context) {
1519
if (isInitialized) return
1620

@@ -23,20 +27,25 @@ internal object AnalyticsManager {
2327
isInitialized = true
2428
}
2529

26-
fun trackEvent(eventName: String, properties: Map<String, Any>? = null) {
30+
fun setGlobalProperties(properties: Map<String, Any>) {
31+
globalProperties.putAll(properties)
32+
}
33+
34+
fun trackEvent(eventName: String, properties: Map<String, Any?>? = null) {
2735
if (!isInitialized) return
36+
if (isSkipped()) return
2837

29-
val props = properties?.let {
30-
Properties().apply {
31-
it.forEach { (k, v) -> putValue(k, v) }
32-
}
38+
val combinedProps = Properties().apply {
39+
globalProperties.forEach { (k, v) -> putValue(k, v) }
40+
properties?.forEach { (k, v) -> putValue(k, v) }
3341
}
3442

35-
analytics?.track(eventName, props)
43+
analytics?.track(eventName, combinedProps)
3644
}
3745

38-
fun identifyUser(userId: String, traits: Map<String, Any>? = null) {
46+
fun identify(userId: String, traits: Map<String, Any>? = null) {
3947
if (!isInitialized) return
48+
if (isSkipped()) return
4049

4150
val traitsObj = traits?.let {
4251
Traits().apply {
@@ -50,5 +59,11 @@ internal object AnalyticsManager {
5059
fun reset() {
5160
analytics?.reset()
5261
isInitialized = false
62+
globalProperties.clear()
63+
}
64+
65+
private fun isSkipped(): Boolean {
66+
return BuildConfig.DEBUG
5367
}
5468
}
69+

core/src/main/java/com/web3auth/core/types/MfaSettings.kt

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,16 @@ data class MfaSettings(
1010
@Keep private var passwordFactor: MfaSetting? = null,
1111
@Keep private var passkeysFactor: MfaSetting? = null,
1212
@Keep private var authenticatorFactor: MfaSetting? = null,
13-
)
13+
) {
14+
fun merge(other: MfaSettings?): MfaSettings {
15+
if (other == null) return this
16+
return MfaSettings(
17+
deviceShareFactor = other.deviceShareFactor ?: this.deviceShareFactor,
18+
backUpShareFactor = other.backUpShareFactor ?: this.backUpShareFactor,
19+
socialBackupFactor = other.socialBackupFactor ?: this.socialBackupFactor,
20+
passwordFactor = other.passwordFactor ?: this.passwordFactor,
21+
passkeysFactor = other.passkeysFactor ?: this.passkeysFactor,
22+
authenticatorFactor = other.authenticatorFactor ?: this.authenticatorFactor
23+
)
24+
}
25+
}

core/src/main/java/com/web3auth/core/types/ProjectConfigResponse.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ data class ProjectConfigResponse(
2323
@Keep val wallet_connect_enabled: Boolean?,
2424
@Keep val walletConnectProjectId: String?,
2525
@Keep val whitelabel: WhiteLabelData? = null,
26+
@Keep val teamId: Int? = null,
27+
@Keep val mfaSettings: MfaSettings? = null
2628
)
2729

2830
@Keep

core/src/main/java/com/web3auth/core/types/Web3AuthOptions.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ data class Web3AuthOptions(
2828
@Keep val web3AuthNetwork: Web3AuthNetwork,
2929
@Keep val useSFAKey: Boolean? = false,
3030
@Keep val walletServicesConfig: WalletServicesConfig? = null,
31-
@Keep val mfaSettings: MfaSettings? = null,
31+
@Keep var mfaSettings: MfaSettings? = null,
3232
) {
3333
init {
3434
if (dashboardUrl == null) {

0 commit comments

Comments
 (0)