Skip to content

Commit 47bb23a

Browse files
authored
Merge pull request #8839 from element-hq/feature/bca/add_platformcode_to_posthog
Support reporting super properties to posthog (appPlatform)
2 parents 72575a2 + 7e41d73 commit 47bb23a

File tree

12 files changed

+169
-29
lines changed

12 files changed

+169
-29
lines changed

changelog.d/8839.misc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Posthog | report platform code for EA

matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/Matrix.kt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,5 +147,12 @@ class Matrix(context: Context, matrixConfiguration: MatrixConfiguration) {
147147
fun getSdkVersion(): String {
148148
return BuildConfig.SDK_VERSION + " (" + BuildConfig.GIT_SDK_REVISION + ")"
149149
}
150+
151+
fun getCryptoVersion(longFormat: Boolean): String {
152+
val version = org.matrix.rustcomponents.sdk.crypto.version()
153+
val gitHash = org.matrix.rustcomponents.sdk.crypto.versionInfo().gitSha
154+
val vodozemac = org.matrix.rustcomponents.sdk.crypto.vodozemacVersion()
155+
return if (longFormat) "Rust SDK $version ($gitHash), Vodozemac $vodozemac" else version
156+
}
150157
}
151158
}

matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/session/crypto/CryptoService.kt

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616

1717
package org.matrix.android.sdk.api.session.crypto
1818

