Bump posthog version to 3.25

This commit is contained in:
Valere
2024-05-21 16:02:46 +02:00
parent 36b551a317
commit b9a187c2f5
10 changed files with 41 additions and 45 deletions

View File

@@ -20,30 +20,31 @@ import AnalyticsEvents
/// An analytics client that reports events to a PostHog server.
class PostHogAnalyticsClient: AnalyticsClientProtocol {
/// The PHGPostHog object used to report events.
private var postHog: PHGPostHog?
private var postHog: PostHogSDK?
/// Any user properties to be included with the next captured event.
private(set) var pendingUserProperties: AnalyticsEvent.UserProperties?
static let shared = PostHogAnalyticsClient()
var isRunning: Bool { postHog?.enabled ?? false }
var isRunning: Bool { postHog != nil }
func start() {
// Only start if analytics have been configured in BuildSettings
guard let configuration = PHGPostHogConfiguration.standard else { return }
guard let configuration = PostHogConfig.standard else { return }
if postHog == nil {
postHog = PHGPostHog(configuration: configuration)
PostHogSDK.shared.setup(configuration)
postHog = PostHogSDK.shared
}
postHog?.enable()
postHog?.optIn()
}
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 })
postHog?.identify(id, userProperties: userProperties.properties.compactMapValues { $0 })
pendingUserProperties = nil
} else {
postHog?.identify(id)
@@ -56,10 +57,9 @@ class PostHogAnalyticsClient: AnalyticsClientProtocol {
}
func stop() {
postHog?.disable()
postHog?.optOut()
// As of PostHog 1.4.4, setting the client to nil here doesn't release
// it. Keep it around to avoid having multiple instances if the user re-enables
self.postHog = nil
}
func flush() {
@@ -67,11 +67,13 @@ class PostHogAnalyticsClient: AnalyticsClientProtocol {
}
func capture(_ event: AnalyticsEventProtocol) {
postHog?.capture(event.eventName, properties: attachUserProperties(to: event.properties))
postHog?.capture(event.eventName, properties: event.properties, userProperties: pendingUserProperties?.properties.compactMapValues { $0 })
// Pending user properties have been added
self.pendingUserProperties = nil
}
func screen(_ event: AnalyticsScreenProtocol) {
postHog?.screen(event.screenName.rawValue, properties: attachUserProperties(to: event.properties))
postHog?.screen(event.screenName.rawValue, properties: event.properties)
}
func updateUserProperties(_ userProperties: AnalyticsEvent.UserProperties) {
@@ -87,22 +89,6 @@ class PostHogAnalyticsClient: AnalyticsClientProtocol {
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
properties["$set"] = userProperties.properties.compactMapValues { $0 }
pendingUserProperties = nil
return properties
}
}
extension PostHogAnalyticsClient: RemoteFeaturesClientProtocol {