Posthog: use correct API for user properties (#2793)

This commit is contained in:
Valere
2024-05-07 12:08:56 +02:00
committed by GitHub
parent 6ca92c272a
commit 0eb1fb257c
4 changed files with 45 additions and 41 deletions
@@ -6733,15 +6733,15 @@ class PHGPostHogMock: PHGPostHogProtocol {
}
//MARK: - capture
var capturePropertiesUnderlyingCallsCount = 0
var capturePropertiesCallsCount: Int {
var capturePropertiesUserPropertiesUnderlyingCallsCount = 0
var capturePropertiesUserPropertiesCallsCount: Int {
get {
if Thread.isMainThread {
return capturePropertiesUnderlyingCallsCount
return capturePropertiesUserPropertiesUnderlyingCallsCount
} else {
var returnValue: Int? = nil
DispatchQueue.main.sync {
returnValue = capturePropertiesUnderlyingCallsCount
returnValue = capturePropertiesUserPropertiesUnderlyingCallsCount
}
return returnValue!
@@ -6749,26 +6749,26 @@ class PHGPostHogMock: PHGPostHogProtocol {
}
set {
if Thread.isMainThread {
capturePropertiesUnderlyingCallsCount = newValue
capturePropertiesUserPropertiesUnderlyingCallsCount = newValue
} else {
DispatchQueue.main.sync {
capturePropertiesUnderlyingCallsCount = newValue
capturePropertiesUserPropertiesUnderlyingCallsCount = newValue
}
}
}
}
var capturePropertiesCalled: Bool {
return capturePropertiesCallsCount > 0
var capturePropertiesUserPropertiesCalled: Bool {
return capturePropertiesUserPropertiesCallsCount > 0
}
var capturePropertiesReceivedArguments: (event: String, properties: [String: Any]?)?
var capturePropertiesReceivedInvocations: [(event: String, properties: [String: Any]?)] = []
var capturePropertiesClosure: ((String, [String: Any]?) -> Void)?
var capturePropertiesUserPropertiesReceivedArguments: (event: String, properties: [String: Any]?, userProperties: [String: Any]?)?
var capturePropertiesUserPropertiesReceivedInvocations: [(event: String, properties: [String: Any]?, userProperties: [String: Any]?)] = []
var capturePropertiesUserPropertiesClosure: ((String, [String: Any]?, [String: Any]?) -> Void)?
func capture(_ event: String, properties: [String: Any]?) {
capturePropertiesCallsCount += 1
capturePropertiesReceivedArguments = (event: event, properties: properties)
capturePropertiesReceivedInvocations.append((event: event, properties: properties))
capturePropertiesClosure?(event, properties)
func capture(_ event: String, properties: [String: Any]?, userProperties: [String: Any]?) {
capturePropertiesUserPropertiesCallsCount += 1
capturePropertiesUserPropertiesReceivedArguments = (event: event, properties: properties, userProperties: userProperties)
capturePropertiesUserPropertiesReceivedInvocations.append((event: event, properties: properties, userProperties: userProperties))
capturePropertiesUserPropertiesClosure?(event, properties, userProperties)
}
//MARK: - screen
@@ -25,7 +25,7 @@ protocol PHGPostHogProtocol {
func reset()
func capture(_ event: String, properties: [String: Any]?)
func capture(_ event: String, properties: [String: Any]?, userProperties: [String: Any]?)
func screen(_ screenTitle: String, properties: [String: Any]?)
}
@@ -69,12 +69,13 @@ class PostHogAnalyticsClient: AnalyticsClientProtocol {
func capture(_ event: AnalyticsEventProtocol) {
guard isRunning else { return }
postHog?.capture(event.eventName, properties: attachUserProperties(to: attachSuperProperties(to: event.properties)))
postHog?.capture(event.eventName, properties: attachSuperProperties(to: event.properties), userProperties: pendingUserProperties?.properties.compactMapValues { $0 })
pendingUserProperties = nil
}
func screen(_ event: AnalyticsScreenProtocol) {
guard isRunning else { return }
postHog?.screen(event.screenName.rawValue, properties: attachUserProperties(to: attachSuperProperties(to: event.properties)))
postHog?.screen(event.screenName.rawValue, properties: attachSuperProperties(to: event.properties))
}
func updateUserProperties(_ userProperties: AnalyticsEvent.UserProperties) {
@@ -106,21 +107,6 @@ class PostHogAnalyticsClient: AnalyticsClientProtocol {
// 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
properties["$set"] = userProperties.properties.compactMapValues { $0 }
pendingUserProperties = nil
return properties
}
/// Attach super properties to events.
/// If the property is already set on the event, the already set value will be kept.
private func attachSuperProperties(to properties: [String: Any]) -> [String: Any] {