Add analytics for Unauthenticated errors

This commit is contained in:
David Langley 2022-01-13 17:33:51 +00:00
parent 38fae3c7cf
commit 06a8d0c0c9
12 changed files with 55 additions and 14 deletions

View file

@ -83,7 +83,7 @@ import AnalyticsEvents
// Catch and log crashes // Catch and log crashes
MXLogger.logCrashes(true) MXLogger.logCrashes(true)
MXLogger.setBuildVersion(AppDelegate.theDelegate().build) MXLogger.setBuildVersion(AppInfo.current.buildInfo.readableBuildVersion)
} }
/// Use the analytics settings from the supplied session to configure analytics. /// Use the analytics settings from the supplied session to configure analytics.
@ -200,6 +200,16 @@ extension Analytics {
} }
} }
/// Track when a user becomes unauthenticated without pressing the `sign out` button.
/// - Parameters:
/// - reason: The error that occurred.
/// - count: The number of times that error occurred.
func trackAuthUnauthenticatedError(softLogout: Bool, refreshTokenAuth: Bool, errorCode: String, errorReason: String) {
let errorCode = AnalyticsEvent.UnauthenticatedError.ErrorCode(rawValue: errorCode) ?? .M_UNKNOWN
let event = AnalyticsEvent.UnauthenticatedError(errorCode: errorCode, errorReason: errorReason, refreshTokenAuth: refreshTokenAuth, softLogout: softLogout)
client.capture(event)
}
/// Track whether the user accepted or declined the terms to an identity server. /// Track whether the user accepted or declined the terms to an identity server.
/// **Note** This method isn't currently implemented. /// **Note** This method isn't currently implemented.
/// - Parameter accepted: Whether the terms were accepted. /// - Parameter accepted: Whether the terms were accepted.

View file

