mirror of
https://gitlab.opencode.de/bwi/bundesmessenger/clients/bundesmessenger-ios.git
synced 2026-04-21 17:12:45 +02:00
Include user properties with the next sent event.
This commit is contained in:
@@ -108,7 +108,6 @@ import AnalyticsEvents
|
||||
!RiotSettings.shared.isIdentifiedForAnalytics
|
||||
else { return }
|
||||
|
||||
let userProperties = makeUserProperties(for: session)
|
||||
let service = AnalyticsService(session: session)
|
||||
self.service = service
|
||||
|
||||
@@ -117,7 +116,7 @@ import AnalyticsEvents
|
||||
|
||||
switch result {
|
||||
case .success(let settings):
|
||||
self.identify(with: settings, and: userProperties)
|
||||
self.identify(with: settings)
|
||||
self.service = nil
|
||||
case .failure:
|
||||
MXLog.error("[Analytics] Failed to use analytics settings. Will continue to run without analytics ID.")
|
||||
@@ -150,30 +149,17 @@ import AnalyticsEvents
|
||||
|
||||
/// Identify (pseudonymously) any future events with the ID from the analytics account data settings.
|
||||
/// - Parameter settings: The settings to use for identification. The ID must be set *before* calling this method.
|
||||
/// - Parameter userProperties: Any user properties that should be set for creating cohorts etc.
|
||||
private func identify(with settings: AnalyticsSettings, and userProperties: AnalyticsEvent.UserProperties) {
|
||||
private func identify(with settings: AnalyticsSettings) {
|
||||
guard let id = settings.id else {
|
||||
MXLog.error("[Analytics] identify(with:) called before an ID has been generated.")
|
||||
return
|
||||
}
|
||||
|
||||
client.identify(id: id, userProperties: userProperties)
|
||||
client.identify(id: id)
|
||||
MXLog.debug("[Analytics] Identified.")
|
||||
RiotSettings.shared.isIdentifiedForAnalytics = true
|
||||
}
|
||||
|
||||
/// Returns the user properties for use when identifying a session.
|
||||
/// - Parameter session: The session to gather any user properties from.
|
||||
/// - Returns: The properties to be set.
|
||||
private func makeUserProperties(for session: MXSession) -> AnalyticsEvent.UserProperties {
|
||||
var useCaseSelection: AnalyticsEvent.UserProperties.FtueUseCaseSelection?
|
||||
if let userId = session.credentials.userId, let userSession = UserSessionsService.shared.userSession(withUserId: userId) {
|
||||
useCaseSelection = userSession.userProperties.useCase?.analyticsName
|
||||
}
|
||||
|
||||
return AnalyticsEvent.UserProperties(ftueUseCaseSelection: useCaseSelection, numSpaces: nil)
|
||||
}
|
||||
|
||||
/// Capture an event in the `client`.
|
||||
/// - Parameter event: The event to capture.
|
||||
private func capture(event: AnalyticsEventProtocol) {
|
||||
|
||||
@@ -26,10 +26,7 @@ protocol AnalyticsClientProtocol {
|
||||
|
||||
/// Associate the client with an ID. This is persisted until `reset` is called.
|
||||
/// - Parameter id: The ID to associate with the user.
|
||||
/// - Parameter userProperties: Any user properties that should be included.
|
||||
///
|
||||
/// Only non-nil user properties will be updated when calling this method.
|
||||
func identify(id: String, userProperties: AnalyticsEvent.UserProperties)
|
||||
func identify(id: String)
|
||||
|
||||
/// Reset all stored properties and any event queues on the client. Note that
|
||||
/// the client will remain active, but in a fresh unidentified state.
|
||||
@@ -52,6 +49,8 @@ protocol AnalyticsClientProtocol {
|
||||
/// Updates any user properties to help with creating cohorts.
|
||||
/// - Parameter userProperties: The user properties to be updated.
|
||||
///
|
||||
/// Only non-nil properties will be updated when calling this method.
|
||||
/// Only non-nil properties will be updated when calling this method. There might
|
||||
/// be a delay when updating user properties as these are cached to be included
|
||||
/// as part of the next event that gets captured.
|
||||
func updateUserProperties(_ userProperties: AnalyticsEvent.UserProperties)
|
||||
}
|
||||
|
||||
@@ -22,6 +22,9 @@ class PostHogAnalyticsClient: AnalyticsClientProtocol {
|
||||
/// The PHGPostHog object used to report events.
|
||||
private var postHog: PHGPostHog?
|
||||
|
||||
/// Any user properties to be included with the next captured event.
|
||||
private(set) var pendingUserProperties: AnalyticsEvent.UserProperties?
|
||||
|
||||
var isRunning: Bool { postHog?.enabled ?? false }
|
||||
|
||||
func start() {
|
||||
@@ -35,13 +38,19 @@ class PostHogAnalyticsClient: AnalyticsClientProtocol {
|
||||
postHog?.enable()
|
||||
}
|
||||
|
||||
func identify(id: String, userProperties: AnalyticsEvent.UserProperties) {
|
||||
// As user properties overwrite old ones, compactMap the dictionary to avoid resetting any missing properties
|
||||
postHog?.identify(id, properties: userProperties.properties.compactMapValues { $0 })
|
||||
func identify(id: String) {
|
||||
if let userProperties = pendingUserProperties {
|
||||
// As user properties overwrite old ones, compactMap the dictionary to avoid resetting any missing properties
|
||||
postHog?.identify(id, properties: userProperties.properties.compactMapValues { $0 })
|
||||
pendingUserProperties = nil
|
||||
} else {
|
||||
postHog?.identify(id)
|
||||
}
|
||||
}
|
||||
|
||||
func reset() {
|
||||
postHog?.reset()
|
||||
pendingUserProperties = nil
|
||||
}
|
||||
|
||||
func stop() {
|
||||
@@ -56,15 +65,38 @@ class PostHogAnalyticsClient: AnalyticsClientProtocol {
|
||||
}
|
||||
|
||||
func capture(_ event: AnalyticsEventProtocol) {
|
||||
postHog?.capture(event.eventName, properties: event.properties)
|
||||
postHog?.capture(event.eventName, properties: attachUserProperties(to: event.properties))
|
||||
}
|
||||
|
||||
func screen(_ event: AnalyticsScreenProtocol) {
|
||||
postHog?.screen(event.screenName.rawValue, properties: event.properties)
|
||||
postHog?.screen(event.screenName.rawValue, properties: attachUserProperties(to: event.properties))
|
||||
}
|
||||
|
||||
func updateUserProperties(_ userProperties: AnalyticsEvent.UserProperties) {
|
||||
guard let pendingUserProperties = pendingUserProperties else {
|
||||
pendingUserProperties = userProperties
|
||||
return
|
||||
}
|
||||
|
||||
// Merge the updated user properties with the existing ones
|
||||
self.pendingUserProperties = AnalyticsEvent.UserProperties(ftueUseCaseSelection: userProperties.ftueUseCaseSelection ?? pendingUserProperties.ftueUseCaseSelection,
|
||||
numSpaces: userProperties.numSpaces ?? pendingUserProperties.numSpaces)
|
||||
}
|
||||
|
||||
// MARK: - Private
|
||||
|
||||
/// Given a dictionary containing properties from an event, this method will return those properties
|
||||
/// with any pending user properties included under the `$set` key.
|
||||
/// - Parameter properties: A dictionary of properties from an event.
|
||||
/// - Returns: The `properties` dictionary with any user properties included.
|
||||
private func attachUserProperties(to properties: [String: Any]) -> [String: Any] {
|
||||
guard isRunning, let userProperties = pendingUserProperties else { return properties }
|
||||
|
||||
var properties = properties
|
||||
|
||||
// As user properties overwrite old ones via $set, compactMap the dictionary to avoid resetting any missing properties
|
||||
postHog?.capture("$identify", properties: ["$set": userProperties.properties.compactMapValues { $0 }])
|
||||
properties["$set"] = userProperties.properties.compactMapValues { $0 }
|
||||
pendingUserProperties = nil
|
||||
return properties
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user