mirror of
https://gitlab.opencode.de/bwi/bundesmessenger/clients/bundesmessenger-ios.git
synced 2026-05-04 06:58:20 +02:00
Abstract PostHog out of the Analytics client.
This commit is contained in:
@@ -23,9 +23,9 @@ import AnalyticsEvents
|
||||
|
||||
static let shared = Analytics()
|
||||
|
||||
private var postHog: PHGPostHog?
|
||||
private var client = PostHogAnalyticsClient()
|
||||
|
||||
var isRunning: Bool { postHog?.enabled ?? false }
|
||||
var isRunning: Bool { client.isRunning }
|
||||
|
||||
var shouldShowAnalyticsPrompt: Bool {
|
||||
// Show an analytics prompt when the user hasn't seen the PostHog prompt before
|
||||
@@ -34,8 +34,7 @@ import AnalyticsEvents
|
||||
}
|
||||
|
||||
var promptShouldDisplayUpgradeMessage: Bool {
|
||||
// Show an analytics prompt when the user hasn't seen the PostHog prompt before
|
||||
// so long as they haven't previously declined the Matomo analytics prompt.
|
||||
// Only show an upgrade prompt if the user previously accepted Matomo analytics.
|
||||
RiotSettings.shared.hasAcceptedMatomoAnalytics
|
||||
}
|
||||
|
||||
@@ -72,11 +71,11 @@ import AnalyticsEvents
|
||||
func startIfEnabled() {
|
||||
guard RiotSettings.shared.enableAnalytics, !isRunning else { return }
|
||||
|
||||
// Ensures that analytics are configured BuildSettings
|
||||
guard let configuration = PHGPostHogConfiguration.standard else { return }
|
||||
client.start()
|
||||
|
||||
// Sanity check in case something went wrong.
|
||||
guard client.isRunning else { return }
|
||||
|
||||
postHog = PHGPostHog(configuration: configuration)
|
||||
postHog?.enable()
|
||||
MXLog.debug("[Analytics] Started.")
|
||||
|
||||
// Catch and log crashes
|
||||
@@ -90,7 +89,7 @@ import AnalyticsEvents
|
||||
return
|
||||
}
|
||||
|
||||
postHog?.identify(id)
|
||||
client.identify(id: id)
|
||||
MXLog.debug("[Analytics] Identified.")
|
||||
RiotSettings.shared.isIdentifiedForAnalytics = true
|
||||
}
|
||||
@@ -98,28 +97,25 @@ import AnalyticsEvents
|
||||
func reset() {
|
||||
guard isRunning else { return }
|
||||
|
||||
postHog?.disable()
|
||||
MXLog.debug("[Analytics] Stopped.")
|
||||
|
||||
postHog?.reset()
|
||||
client.reset()
|
||||
MXLog.debug("[Analytics] Stopped and reset.")
|
||||
RiotSettings.shared.isIdentifiedForAnalytics = false
|
||||
|
||||
postHog = nil
|
||||
|
||||
// Stop collecting crash logs
|
||||
MXLogger.logCrashes(false)
|
||||
}
|
||||
|
||||
func forceUpload() {
|
||||
postHog?.flush()
|
||||
client.flush()
|
||||
}
|
||||
|
||||
private func capture(event: AnalyticsEventProtocol) {
|
||||
postHog?.capture(event.eventName, properties: event.properties)
|
||||
client.capture(event)
|
||||
}
|
||||
|
||||
func trackScreen(_ screen: AnalyticsScreen, duration milliseconds: Int?) {
|
||||
let event = AnalyticsEvent.Screen(durationMs: milliseconds, screenName: screen.screenName)
|
||||
postHog?.screen(event.screenName.rawValue, properties: event.properties)
|
||||
client.screen(event)
|
||||
}
|
||||
|
||||
func trackE2EEError(_ reason: DecryptionFailureReason, count: Int) {
|
||||
|
||||
@@ -0,0 +1,44 @@
|
||||
//
|
||||
// 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 AnalyticsEvents
|
||||
|
||||
/// A protocol representing an analytics client.
|
||||
protocol AnalyticsClientProtocol {
|
||||
/// Whether the analytics client is currently reporting data or ignoring it.
|
||||
var isRunning: Bool { get }
|
||||
|
||||
/// Starts the analytics client reporting data.
|
||||
func start()
|
||||
|
||||
/// Associate the client with an ID. This is persisted until `reset` is called.
|
||||
/// - Parameter id: The ID to associate with the user.
|
||||
func identify(id: String)
|
||||
|
||||
/// Stop the analytics client reporting data and reset all stored properties and events.
|
||||
func reset()
|
||||
|
||||
/// Send any queued events immediately.
|
||||
func flush()
|
||||
|
||||
/// Capture the supplied analytics event.
|
||||
/// - Parameter event: The event to capture.
|
||||
func capture(_ event: AnalyticsEventProtocol)
|
||||
|
||||
/// Capture the supplied analytics screen event.
|
||||
/// - Parameter event: The screen event to capture.
|
||||
func screen(_ event: AnalyticsScreenProtocol)
|
||||
}
|
||||
@@ -0,0 +1,63 @@
|
||||
//
|
||||
// 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.
|
||||
private var postHog: PHGPostHog?
|
||||
|
||||
var isRunning: Bool { postHog?.enabled ?? false }
|
||||
|
||||
func start() {
|
||||
// Only start if analytics have been configured in BuildSettings
|
||||
guard let configuration = PHGPostHogConfiguration.standard else { return }
|
||||
|
||||
if postHog == nil {
|
||||
postHog = PHGPostHog(configuration: configuration)
|
||||
}
|
||||
|
||||
postHog?.enable()
|
||||
}
|
||||
|
||||
func identify(id: String) {
|
||||
postHog?.identify(id)
|
||||
}
|
||||
|
||||
func reset() {
|
||||
postHog?.disable()
|
||||
postHog?.reset()
|
||||
|
||||
// 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
|
||||
}
|
||||
|
||||
func flush() {
|
||||
postHog?.flush()
|
||||
}
|
||||
|
||||
func capture(_ event: AnalyticsEventProtocol) {
|
||||
postHog?.capture(event.eventName, properties: event.properties)
|
||||
}
|
||||
|
||||
func screen(_ event: AnalyticsScreenProtocol) {
|
||||
postHog?.screen(event.screenName.rawValue, properties: event.properties)
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user