@ -362,5 +362,5 @@ typedef BOOL (^MXKAccountOnCertificateChange)(MXKAccount *mxAccount, NSData *cer
/** /**
Handle unauthenticated errors from the server triggering hard/soft logouts as appropriate. Handle unauthenticated errors from the server triggering hard/soft logouts as appropriate.
*/ */
- (void)handleUnauthenticated:(MXError *)error andCompletion:(void (^)(void))completion; - (void)handleUnauthenticatedWithError:(MXError *)error isSoftLogout:(BOOL)isSoftLogout isRefreshTokenAuth:(BOOL)isRefreshTokenAuth andCompletion:(void (^)(void))completion;
@end @end

View file

@ -36,6 +36,8 @@
#import "MXKSwiftHeader.h" #import "MXKSwiftHeader.h"
#import "GeneratedInterface-Swift.h"
NSString *const kMXKAccountUserInfoDidChangeNotification = @"kMXKAccountUserInfoDidChangeNotification"; NSString *const kMXKAccountUserInfoDidChangeNotification = @"kMXKAccountUserInfoDidChangeNotification";
NSString *const kMXKAccountAPNSActivityDidChangeNotification = @"kMXKAccountAPNSActivityDidChangeNotification"; NSString *const kMXKAccountAPNSActivityDidChangeNotification = @"kMXKAccountAPNSActivityDidChangeNotification";
NSString *const kMXKAccountPushKitActivityDidChangeNotification = @"kMXKAccountPushKitActivityDidChangeNotification"; NSString *const kMXKAccountPushKitActivityDidChangeNotification = @"kMXKAccountPushKitActivityDidChangeNotification";
@ -1700,17 +1702,18 @@ static NSArray<NSNumber*> *initialSyncSilentErrorsHTTPStatusCodes;
} andPersistentTokenDataHandler:^(void (^handler)(NSArray<MXCredentials *> *credentials, void (^completion)(BOOL didUpdateCredentials))) { } andPersistentTokenDataHandler:^(void (^handler)(NSArray<MXCredentials *> *credentials, void (^completion)(BOOL didUpdateCredentials))) {
[MXKAccountManager.sharedManager readAndWriteCredentials:handler]; [MXKAccountManager.sharedManager readAndWriteCredentials:handler];
} andUnauthenticatedHandler:^(MXError *error, void (^completion)(void)) { } andUnauthenticatedHandler:^(MXError *error, BOOL isSoftLogout, BOOL isRefreshTokenAuth, void (^completion)(void)) {
MXStrongifyAndReturnIfNil(self); MXStrongifyAndReturnIfNil(self);
[self handleUnauthenticated:error andCompletion:completion]; [self handleUnauthenticatedWithError:error isSoftLogout:isSoftLogout isRefreshTokenAuth:isRefreshTokenAuth andCompletion:completion];
}]; }];
} }
- (void)handleUnauthenticatedWithError:(MXError *)error isSoftLogout:(BOOL)isSoftLogout isRefreshTokenAuth:(BOOL)isRefreshTokenAuth andCompletion:(void (^)(void))completion
- (void)handleUnauthenticated:(MXError *)error andCompletion:(void (^)(void))completion
{ {
if (error.httpResponse.statusCode == 401
&& [error.userInfo[kMXErrorSoftLogoutKey] isEqual:@(YES)]) [Analytics.shared trackAuthUnauthenticatedErrorWithSoftLogout:isSoftLogout refreshTokenAuth:isRefreshTokenAuth errorCode:error.errcode errorReason:error.error];
MXLogDebug(@"[MXKAccountManager] handleUnauthenticated: trackAuthUnauthenticatedErrorWithSoftLogout sent");
if (isSoftLogout)
{ {
MXLogDebug(@"[MXKAccountManager] handleUnauthenticated: soft logout."); MXLogDebug(@"[MXKAccountManager] handleUnauthenticated: soft logout.");
[[MXKAccountManager sharedManager] softLogout:self]; [[MXKAccountManager sharedManager] softLogout:self];

View file

@ -58,8 +58,8 @@ class NotificationService: UNNotificationServiceExtension {
} }
let restClient = MXRestClient(credentials: userAccount.mxCredentials, unrecognizedCertificateHandler: nil, persistentTokenDataHandler: { persistTokenDataHandler in let restClient = MXRestClient(credentials: userAccount.mxCredentials, unrecognizedCertificateHandler: nil, persistentTokenDataHandler: { persistTokenDataHandler in
MXKAccountManager.shared().readAndWriteCredentials(persistTokenDataHandler) MXKAccountManager.shared().readAndWriteCredentials(persistTokenDataHandler)
}, unauthenticatedHandler: { error, completion in }, unauthenticatedHandler: { error, softLogout, refreshTokenAuth, completion in
userAccount.handleUnauthenticated(error, andCompletion: completion) userAccount.handleUnauthenticatedWithError(error, isSoftLogout: softLogout, isRefreshTokenAuth: refreshTokenAuth, andCompletion: completion)
}) })
return restClient return restClient
}() }()
@ -97,6 +97,8 @@ class NotificationService: UNNotificationServiceExtension {
// log memory at the beginning of the process // log memory at the beginning of the process
logMemory() logMemory()
setupAnalytics()
UNUserNotificationCenter.current().removeUnwantedNotifications() UNUserNotificationCenter.current().removeUnwantedNotifications()
// check if this is a Matrix notification // check if this is a Matrix notification
@ -170,6 +172,13 @@ class NotificationService: UNNotificationServiceExtension {
} }
} }
private func setupAnalytics(){
// Configure our analytics. It will start if the option is enabled
let analytics = Analytics.shared
MXSDKOptions.sharedInstance().analyticsDelegate = analytics
analytics.startIfEnabled()
}
private func setup(withRoomId roomId: String, eventId: String, completion: @escaping () -> Void) { private func setup(withRoomId roomId: String, eventId: String, completion: @escaping () -> Void) {
MXKAccountManager.sharedManager(withReload: true) MXKAccountManager.sharedManager(withReload: true)
self.userAccount = MXKAccountManager.shared()?.activeAccounts.first self.userAccount = MXKAccountManager.shared()?.activeAccounts.first
@ -180,8 +189,8 @@ class NotificationService: UNNotificationServiceExtension {
self.logMemory() self.logMemory()
NotificationService.backgroundSyncService = MXBackgroundSyncService(withCredentials: userAccount.mxCredentials, persistTokenDataHandler: { persistTokenDataHandler in NotificationService.backgroundSyncService = MXBackgroundSyncService(withCredentials: userAccount.mxCredentials, persistTokenDataHandler: { persistTokenDataHandler in
MXKAccountManager.shared().readAndWriteCredentials(persistTokenDataHandler) MXKAccountManager.shared().readAndWriteCredentials(persistTokenDataHandler)
}, unauthenticatedHandler: { error, completion in }, unauthenticatedHandler: { error, softLogout, refreshTokenAuth, completion in
userAccount.handleUnauthenticated(error, andCompletion: completion) userAccount.handleUnauthenticatedWithError(error, isSoftLogout: softLogout, isRefreshTokenAuth: refreshTokenAuth, andCompletion: completion)
}) })
MXLog.debug("[NotificationService] setup: MXBackgroundSyncService init: AFTER") MXLog.debug("[NotificationService] setup: MXBackgroundSyncService init: AFTER")
self.logMemory() self.logMemory()

View file

@ -21,4 +21,6 @@
#import "MatrixKit-Bridging-Header.h" #import "MatrixKit-Bridging-Header.h"
#import "BuildInfo.h"
#endif /* RiotNSE_Bridging_Header_h */ #endif /* RiotNSE_Bridging_Header_h */

View file

@ -62,6 +62,8 @@ targets:
- path: ../Riot/Managers/Widgets/WidgetConstants.m - path: ../Riot/Managers/Widgets/WidgetConstants.m
- path: ../Riot/PropertyWrappers/UserDefaultsBackedPropertyWrapper.swift - path: ../Riot/PropertyWrappers/UserDefaultsBackedPropertyWrapper.swift
- path: ../Riot/Modules/MatrixKit - path: ../Riot/Modules/MatrixKit
- path: ../Riot/Modules/Analytics
- path: ../Riot/Managers/AppInfo/
excludes: excludes:
- "**/*.md" # excludes all files with the .md extension - "**/*.md" # excludes all files with the .md extension
- path: ../Riot/Generated/MatrixKitStrings.swift - path: ../Riot/Generated/MatrixKitStrings.swift

View file

@ -81,9 +81,9 @@
MXWeakify(self); MXWeakify(self);
MXRestClient *restClient = [[MXRestClient alloc] initWithCredentials:self.userAccount.mxCredentials andOnUnrecognizedCertificateBlock:nil andPersistentTokenDataHandler:^(void (^handler)(NSArray<MXCredentials *> *credentials, void (^completion)(BOOL didUpdateCredentials))) { MXRestClient *restClient = [[MXRestClient alloc] initWithCredentials:self.userAccount.mxCredentials andOnUnrecognizedCertificateBlock:nil andPersistentTokenDataHandler:^(void (^handler)(NSArray<MXCredentials *> *credentials, void (^completion)(BOOL didUpdateCredentials))) {
[[MXKAccountManager sharedManager] readAndWriteCredentials:handler]; [[MXKAccountManager sharedManager] readAndWriteCredentials:handler];
} andUnauthenticatedHandler:^(MXError *error, void (^completion)(void)) { } andUnauthenticatedHandler:^(MXError *error, BOOL isSoftLogout, BOOL isRefreshTokenAuth, void (^completion)(void)) {
MXStrongifyAndReturnIfNil(self); MXStrongifyAndReturnIfNil(self);
[self.userAccount handleUnauthenticated:error andCompletion:completion]; [self.userAccount handleUnauthenticatedWithError:error isSoftLogout:isSoftLogout isRefreshTokenAuth:isRefreshTokenAuth andCompletion:completion];
}]; }];
MXSession *session = [[MXSession alloc] initWithMatrixRestClient:restClient]; MXSession *session = [[MXSession alloc] initWithMatrixRestClient:restClient];
[MXFileStore setPreloadOptions:0]; [MXFileStore setPreloadOptions:0];

View file

@ -52,6 +52,10 @@
[MXLog configure:configuration]; [MXLog configure:configuration];
// Configure our analytics. It will start if the option is enabled
Analytics *analytics = Analytics.shared;
[MXSDKOptions sharedInstance].analyticsDelegate = analytics;
[analytics startIfEnabled];
[ThemeService.shared setThemeId:RiotSettings.shared.userInterfaceTheme]; [ThemeService.shared setThemeId:RiotSettings.shared.userInterfaceTheme];

View file

@ -69,6 +69,7 @@ targets:
- path: ../Riot/Assets/SharedImages.xcassets - path: ../Riot/Assets/SharedImages.xcassets
buildPhase: resources buildPhase: resources
- path: ../Riot/Modules/MatrixKit - path: ../Riot/Modules/MatrixKit
- path: ../Riot/Modules/Analytics
excludes: excludes:
- "**/*.md" # excludes all files with the .md extension - "**/*.md" # excludes all files with the .md extension
- path: ../Riot/Generated/MatrixKitStrings.swift - path: ../Riot/Generated/MatrixKitStrings.swift

View file

@ -53,6 +53,11 @@
} }
[MXLog configure:configuration]; [MXLog configure:configuration];
// Configure our analytics. It will start if the option is enabled
Analytics *analytics = Analytics.shared;
[MXSDKOptions sharedInstance].analyticsDelegate = analytics;
[analytics startIfEnabled];
} }
return self; return self;
} }

View file

@ -15,3 +15,4 @@
// //
#import "MatrixKit-Bridging-Header.h" #import "MatrixKit-Bridging-Header.h"
#import "BuildInfo.h"

View file

@ -51,6 +51,10 @@ targets:
- path: ../Riot/Managers/Locale/LocaleProviderType.swift - path: ../Riot/Managers/Locale/LocaleProviderType.swift
- path: ../Riot/Managers/Locale/LocaleProvider.swift - path: ../Riot/Managers/Locale/LocaleProvider.swift
- path: ../Riot/Modules/MatrixKit - path: ../Riot/Modules/MatrixKit
- path: ../Riot/Modules/Analytics
- path: ../Riot/Managers/AppInfo/
- path: ../Riot/Managers/Locale/LocaleProviderType.swift
- path: ../Riot/Generated/Strings.swift
excludes: excludes:
- "**/*.md" # excludes all files with the .md extension - "**/*.md" # excludes all files with the .md extension
- path: ../Riot/Generated/MatrixKitStrings.swift - path: ../Riot/Generated/MatrixKitStrings.swift