19-
import android.content.Context
2019
import androidx.annotation.Size
2120
import androidx.lifecycle.LiveData
2221
import androidx.paging.PagedList
@@ -61,8 +60,6 @@ interface CryptoService {
6160

6261
suspend fun deleteDevices(@Size(min = 1) deviceIds: List<String>, userInteractiveAuthInterceptor: UserInteractiveAuthInterceptor)
6362

64-
fun getCryptoVersion(context: Context, longFormat: Boolean): String
65-
6663
fun isCryptoEnabled(): Boolean
6764

6865
fun isRoomBlacklistUnverifiedDevices(roomId: String?): Boolean

matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/crypto/RustCryptoService.kt

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616

1717
package org.matrix.android.sdk.internal.crypto
1818

19-
import android.content.Context
2019
import androidx.lifecycle.LiveData
2120
import androidx.lifecycle.map
2221
import androidx.paging.PagedList
@@ -184,13 +183,6 @@ internal class RustCryptoService @Inject constructor(
184183
deleteDevices(listOf(deviceId), userInteractiveAuthInterceptor)
185184
}
186185

187-
override fun getCryptoVersion(context: Context, longFormat: Boolean): String {
188-
val version = org.matrix.rustcomponents.sdk.crypto.version()
189-
val gitHash = org.matrix.rustcomponents.sdk.crypto.versionInfo().gitSha
190-
val vodozemac = org.matrix.rustcomponents.sdk.crypto.vodozemacVersion()
191-
return if (longFormat) "Rust SDK $version ($gitHash), Vodozemac $vodozemac" else version
192-
}
193-
194186
override suspend fun getMyCryptoDevice(): CryptoDeviceInfo = withContext(coroutineDispatchers.io) {
195187
olmMachine.ownDevice()
196188
}

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

Lines changed: 8 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,13 @@ 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+
cryptoSDKVersion = Matrix.getCryptoVersion(longFormat = false)
139+
)
140+
)
133141
invitesAcceptor.initialize()
134142
autoRageShaker.initialize()
135143
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.15.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 & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +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.DecryptionFailureTracker
2625
import im.vector.app.features.call.webrtc.WebRtcCallManager
2726
import im.vector.app.features.crypto.keysrequest.KeyRequestHandler
2827
import im.vector.app.features.crypto.verification.IncomingVerificationRequestHandler
@@ -57,7 +56,6 @@ class ActiveSessionHolder @Inject constructor(
5756
private val unregisterUnifiedPushUseCase: UnregisterUnifiedPushUseCase,
5857
private val applicationCoroutineScope: CoroutineScope,
5958
private val coroutineDispatchers: CoroutineDispatchers,
60-
private val decryptionFailureTracker: DecryptionFailureTracker,
6159
) {
6260

6361
private var activeSessionReference: AtomicReference<Session?> = AtomicReference()

vector/src/main/java/im/vector/app/features/analytics/AnalyticsTracker.kt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ package im.vector.app.features.analytics
1818

1919
import im.vector.app.features.analytics.itf.VectorAnalyticsEvent
2020
import im.vector.app.features.analytics.itf.VectorAnalyticsScreen
21+
import im.vector.app.features.analytics.plan.SuperProperties
2122
import im.vector.app.features.analytics.plan.UserProperties
2223

2324
interface AnalyticsTracker {
@@ -35,4 +36,10 @@ interface AnalyticsTracker {
3536
* Update user specific properties.
3637
*/
3738
fun updateUserProperties(userProperties: UserProperties)
39+
40+
/**
41+
* Update the super properties.
42+
* Super properties are added to any tracked event automatically.
43+
*/
44+
fun updateSuperProperties(updatedProperties: SuperProperties)
3845
}

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

Lines changed: 30 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import im.vector.app.features.analytics.VectorAnalytics
2323
import im.vector.app.features.analytics.itf.VectorAnalyticsEvent
2424
import im.vector.app.features.analytics.itf.VectorAnalyticsScreen
2525
import im.vector.app.features.analytics.log.analyticsTag
26+
import im.vector.app.features.analytics.plan.SuperProperties
2627
import im.vector.app.features.analytics.plan.UserProperties
2728
import im.vector.app.features.analytics.store.AnalyticsStore
2829
import kotlinx.coroutines.CoroutineScope
@@ -63,6 +64,8 @@ class DefaultVectorAnalytics @Inject constructor(
6364
// Cache for the properties to send
6465
private var pendingUserProperties: UserProperties? = null
6566

67+
private var superProperties: SuperProperties? = null
68+
6669
override fun init() {
6770
observeUserConsent()
6871
observeAnalyticsId()
@@ -168,20 +171,14 @@ class DefaultVectorAnalytics @Inject constructor(
168171

169172
override fun capture(event: VectorAnalyticsEvent) {
170173
Timber.tag(analyticsTag.value).d("capture($event)")
171-
posthog
172-
?.takeIf { userConsent == true }
173-
?.capture(
174-
event.getName(),
175-
analyticsId,
176-
event.getProperties()?.toPostHogProperties()
174+
posthog?.takeIf { userConsent == true }?.capture(
175+
event.getName(), analyticsId, event.getProperties()?.toPostHogProperties().orEmpty().withSuperProperties()
177176
)
178177
}
179178

180179
override fun screen(screen: VectorAnalyticsScreen) {
181180
Timber.tag(analyticsTag.value).d("screen($screen)")
182-
posthog
183-
?.takeIf { userConsent == true }
184-
?.screen(screen.getName(), screen.getProperties()?.toPostHogProperties())
181+
posthog?.takeIf { userConsent == true }?.screen(screen.getName(), screen.getProperties()?.toPostHogProperties().orEmpty().withSuperProperties())
185182
}
186183

187184
override fun updateUserProperties(userProperties: UserProperties) {
@@ -195,9 +192,7 @@ class DefaultVectorAnalytics @Inject constructor(
195192
private fun doUpdateUserProperties(userProperties: UserProperties) {
196193
// we need a distinct id to set user properties
197194
val distinctId = analyticsId ?: return
198-
posthog
199-
?.takeIf { userConsent == true }
200-
?.identify(distinctId, userProperties.getProperties())
195+
posthog?.takeIf { userConsent == true }?.identify(distinctId, userProperties.getProperties())
201196
}
202197

203198
private fun Map<String, Any?>?.toPostHogProperties(): Map<String, Any>? {
@@ -226,9 +221,32 @@ class DefaultVectorAnalytics @Inject constructor(
226221
return nonNulls
227222
}
228223

224+
/**
225+
* Adds super properties to the actual property set.
226+
* If a property of the same name is already on the reported event it will not be overwritten.
227+
*/
228+
private fun Map<String, Any>.withSuperProperties(): Map<String, Any>? {
229+
val withSuperProperties = this.toMutableMap()
230+
val superProperties = this@DefaultVectorAnalytics.superProperties?.getProperties()
231+
superProperties?.forEach {
232+
if (!withSuperProperties.containsKey(it.key)) {
233+
withSuperProperties[it.key] = it.value
234+
}
235+
}
236+
return withSuperProperties.takeIf { it.isEmpty().not() }
237+
}
238+
229239
override fun trackError(throwable: Throwable) {
230240
sentryAnalytics
231241
.takeIf { userConsent == true }
232242
?.trackError(throwable)
233243
}
244+
245+
override fun updateSuperProperties(updatedProperties: SuperProperties) {
246+
this.superProperties = SuperProperties(
247+
cryptoSDK = updatedProperties.cryptoSDK ?: this.superProperties?.cryptoSDK,
248+
appPlatform = updatedProperties.appPlatform ?: this.superProperties?.appPlatform,
249+
cryptoSDKVersion = updatedProperties.cryptoSDKVersion ?: superProperties?.cryptoSDKVersion
250+
)
251+
}
234252
}

vector/src/main/java/im/vector/app/features/rageshake/BugReporter.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,7 @@ class BugReporter @Inject constructor(
265265
activeSessionHolder.getSafeActiveSession()?.let { session ->
266266
userId = session.myUserId
267267
deviceId = session.sessionParams.deviceId
268-
olmVersion = session.cryptoService().getCryptoVersion(context, true)
268+
olmVersion = Matrix.getCryptoVersion(true)
269269
}
270270

271271
if (!mIsCancelled) {

0 commit comments

Comments
 (0)