mirror of
https://github.com/Novaloop-AG/element-x-ios.git
synced 2026-11-03 06:31:27 +00:00
Support for posthog super properties (#2774)
* Support for posthog super properties * Use typed SuperProperties and add tests * update superProperty in AppCoordinator
This commit is contained in:
@@ -16,12 +16,14 @@
|
||||
|
||||
import AnalyticsEvents
|
||||
@testable import ElementX
|
||||
import PostHog
|
||||
import XCTest
|
||||
|
||||
class AnalyticsTests: XCTestCase {
|
||||
private var appSettings: AppSettings!
|
||||
private var analyticsClient: AnalyticsClientMock!
|
||||
private var bugReportService: BugReportServiceMock!
|
||||
private var posthogMock: PHGPostHogMock!
|
||||
|
||||
override func setUp() {
|
||||
AppSettings.resetAllSettings()
|
||||
@@ -35,6 +37,9 @@ class AnalyticsTests: XCTestCase {
|
||||
ServiceLocator.shared.register(analytics: AnalyticsService(client: analyticsClient,
|
||||
appSettings: appSettings,
|
||||
bugReportService: ServiceLocator.shared.bugReportService))
|
||||
|
||||
posthogMock = PHGPostHogMock()
|
||||
posthogMock.configureMockBehavior()
|
||||
}
|
||||
|
||||
override func tearDown() {
|
||||
@@ -80,7 +85,7 @@ class AnalyticsTests: XCTestCase {
|
||||
XCTAssertFalse(analyticsClient.startAnalyticsConfigurationCalled)
|
||||
XCTAssertFalse(bugReportService.startCalled)
|
||||
}
|
||||
|
||||
|
||||
func testAnalyticsOptOut() {
|
||||
// Given a fresh install of the app (without PostHog analytics having been set).
|
||||
// When analytics is opt-out
|
||||
@@ -95,7 +100,7 @@ class AnalyticsTests: XCTestCase {
|
||||
XCTAssertTrue(analyticsClient.stopCalled)
|
||||
XCTAssertTrue(bugReportService.stopCalled)
|
||||
}
|
||||
|
||||
|
||||
func testAnalyticsOptIn() {
|
||||
// Given a fresh install of the app (without PostHog analytics having been set).
|
||||
// When analytics is opt-in
|
||||
@@ -107,7 +112,7 @@ class AnalyticsTests: XCTestCase {
|
||||
XCTAssertTrue(analyticsClient.startAnalyticsConfigurationCalled)
|
||||
XCTAssertTrue(bugReportService.startCalled)
|
||||
}
|
||||
|
||||
|
||||
func testAnalyticsStartIfNotEnabled() {
|
||||
// Given an existing install of the app where the user previously declined the tracking
|
||||
appSettings.analyticsConsentState = .optedOut
|
||||
@@ -192,7 +197,7 @@ class AnalyticsTests: XCTestCase {
|
||||
// Given an existing install of the app where the user previously accpeted the tracking
|
||||
appSettings.analyticsConsentState = .optedIn
|
||||
XCTAssertFalse(ServiceLocator.shared.analytics.shouldShowAnalyticsPrompt)
|
||||
|
||||
|
||||
// When forgetting analytics consents
|
||||
ServiceLocator.shared.analytics.resetConsentState()
|
||||
|
||||
@@ -200,4 +205,64 @@ class AnalyticsTests: XCTestCase {
|
||||
XCTAssertEqual(appSettings.analyticsConsentState, .unknown)
|
||||
XCTAssertTrue(ServiceLocator.shared.analytics.shouldShowAnalyticsPrompt)
|
||||
}
|
||||
|
||||
func testSendingAndUpdatingSuperProperties() {
|
||||
// Given a client with user properties set
|
||||
let client = PostHogAnalyticsClient(posthogFactory: MockPostHogFactory(mock: posthogMock))
|
||||
client.start(analyticsConfiguration: appSettings.analyticsConfiguration)
|
||||
|
||||
client.updateSuperProperties(
|
||||
AnalyticsEvent.SuperProperties(appPlatform: "A thing",
|
||||
cryptoSDK: .Rust,
|
||||
cryptoSDKVersion: "000")
|
||||
)
|
||||
|
||||
// When sending an event (tests run under Debug configuration so this is sent to the development instance)
|
||||
client.screen(AnalyticsEvent.MobileScreen(durationMs: nil, screenName: .Home))
|
||||
|
||||
let screenEvent = posthogMock.screenPropertiesReceivedArguments
|
||||
|
||||
XCTAssertEqual(screenEvent?.screenTitle, AnalyticsEvent.MobileScreen.ScreenName.Home.rawValue)
|
||||
|
||||
// All the super properties should have been added
|
||||
XCTAssertEqual(screenEvent?.properties?["cryptoSDK"] as? String, AnalyticsEvent.SuperProperties.CryptoSDK.Rust.rawValue)
|
||||
XCTAssertEqual(screenEvent?.properties?["appPlatform"] as? String, "A thing")
|
||||
XCTAssertEqual(screenEvent?.properties?["cryptoSDKVersion"] as? String, "000")
|
||||
|
||||
// It should be the same for any event
|
||||
let someEvent = AnalyticsEvent.Error(context: nil,
|
||||
cryptoModule: .Rust,
|
||||
cryptoSDK: .Rust,
|
||||
domain: .E2EE,
|
||||
eventLocalAgeMillis: nil,
|
||||
isFederated: nil,
|
||||
isMatrixDotOrg: nil,
|
||||
name: .OlmKeysNotSentError,
|
||||
timeToDecryptMillis: nil,
|
||||
userTrustsOwnIdentity: nil,
|
||||
wasVisibleToUser: nil)
|
||||
client.capture(someEvent)
|
||||
|
||||
let capturedEvent = posthogMock.capturePropertiesReceivedArguments
|
||||
|
||||
// All the super properties should have been added
|
||||
XCTAssertEqual(capturedEvent?.properties?["cryptoSDK"] as? String, AnalyticsEvent.SuperProperties.CryptoSDK.Rust.rawValue)
|
||||
XCTAssertEqual(capturedEvent?.properties?["appPlatform"] as? String, "A thing")
|
||||
XCTAssertEqual(capturedEvent?.properties?["cryptoSDKVersion"] as? String, "000")
|
||||
|
||||
// Updating should keep the previously set properties
|
||||
client.updateSuperProperties(
|
||||
AnalyticsEvent.SuperProperties(appPlatform: nil,
|
||||
cryptoSDK: nil,
|
||||
cryptoSDKVersion: "001")
|
||||
)
|
||||
|
||||
client.capture(someEvent)
|
||||
let capturedEvent2 = posthogMock.capturePropertiesReceivedArguments
|
||||
|
||||
// All the super properties should have been added, with the one udpated
|
||||
XCTAssertEqual(capturedEvent2?.properties?["cryptoSDK"] as? String, AnalyticsEvent.SuperProperties.CryptoSDK.Rust.rawValue)
|
||||
XCTAssertEqual(capturedEvent2?.properties?["appPlatform"] as? String, "A thing")
|
||||
XCTAssertEqual(capturedEvent2?.properties?["cryptoSDKVersion"] as? String, "001")
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user