Skip to content

Commit a363e39

Browse files
committed
Update to analytics events 0.23.0 and refactor
1 parent 09c68f3 commit a363e39

File tree

6 files changed

+75
-34
lines changed

6 files changed

+75
-34
lines changed

vector-app/src/main/java/im/vector/app/VectorApplication.kt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ import im.vector.app.core.pushers.FcmHelper
5353
import im.vector.app.core.resources.BuildMeta
5454
import im.vector.app.features.analytics.DecryptionFailureTracker
5555
import im.vector.app.features.analytics.VectorAnalytics
56+
import im.vector.app.features.analytics.plan.SuperProperties
5657
import im.vector.app.features.call.webrtc.WebRtcCallManager
5758
import im.vector.app.features.configuration.VectorConfiguration
5859
import im.vector.app.features.invite.InvitesAcceptor
@@ -130,6 +131,12 @@ class VectorApplication :
130131
appContext = this
131132
flipperProxy.init(matrix)
132133
vectorAnalytics.init()
134+
vectorAnalytics.updateSuperProperties(
135+
SuperProperties(
136+
appPlatform = SuperProperties.AppPlatform.EA,
137+
cryptoSDK = SuperProperties.CryptoSDK.Rust,
138+
)
139+
)
133140
invitesAcceptor.initialize()
134141
autoRageShaker.initialize()
135142
decryptionFailureTracker.start()

