2021-12-02 16:56:04 +00:00
|
|
|
//
|
|
|
|
// Copyright 2021 New Vector Ltd
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
//
|
|
|
|
|
|
|
|
import PostHog
|
|
|
|
import AnalyticsEvents
|
|
|
|
|
|
|
|
/// An analytics client that reports events to a PostHog server.
|
|
|
|
class PostHogAnalyticsClient: AnalyticsClientProtocol {
|
2024-06-01 11:05:04 +00:00
|
|
|
|
|
|
|
private var posthogFactory: PostHogFactory = DefaultPostHogFactory()
|
|
|
|
|
|
|
|
init(posthogFactory: PostHogFactory? = nil) {
|
|
|
|
if let factory = posthogFactory {
|
|
|
|
self.posthogFactory = factory
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-02 16:56:04 +00:00
|
|
|
/// The PHGPostHog object used to report events.
|
2024-06-01 11:05:04 +00:00
|
|
|
private var postHog: PostHogProtocol?
|
2021-12-02 16:56:04 +00:00
|
|
|
|
2022-02-17 12:39:11 +00:00
|
|
|
/// Any user properties to be included with the next captured event.
|
|
|
|
private(set) var pendingUserProperties: AnalyticsEvent.UserProperties?
|
|
|
|
|
2024-06-01 11:05:04 +00:00
|
|
|
/// Super Properties are properties associated with events that are set once and then sent with every capture call, be it a $screen, an autocaptured button click, or anything else.
|
|
|
|
/// It is different from user properties that will be attached to the user and not events.
|
|
|
|
/// Not persisted for now, should be set on start.
|
|
|
|
private var superProperties: AnalyticsEvent.SuperProperties?
|
|
|
|
|
2023-02-14 17:03:25 +00:00
|
|
|
static let shared = PostHogAnalyticsClient()
|
|
|
|
|
2024-06-01 11:05:04 +00:00
|
|
|
var isRunning: Bool {
|
|
|
|
guard let postHog else { return false }
|
|
|
|
return !postHog.isOptOut()
|
|
|
|
}
|
2021-12-02 16:56:04 +00:00
|
|
|
|
|
|
|
func start() {
|
|
|
|
// Only start if analytics have been configured in BuildSettings
|
2024-05-21 14:02:46 +00:00
|
|
|
guard let configuration = PostHogConfig.standard else { return }
|
2021-12-02 16:56:04 +00:00
|
|
|
|
|
|
|
if postHog == nil {
|
2024-06-01 11:05:04 +00:00
|
|
|
postHog = posthogFactory.createPostHog(config: configuration)
|
2021-12-02 16:56:04 +00:00
|
|
|
}
|
|
|
|
|
2024-05-21 14:02:46 +00:00
|
|
|
postHog?.optIn()
|
2021-12-02 16:56:04 +00:00
|
|
|
}
|
|
|
|
|
2022-02-17 12:39:11 +00:00
|
|
|
func identify(id: String) {
|
|
|
|
if let userProperties = pendingUserProperties {
|
|
|
|
// As user properties overwrite old ones, compactMap the dictionary to avoid resetting any missing properties
|
2024-05-21 14:02:46 +00:00
|
|
|
postHog?.identify(id, userProperties: userProperties.properties.compactMapValues { $0 })
|
2022-02-17 12:39:11 +00:00
|
|
|
pendingUserProperties = nil
|
|
|
|
} else {
|
|
|
|
postHog?.identify(id)
|
|
|
|
}
|
2021-12-02 16:56:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func reset() {
|
|
|
|
postHog?.reset()
|
2022-02-17 12:39:11 +00:00
|
|
|
pendingUserProperties = nil
|
2021-12-13 17:30:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func stop() {
|
2024-05-21 14:02:46 +00:00
|
|
|
postHog?.optOut()
|
2021-12-02 16:56:04 +00:00
|
|
|
|
2024-05-21 14:02:46 +00:00
|
|
|
self.postHog = nil
|
2021-12-02 16:56:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func flush() {
|
|
|
|
postHog?.flush()
|
|
|
|
}
|
|
|
|
|
|
|
|
func capture(_ event: AnalyticsEventProtocol) {
|
2024-06-01 11:05:04 +00:00
|
|
|
postHog?.capture(event.eventName, properties: attachSuperProperties(to: event.properties), userProperties: pendingUserProperties?.properties.compactMapValues { $0 })
|
2024-05-21 14:02:46 +00:00
|
|
|
// Pending user properties have been added
|
|
|
|
self.pendingUserProperties = nil
|
2021-12-02 16:56:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func screen(_ event: AnalyticsScreenProtocol) {
|
2024-06-01 11:05:04 +00:00
|
|
|
postHog?.screen(event.screenName.rawValue, properties: attachSuperProperties(to: event.properties))
|
2021-12-02 16:56:04 +00:00
|
|
|
}
|
|
|
|
|
2022-02-15 17:54:17 +00:00
|
|
|
func updateUserProperties(_ userProperties: AnalyticsEvent.UserProperties) {
|
2022-02-17 12:39:11 +00:00
|
|
|
guard let pendingUserProperties = pendingUserProperties else {
|
|
|
|
pendingUserProperties = userProperties
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Merge the updated user properties with the existing ones
|
2023-03-02 14:18:04 +00:00
|
|
|
self.pendingUserProperties = AnalyticsEvent.UserProperties(allChatsActiveFilter: userProperties.allChatsActiveFilter ?? pendingUserProperties.allChatsActiveFilter,
|
|
|
|
ftueUseCaseSelection: userProperties.ftueUseCaseSelection ?? pendingUserProperties.ftueUseCaseSelection,
|
2022-02-28 14:45:15 +00:00
|
|
|
numFavouriteRooms: userProperties.numFavouriteRooms ?? pendingUserProperties.numFavouriteRooms,
|
2024-06-01 11:05:04 +00:00
|
|
|
numSpaces: userProperties.numSpaces ?? pendingUserProperties.numSpaces,
|
|
|
|
// Not yet supported
|
|
|
|
recoveryState: nil, verificationState: nil)
|
2022-02-17 12:39:11 +00:00
|
|
|
}
|
|
|
|
|
2024-06-01 11:05:04 +00:00
|
|
|
func updateSuperProperties(_ updatedProperties: AnalyticsEvent.SuperProperties) {
|
|
|
|
self.superProperties = AnalyticsEvent.SuperProperties(
|
|
|
|
appPlatform: updatedProperties.appPlatform ?? superProperties?.appPlatform,
|
|
|
|
cryptoSDK: updatedProperties.cryptoSDK ?? superProperties?.cryptoSDK,
|
|
|
|
cryptoSDKVersion: updatedProperties.cryptoSDKVersion ?? superProperties?.cryptoSDKVersion
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// 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] {
|
|
|
|
guard isRunning, let superProperties else { return properties }
|
|
|
|
|
|
|
|
var properties = properties
|
|
|
|
|
|
|
|
superProperties.properties.forEach { (key: String, value: Any) in
|
|
|
|
if properties[key] == nil {
|
|
|
|
properties[key] = value
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return properties
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-12-02 16:56:04 +00:00
|
|
|
}
|
2023-02-14 17:03:25 +00:00
|
|
|
|
|
|
|
extension PostHogAnalyticsClient: RemoteFeaturesClientProtocol {
|
|
|
|
func isFeatureEnabled(_ feature: String) -> Bool {
|
|
|
|
postHog?.isFeatureEnabled(feature) == true
|
|
|
|
}
|
|
|
|
}
|