From 46477d7023bc2dfc2fbbdf032965b8c05ace50b5 Mon Sep 17 00:00:00 2001 From: Frank Rotermund Date: Wed, 12 Apr 2023 16:13:25 +0200 Subject: [PATCH] MESSENGER-4535 matomo e2ee error tracking --- .../Modules/Analytics/DecryptionFailure.swift | 5 +- .../Analytics/DecryptionFailureTracker.m | 13 ++- Riot/Modules/Room/RoomViewController.m | 1 - RiotNSE/target.yml | 4 + bwi/MatomoAnalytics/BWIAnalytics.swift | 23 ++++- bwi/MatomoAnalytics/E2EEError.swift | 99 +++++++++++++++++++ 6 files changed, 135 insertions(+), 10 deletions(-) create mode 100644 bwi/MatomoAnalytics/E2EEError.swift diff --git a/Riot/Modules/Analytics/DecryptionFailure.swift b/Riot/Modules/Analytics/DecryptionFailure.swift index 1c991db88..bbb26fc4f 100644 --- a/Riot/Modules/Analytics/DecryptionFailure.swift +++ b/Riot/Modules/Analytics/DecryptionFailure.swift @@ -43,10 +43,13 @@ import AnalyticsEvents let reason: DecryptionFailureReason /// Additional context of failure let context: String + // bwi: tracking information + let errorCode: NSInteger - init(failedEventId: String, reason: DecryptionFailureReason, context: String) { + init(failedEventId: String, reason: DecryptionFailureReason, context: String, errorCode: NSInteger) { self.failedEventId = failedEventId self.reason = reason self.context = context + self.errorCode = errorCode } } diff --git a/Riot/Modules/Analytics/DecryptionFailureTracker.m b/Riot/Modules/Analytics/DecryptionFailureTracker.m index 295bffa6d..7161b02ef 100644 --- a/Riot/Modules/Analytics/DecryptionFailureTracker.m +++ b/Riot/Modules/Analytics/DecryptionFailureTracker.m @@ -116,7 +116,8 @@ NSString *const kDecryptionFailureTrackerAnalyticsCategory = @"e2e.failure"; NSString *context = [NSString stringWithFormat:@"code: %ld, description: %@", event.decryptionError.code, event.decryptionError.localizedDescription]; reportedFailures[event.eventId] = [[DecryptionFailure alloc] initWithFailedEventId:failedEventId reason:reason - context:context]; + context:context + errorCode:event.decryptionError.code]; } - (void)dispatch @@ -157,16 +158,18 @@ NSString *const kDecryptionFailureTrackerAnalyticsCategory = @"e2e.failure"; NSMutableDictionary *failuresCounts = [NSMutableDictionary dictionary]; for (DecryptionFailure *failure in failuresToTrack) { - failuresCounts[@(failure.reason)] = @(failuresCounts[@(failure.reason)].unsignedIntegerValue + 1); - //[self.delegate trackE2EEError:failure.reason context:failure.context]; + if ( failure.reason == DecryptionFailureReasonUnspecified) { + [BWIAnalytics.sharedTracker trackE2EEError:failure.errorCode context:failure.context]; + } else { + failuresCounts[@(failure.reason)] = @(failuresCounts[@(failure.reason)].unsignedIntegerValue + 1); + } } MXLogDebug(@"[DecryptionFailureTracker] trackFailures: %@", failuresCounts); for (NSNumber *reason in failuresCounts) { - // FRANK181 - //[self.delegate trackE2EEError:reason.integerValue count:failuresCounts[reason].integerValue]; + [BWIAnalytics.sharedTracker trackE2EEErrors:reason.integerValue count:failuresCounts[reason].integerValue]; } } } diff --git a/Riot/Modules/Room/RoomViewController.m b/Riot/Modules/Room/RoomViewController.m index 4454b4094..8ac4bad5b 100644 --- a/Riot/Modules/Room/RoomViewController.m +++ b/Riot/Modules/Room/RoomViewController.m @@ -8140,7 +8140,6 @@ static CGSize kThreadListBarButtonItemImageSize; }]; } - [BWIAnalytics.sharedTracker trackEvent:@"Session" action:@"No DIMENSION"]; } #pragma mark - BWI Emoji History diff --git a/RiotNSE/target.yml b/RiotNSE/target.yml index f902bac7d..4e54e2dd0 100644 --- a/RiotNSE/target.yml +++ b/RiotNSE/target.yml @@ -50,6 +50,8 @@ targets: - path: ../bwi/NotificationTimes/NotificationTimesRoomSettings.swift - path: ../bwi/NotificationTimes/NotificationTimesWeekday.swift - path: ../bwi/NotificationTimes/Date+fromHourMinute.swift + - path: ../bwi/MatomoAnalytics/BWIAnalytics.swift + - path: ../bwi/MatomoAnalytics/BWIAnalyticsAccountDataService.swift - path: ../Riot/Managers/Settings/RiotSettings.swift - path: ../Config/BuildSettings.swift - path: ../Config/BWIBuildSettings.swift @@ -65,6 +67,8 @@ targets: - path: ../Riot/Managers/Locale/LocaleProvider.swift - path: ../Riot/Managers/Locale/LocaleProviderType.swift - path: ../Riot/Managers/EncryptionKeyManager/EncryptionKeyManager.swift + - path: ../Riot/Managers/Serialization/SerializationService.swift + - path: ../Riot/Managers/Serialization/SerializationServiceType.swift - path: ../Riot/Categories/Bundle.swift - path: ../Riot/Categories/MXEvent.swift - path: ../Riot/Generated/Strings.swift diff --git a/bwi/MatomoAnalytics/BWIAnalytics.swift b/bwi/MatomoAnalytics/BWIAnalytics.swift index 71d52476f..4b64ca0c6 100644 --- a/bwi/MatomoAnalytics/BWIAnalytics.swift +++ b/bwi/MatomoAnalytics/BWIAnalytics.swift @@ -53,7 +53,7 @@ import MatomoTracker } else { appName = Bundle.main.object(forInfoDictionaryKey: "CFBundleDisplayName") as! String } - appVersion = AppDelegate.theDelegate().appVersion + appVersion = AppInfo.current.appVersion?.bundleShortVersion ?? "" if BWIBuildSettings.shared.bwiMatomoEnabled { // bwi: Analytics use custom config @@ -158,9 +158,9 @@ import MatomoTracker func trackSlowMessage( dimension: String, value: Int) { if fastRunning { // bwi: Analytics use custom config - matomo?.setDimension(dimension, forIndex: 1) + matomo?.setDimension(dimension, forIndex: 2) matomo?.track(eventWithCategory: "Performance", action: "SendMessage", name: nil, number: NSNumber(value: value), url:nil) - matomo?.remove(dimensionAtIndex: 1) + matomo?.remove(dimensionAtIndex: 2) } } } @@ -224,5 +224,22 @@ extension BWIAnalytics : MXAnalyticsDelegate { func trackNonFatalIssue(_ issue: String, details: [String : Any]?) { // dont track NV specific logs } + + func trackE2EEError(_ reason: DecryptionFailureReason, context: String) { + if let errorCode = AnalyticsE2EEErrorCode(rawValue: reason.rawValue) { + self.trackBwiValue(NSNumber(value: 1), "Encryption", "SendMessage", errorCode.description) + } else { + self.trackBwiValue(NSNumber(value: 1), "Encryption", "SendMessage", "Unknown_Error") + } + + } + + func trackE2EEErrors(_ reason: DecryptionFailureReason, count: Int) { + if let errorCode = AnalyticsE2EEErrorCode(rawValue: reason.rawValue) { + self.trackBwiValue(NSNumber(value: count), "Encryption", "SendMessage", errorCode.description) + } else { + self.trackBwiValue(NSNumber(value: count), "Encryption", "SendMessage", "Unknown_Error") + } + } } diff --git a/bwi/MatomoAnalytics/E2EEError.swift b/bwi/MatomoAnalytics/E2EEError.swift new file mode 100644 index 000000000..1c0a85af6 --- /dev/null +++ b/bwi/MatomoAnalytics/E2EEError.swift @@ -0,0 +1,99 @@ +// +/* + * Copyright (c) 2022 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 + +/* + typedef enum : NSUInteger + { + encryptionNotEnabledCode = 0, + unableToEncryptCode, + unableToDecryptCode, + olmCode, + unknownInboundSessionIdCode, + inboundSessionMismatchRoomIdCode, + missingFieldsCode, + missingCiphertextCode, + notIncludedInRecipientsCode, + badRecipientCode, + badRecipientKeyCode, + forwardedMessageCode, + badRoomCode, + badEncryptedMessageCode, + duplicateMessageIndexCode, + missingPropertyCode, + } Code; + */ + +enum AnalyticsE2EEErrorCode: Int { + case encryptionNotEnabledCode = 0 + case unableToEncryptCode = 1 + case unableToDecryptCode = 2 + case olmCode = 3 + case unknownInboundSessionIdCode = 4 + case inboundSessionMismatchRoomIdCode = 5 + case missingFieldsCode = 6 + case missingCiphertextCode = 7 + case notIncludedInRecipientsCode = 8 + case badRecipientCode = 9 + case badRecipientKeyCode = 10 + case forwardedMessageCode = 11 + case badRoomCode = 12 + case badEncryptedMessageCode = 13 + case duplicateMessageIndexCode = 14 + case missingPropertyCode = 15 + // Add more error codes as needed + + var description: String { + switch self { + case .encryptionNotEnabledCode: + return "Encryption_Not_Enabled" + case .unableToEncryptCode: + return "Unable_To_Encrypt" + case .unableToDecryptCode: + return "Unable_To_Decrypt" + case .olmCode: + return "Olm_Unknown_Message_Index" + case .unknownInboundSessionIdCode: + return "Unknown_Inbound_SessionId" + case .inboundSessionMismatchRoomIdCode: + return "Inbound_Session_Mismatch_RoomId" + case .missingFieldsCode: + return "Missing_Fields" + case .missingCiphertextCode: + return "Missing_Ciphertext" + case .notIncludedInRecipientsCode: + return "Not_Included_In_Recipients" + case .badRecipientCode: + return "Bad_Recipient" + case .badRecipientKeyCode: + return "Bad_Recipient_Key" + case .forwardedMessageCode: + return "Forwarded_Message" + case .badRoomCode: + return "Bad_Room" + case .badEncryptedMessageCode: + return "Bad_Encrypted_Message" + case .duplicateMessageIndexCode: + return "Duplicate_Message_Index" + case .missingPropertyCode: + return "Missing_Property" + } + } + + // You can also define additional methods or properties as needed +}