element-ios/Riot/Modules/Analytics/PostHogAnalyticsClient.swift

99 lines
3.6 KiB
Swift
Raw Normal View History

//
// 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 {
/// The PHGPostHog object used to report events.
2024-05-21 14:02:46 +00:00
private var postHog: PostHogSDK?
/// Any user properties to be included with the next captured event.
private(set) var pendingUserProperties: AnalyticsEvent.UserProperties?
2023-02-14 17:03:25 +00:00
static let shared = PostHogAnalyticsClient()
2024-05-27 14:06:25 +00:00
var isRunning: Bool { postHog != nil && !postHog!.isOptOut() }
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 }
if postHog == nil {
2024-05-21 14:02:46 +00:00
PostHogSDK.shared.setup(configuration)
postHog = PostHogSDK.shared
}
2024-05-21 14:02:46 +00:00
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
2024-05-21 14:02:46 +00:00
postHog?.identify(id, userProperties: userProperties.properties.compactMapValues { $0 })
pendingUserProperties = nil
} else {
postHog?.identify(id)
}
}
func reset() {
postHog?.reset()
pendingUserProperties = nil
}
func stop() {
2024-05-21 14:02:46 +00:00
postHog?.optOut()
2024-05-21 14:02:46 +00:00
self.postHog = nil
}
func flush() {
postHog?.flush()
}
func capture(_ event: AnalyticsEventProtocol) {
2024-05-21 14:02:46 +00:00
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) {
2024-05-21 14:02:46 +00:00
postHog?.screen(event.screenName.rawValue, properties: event.properties)
}
func updateUserProperties(_ userProperties: AnalyticsEvent.UserProperties) {
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,
2023-03-02 14:18:04 +00:00
numSpaces: userProperties.numSpaces ?? pendingUserProperties.numSpaces)
}
}
2023-02-14 17:03:25 +00:00
extension PostHogAnalyticsClient: RemoteFeaturesClientProtocol {
func isFeatureEnabled(_ feature: String) -> Bool {
postHog?.isFeatureEnabled(feature) == true
}
}