|
16 | 16 |
|
17 | 17 | package im.vector.app.features.analytics.impl
|
18 | 18 |
|
| 19 | +import im.vector.app.features.analytics.plan.SuperProperties |
19 | 20 | import im.vector.app.test.fakes.FakeAnalyticsStore
|
20 | 21 | import im.vector.app.test.fakes.FakeLateInitUserPropertiesFactory
|
21 | 22 | import im.vector.app.test.fakes.FakePostHog
|
@@ -174,6 +175,117 @@ class DefaultVectorAnalyticsTest {
|
174 | 175 | fakeSentryAnalytics.verifyNoErrorTracking()
|
175 | 176 | }
|
176 | 177 |
|
| 178 | + @Test |
| 179 | + fun `Super properties should be added to all captured events`() = runTest { |
| 180 | + fakeAnalyticsStore.givenUserContent(consent = true) |
| 181 | + |
| 182 | + val updatedProperties = SuperProperties( |
| 183 | + platformCodeName = SuperProperties.PlatformCodeName.EA, |
| 184 | + cryptoSDKVersion = "0.0", |
| 185 | + cryptoSDK = SuperProperties.CryptoSDK.Rust |
| 186 | + ) |
| 187 | + |
| 188 | + defaultVectorAnalytics.updateSuperProperties(updatedProperties) |
| 189 | + |
| 190 | + val fakeEvent = aVectorAnalyticsEvent("THE_NAME", mutableMapOf("foo" to "bar")) |
| 191 | + defaultVectorAnalytics.capture(fakeEvent) |
| 192 | + |
| 193 | + fakePostHog.verifyEventTracked( |
| 194 | + "THE_NAME", |
| 195 | + fakeEvent.getProperties().clearNulls()?.toMutableMap()?.apply { |
| 196 | + updatedProperties.getProperties()?.let { putAll(it) } |
| 197 | + } |
| 198 | + ) |
| 199 | + |
| 200 | + // Check with a screen event |
| 201 | + val fakeScreen = aVectorAnalyticsScreen("Screen", mutableMapOf("foo" to "bar")) |
| 202 | + defaultVectorAnalytics.screen(fakeScreen) |
| 203 | + |
| 204 | + fakePostHog.verifyScreenTracked( |
| 205 | + "Screen", |
| 206 | + fakeScreen.getProperties().clearNulls()?.toMutableMap()?.apply { |
| 207 | + updatedProperties.getProperties()?.let { putAll(it) } |
| 208 | + } |
| 209 | + ) |
| 210 | + } |
| 211 | + |
| 212 | + @Test |
| 213 | + fun `Super properties can be updated`() = runTest { |
| 214 | + fakeAnalyticsStore.givenUserContent(consent = true) |
| 215 | + |
| 216 | + val superProperties = SuperProperties( |
| 217 | + platformCodeName = SuperProperties.PlatformCodeName.EA, |
| 218 | + cryptoSDKVersion = "0.0", |
| 219 | + cryptoSDK = SuperProperties.CryptoSDK.Rust |
| 220 | + ) |
| 221 | + |
| 222 | + defaultVectorAnalytics.updateSuperProperties(superProperties) |
| 223 | + |
| 224 | + val fakeEvent = aVectorAnalyticsEvent("THE_NAME", mutableMapOf("foo" to "bar")) |
| 225 | + defaultVectorAnalytics.capture(fakeEvent) |
| 226 | + |
| 227 | + fakePostHog.verifyEventTracked( |
| 228 | + "THE_NAME", |
| 229 | + fakeEvent.getProperties().clearNulls()?.toMutableMap()?.apply { |
| 230 | + superProperties.getProperties()?.let { putAll(it) } |
| 231 | + } |
| 232 | + ) |
| 233 | + |
| 234 | + val superPropertiesUpdate = superProperties.copy(cryptoSDKVersion = "1.0") |
| 235 | + defaultVectorAnalytics.updateSuperProperties(superPropertiesUpdate) |
| 236 | + |
| 237 | + defaultVectorAnalytics.capture(fakeEvent) |
| 238 | + |
| 239 | + fakePostHog.verifyEventTracked( |
| 240 | + "THE_NAME", |
| 241 | + fakeEvent.getProperties().clearNulls()?.toMutableMap()?.apply { |
| 242 | + superPropertiesUpdate.getProperties()?.let { putAll(it) } |
| 243 | + } |
| 244 | + ) |
| 245 | + } |
| 246 | + |
| 247 | + @Test |
| 248 | + fun `Super properties should not override event property`() = runTest { |
| 249 | + fakeAnalyticsStore.givenUserContent(consent = true) |
| 250 | + |
| 251 | + val superProperties = SuperProperties( |
| 252 | + cryptoSDKVersion = "0.0", |
| 253 | + ) |
| 254 | + |
| 255 | + defaultVectorAnalytics.updateSuperProperties(superProperties) |
| 256 | + |
| 257 | + val fakeEvent = aVectorAnalyticsEvent("THE_NAME", mutableMapOf("cryptoSDKVersion" to "XXX")) |
| 258 | + defaultVectorAnalytics.capture(fakeEvent) |
| 259 | + |
| 260 | + fakePostHog.verifyEventTracked( |
| 261 | + "THE_NAME", |
| 262 | + mapOf( |
| 263 | + "cryptoSDKVersion" to "XXX" |
| 264 | + ) |
| 265 | + ) |
| 266 | + } |
| 267 | + |
| 268 | + @Test |
| 269 | + fun `Super properties should be added to event with no properties`() = runTest { |
| 270 | + fakeAnalyticsStore.givenUserContent(consent = true) |
| 271 | + |
| 272 | + val superProperties = SuperProperties( |
| 273 | + cryptoSDKVersion = "0.0", |
| 274 | + ) |
| 275 | + |
| 276 | + defaultVectorAnalytics.updateSuperProperties(superProperties) |
| 277 | + |
| 278 | + val fakeEvent = aVectorAnalyticsEvent("THE_NAME", null) |
| 279 | + defaultVectorAnalytics.capture(fakeEvent) |
| 280 | + |
| 281 | + fakePostHog.verifyEventTracked( |
| 282 | + "THE_NAME", |
| 283 | + mapOf( |
| 284 | + "cryptoSDKVersion" to "0.0" |
| 285 | + ) |
| 286 | + ) |
| 287 | + } |
| 288 | + |
177 | 289 | private fun Map<String, Any?>?.clearNulls(): Map<String, Any>? {
|
178 | 290 | if (this == null) return null
|
179 | 291 |
|
|
0 commit comments