Files
bundesmessenger-ios/bwi/MatomoAnalytics/BWIAnalytics.swift
2023-02-08 14:53:45 +00:00

190 lines
6.3 KiB
Swift

//
/*
* Copyright (c) 2021 BWI GmbH
*
* 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 Foundation
import MatomoTracker
@objcMembers class BWIAnalytics : NSObject {
static let sharedTracker = BWIAnalytics()
public var running: Bool {
get {
guard let session = session else {
return false
}
return BWIAnalyticsAccountDataService(mxSession: session).isEnabled()
}
set(enabled) {
guard let session = session else {
return
}
BWIAnalyticsAccountDataService(mxSession: session).setEnabled(enabled)
}
}
private let matomo: MatomoTracker?
private let appName: String
private let appVersion: String
private let appPlattform = "iOS"
var session: MXSession? = nil
private override init() {
if !BWIBuildSettings.shared.secondaryAppName.isEmpty {
appName = BWIBuildSettings.shared.secondaryAppName
} else {
appName = Bundle.main.object(forInfoDictionaryKey: "CFBundleDisplayName") as! String
}
appVersion = AppDelegate.theDelegate().appVersion
if BWIBuildSettings.shared.bwiEnableErrorTracking {
guard let url = URL(string: AppConfigService.shared.analyticsUrl()) else {
matomo = nil
return
}
matomo = MatomoTracker.init(siteId: BWIBuildSettings.shared.bwiAnalyticsAppId, baseURL:url, userAgent: nil)
matomo?.isOptedOut = false
matomo?.logger = DefaultLogger(minLevel: .verbose)
matomo?.setCustomVariable(withIndex: 1, name: "App Platform", value: appPlattform)
matomo?.setCustomVariable(withIndex: 2, name: "App Version", value: appVersion)
} else {
matomo = nil
}
}
func isEnabled() -> Bool {
return matomo != nil
}
func needsToShowPromt() -> Bool {
if !BWIBuildSettings.shared.bwiEnableErrorTracking {
return false
}
guard let session = session else {
return false
}
return !BWIAccountNotificationService(mxSession: session).isNotificationShown(BWIAccountNotificationService.AccountNotifications.CodingKeys.analyticsPromt.rawValue)
}
func setPromtShown(_ shown: Bool) {
guard let session = session else {
return
}
BWIAccountNotificationService(mxSession: session).setNotification(BWIAccountNotificationService.AccountNotifications.CodingKeys.analyticsPromt.rawValue, shown: shown)
}
func trackScreen(_ screenName: String) {
if running {
matomo?.track(view: ["ios", appName, appVersion, screenName], url: nil)
}
}
func trackEvent(_ category: String, action: String) {
if running {
matomo?.track(eventWithCategory: category, action: action, url:nil)
}
}
func trackBwiDuration(_ duration: TimeInterval, _ category: String, _ name: String) {
if running {
matomo?.track(eventWithCategory: "Metrics", action: category, name: name, number: NSNumber(value: duration), url:nil)
}
}
func trackBwiValue(_ value: NSNumber, _ category: String, _ action: String) {
if running {
matomo?.track(eventWithCategory: category, action: action, name: nil, number: value, url:nil)
}
}
func trackBwiValue(_ value: NSNumber, _ category: String, _ action: String, _ name: String) {
if running {
matomo?.track(eventWithCategory: category, action: action, name: name, number: value, url:nil)
}
}
}
extension BWIAnalytics : MXAnalyticsDelegate {
func trackJoinedRoom(asDM isDM: Bool, isSpace: Bool, memberCount: UInt) {
// dont track NV specific logs
}
func trackValue(_ value: NSNumber, category: String, name: String) {
// dont track NV specific logs
}
func trackDuration(_ milliseconds: Int, name: MXTaskProfileName, units: UInt) {
// dont track NV specific logs
}
func startDurationTracking(forName name: String, operation: String) -> StopDurationTracking {
return {}
}
func trackCreatedRoom(asDM isDM: Bool) {
// dont track NV specific logs
}
func trackCallStarted(withVideo isVideo: Bool, numberOfParticipants: Int, incoming isIncoming: Bool) {
// dont track NV specific logs
}
func trackCallEnded(withDuration duration: Int, video isVideo: Bool, numberOfParticipants: Int, incoming isIncoming: Bool) {
// dont track NV specific logs
}
func trackCallError(with reason: __MXCallHangupReason, video isVideo: Bool, numberOfParticipants: Int, incoming isIncoming: Bool) {
// dont track NV specific logs
}
func trackJoinedRoom(asDM isDM: Bool, memberCount: UInt) {
// dont track NV specific logs
}
func trackContactsAccessGranted(_ granted: Bool) {
// dont track NV specific logs
}
func trackBwiSDKValue(_ value: NSNumber, category: String, action: String) {
self.trackBwiValue(value, category, action)
}
func trackBwiSDKValue(_ value: NSNumber, category: String, action: String, name: String) {
self.trackBwiValue(value, category, action, name);
}
func trackBwiEvent(_ category: String, action: String) {
self.trackEvent(category, action: action)
}
func trackComposerEvent(inThread: Bool, isEditing: Bool, isReply: Bool, startsThread: Bool) {
// dont track NV specific logs
}
func trackNonFatalIssue(_ issue: String, details: [String : Any]?) {
// dont track NV specific logs
}
}