vector/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ dependencies {
160160
api 'com.facebook.stetho:stetho:1.6.0'
161161

162162
// Analytics
163-
api 'com.github.matrix-org:matrix-analytics-events:0.22.0'
163+
api 'com.github.matrix-org:matrix-analytics-events:0.23.0'
164164

165165
api libs.google.phonenumber
166166

vector/src/main/java/im/vector/app/core/di/ActiveSessionHolder.kt

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,6 @@ import im.vector.app.core.dispatchers.CoroutineDispatchers
2222
import im.vector.app.core.pushers.UnregisterUnifiedPushUseCase
2323
import im.vector.app.core.services.GuardServiceStarter
2424
import im.vector.app.core.session.ConfigureAndStartSessionUseCase
25-
import im.vector.app.features.analytics.VectorAnalytics
26-
import im.vector.app.features.analytics.plan.SuperProperties
2725
import im.vector.app.features.call.webrtc.WebRtcCallManager
2826
import im.vector.app.features.crypto.keysrequest.KeyRequestHandler
2927
import im.vector.app.features.crypto.verification.IncomingVerificationRequestHandler
@@ -58,7 +56,6 @@ class ActiveSessionHolder @Inject constructor(
5856
private val unregisterUnifiedPushUseCase: UnregisterUnifiedPushUseCase,
5957
private val applicationCoroutineScope: CoroutineScope,
6058
private val coroutineDispatchers: CoroutineDispatchers,
61-
private val vectorAnalytics: VectorAnalytics,
6259
) {
6360

6461
private var activeSessionReference: AtomicReference<Session?> = AtomicReference()
@@ -75,13 +72,6 @@ class ActiveSessionHolder @Inject constructor(
7572
session.callSignalingService().addCallListener(callManager)
7673
imageManager.onSessionStarted(session)
7774
guardServiceStarter.start()
78-
vectorAnalytics.updateSuperProperties(
79-
SuperProperties(
80-
platformCodeName = SuperProperties.PlatformCodeName.EA,
81-
cryptoSDK = SuperProperties.CryptoSDK.Rust,
82-
cryptoSDKVersion = session.cryptoService().getCryptoVersion(applicationContext, false)
83-
)
84-
)
8575
}
8676

8777
suspend fun clearActiveSession() {
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*
2+
* Copyright (c) 2024 New Vector Ltd
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package im.vector.app.features.analytics.impl
18+
19+
import im.vector.app.ActiveSessionDataSource
20+
import im.vector.app.features.analytics.plan.SuperProperties
21+
import kotlinx.coroutines.flow.Flow
22+
import kotlinx.coroutines.flow.distinctUntilChanged
23+
import kotlinx.coroutines.flow.map
24+
import javax.inject.Inject
25+
26+
/**
27+
* Gathers the super properties that are static to this platform or
28+
* that can be automatically resolved from the current session.
29+
*/
30+
class AutoSuperPropertiesFlowProvider @Inject constructor(
31+
activeSessionDataSource: ActiveSessionDataSource,
32+
) {
33+
34+
val superPropertiesFlow: Flow<SuperProperties> = activeSessionDataSource.stream()
35+
.map { session ->
36+
SuperProperties(
37+
appPlatform = SuperProperties.AppPlatform.EA,
38+
cryptoSDK = SuperProperties.CryptoSDK.Rust,
39+
cryptoSDKVersion = session.getOrNull()?.cryptoService()?.getCryptoVersion(false)
40+
)
41+
}
42+
.distinctUntilChanged()
43+
}

vector/src/main/java/im/vector/app/features/analytics/impl/DefaultVectorAnalytics.kt

Lines changed: 14 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ class DefaultVectorAnalytics @Inject constructor(
4242
private val analyticsConfig: AnalyticsConfig,
4343
private val analyticsStore: AnalyticsStore,
4444
private val lateInitUserPropertiesFactory: LateInitUserPropertiesFactory,
45+
private val autoSuperPropertiesFlowProvider: AutoSuperPropertiesFlowProvider,
4546
@NamedGlobalScope private val globalScope: CoroutineScope
4647
) : VectorAnalytics {
4748

@@ -69,6 +70,7 @@ class DefaultVectorAnalytics @Inject constructor(
6970
override fun init() {
7071
observeUserConsent()
7172
observeAnalyticsId()
73+
observeAutoSuperProperties()
7274
}
7375

7476
override fun getUserConsent(): Flow<Boolean> {
@@ -116,6 +118,12 @@ class DefaultVectorAnalytics @Inject constructor(
116118
.launchIn(globalScope)
117119
}
118120

121+
private fun observeAutoSuperProperties() {
122+
autoSuperPropertiesFlowProvider.superPropertiesFlow.onEach {
123+
updateSuperProperties(it)
124+
}.launchIn(globalScope)
125+
}
126+
119127
private suspend fun identifyPostHog() {
120128
val id = analyticsId ?: return
121129
if (!userConsent.orFalse()) return
@@ -171,20 +179,14 @@ class DefaultVectorAnalytics @Inject constructor(
171179

172180
override fun capture(event: VectorAnalyticsEvent) {
173181
Timber.tag(analyticsTag.value).d("capture($event)")
174-
posthog
175-
?.takeIf { userConsent == true }
176-
?.capture(
177-
event.getName(),
178-
analyticsId,
179-
event.getProperties()?.toPostHogProperties().orEmpty().withSuperProperties()
182+
posthog?.takeIf { userConsent == true }?.capture(
183+
event.getName(), analyticsId, event.getProperties()?.toPostHogProperties().orEmpty().withSuperProperties()
180184
)
181185
}
182186

183187
override fun screen(screen: VectorAnalyticsScreen) {
184188
Timber.tag(analyticsTag.value).d("screen($screen)")
185-
posthog
186-
?.takeIf { userConsent == true }
187-
?.screen(screen.getName(), screen.getProperties()?.toPostHogProperties().orEmpty().withSuperProperties())
189+
posthog?.takeIf { userConsent == true }?.screen(screen.getName(), screen.getProperties()?.toPostHogProperties().orEmpty().withSuperProperties())
188190
}
189191

190192
override fun updateUserProperties(userProperties: UserProperties) {
@@ -198,9 +200,7 @@ class DefaultVectorAnalytics @Inject constructor(
198200
private fun doUpdateUserProperties(userProperties: UserProperties) {
199201
// we need a distinct id to set user properties
200202
val distinctId = analyticsId ?: return
201-
posthog
202-
?.takeIf { userConsent == true }
203-
?.identify(distinctId, userProperties.getProperties())
203+
posthog?.takeIf { userConsent == true }?.identify(distinctId, userProperties.getProperties())
204204
}
205205

206206
private fun Map<String, Any?>?.toPostHogProperties(): Map<String, Any>? {
@@ -233,15 +233,15 @@ class DefaultVectorAnalytics @Inject constructor(
233233
* Adds super properties to the actual property set.
234234
* If a property of the same name is already on the reported event it will not be overwritten.
235235
*/
236-
private fun Map<String, Any>.withSuperProperties(): Map<String, Any> {
236+
private fun Map<String, Any>.withSuperProperties(): Map<String, Any>? {
237237
val withSuperProperties = this.toMutableMap()
238238
val superProperties = this@DefaultVectorAnalytics.superProperties?.getProperties()
239239
superProperties?.forEach {
240240
if (!withSuperProperties.containsKey(it.key)) {
241241
withSuperProperties[it.key] = it.value
242242
}
243243
}
244-
return withSuperProperties
244+
return withSuperProperties.takeIf { it.isEmpty().not() }
245245
}
246246

247247
override fun trackError(throwable: Throwable) {
@@ -251,13 +251,7 @@ class DefaultVectorAnalytics @Inject constructor(
251251
}
252252

253253
override fun updateSuperProperties(updatedProperties: SuperProperties) {
254-
if (this.superProperties == null) {
255-
this.superProperties = updatedProperties
256-
return
257-
}
258-
259254
this.superProperties = SuperProperties(
260-
platformCodeName = updatedProperties.platformCodeName ?: this.superProperties?.platformCodeName,
261255
cryptoSDK = updatedProperties.cryptoSDK ?: this.superProperties?.cryptoSDK,
262256
appPlatform = updatedProperties.appPlatform ?: this.superProperties?.appPlatform,
263257
cryptoSDKVersion = updatedProperties.cryptoSDKVersion ?: superProperties?.cryptoSDKVersion

vector/src/test/java/im/vector/app/features/analytics/impl/DefaultVectorAnalyticsTest.kt

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,12 @@ import im.vector.app.test.fixtures.AnalyticsConfigFixture.anAnalyticsConfig
2626
import im.vector.app.test.fixtures.aUserProperties
2727
import im.vector.app.test.fixtures.aVectorAnalyticsEvent
2828
import im.vector.app.test.fixtures.aVectorAnalyticsScreen
29+
import io.mockk.every
30+
import io.mockk.mockk
2931
import kotlinx.coroutines.CoroutineScope
3032
import kotlinx.coroutines.Dispatchers
3133
import kotlinx.coroutines.ExperimentalCoroutinesApi
34+
import kotlinx.coroutines.flow.flowOf
3235
import kotlinx.coroutines.test.runTest
3336
import org.junit.Before
3437
import org.junit.Test
@@ -45,14 +48,18 @@ class DefaultVectorAnalyticsTest {
4548
private val fakeAnalyticsStore = FakeAnalyticsStore()
4649
private val fakeLateInitUserPropertiesFactory = FakeLateInitUserPropertiesFactory()
4750
private val fakeSentryAnalytics = FakeSentryAnalytics()
51+
private val mockAutoSuperPropertiesFlowProvider = mockk<AutoSuperPropertiesFlowProvider>().also {
52+
every { it.superPropertiesFlow } returns flowOf(SuperProperties())
53+
}
4854

4955
private val defaultVectorAnalytics = DefaultVectorAnalytics(
5056
postHogFactory = FakePostHogFactory(fakePostHog.instance).instance,
5157
sentryAnalytics = fakeSentryAnalytics.instance,
5258
analyticsStore = fakeAnalyticsStore.instance,
5359
globalScope = CoroutineScope(Dispatchers.Unconfined),
5460
analyticsConfig = anAnalyticsConfig(isEnabled = true),
55-
lateInitUserPropertiesFactory = fakeLateInitUserPropertiesFactory.instance
61+
lateInitUserPropertiesFactory = fakeLateInitUserPropertiesFactory.instance,
62+
autoSuperPropertiesFlowProvider = mockAutoSuperPropertiesFlowProvider,
5663
)
5764

5865
@Before
@@ -180,7 +187,7 @@ class DefaultVectorAnalyticsTest {
180187
fakeAnalyticsStore.givenUserContent(consent = true)
181188

182189
val updatedProperties = SuperProperties(
183-
platformCodeName = SuperProperties.PlatformCodeName.EA,
190+
appPlatform = SuperProperties.AppPlatform.EA,
184191
cryptoSDKVersion = "0.0",
185192
cryptoSDK = SuperProperties.CryptoSDK.Rust
186193
)
@@ -214,7 +221,7 @@ class DefaultVectorAnalyticsTest {
214221
fakeAnalyticsStore.givenUserContent(consent = true)
215222

216223
val superProperties = SuperProperties(
217-
platformCodeName = SuperProperties.PlatformCodeName.EA,
224+
appPlatform = SuperProperties.AppPlatform.EA,
218225
cryptoSDKVersion = "0.0",
219226
cryptoSDK = SuperProperties.CryptoSDK.Rust
220227
)

0 commit comments

Comments
 (0)