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
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.
@ -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.
/// **Note** This method isn't currently implemented.
/// - 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.
*/
- (void)handleUnauthenticated:(MXError *)error andCompletion:(void (^)(void))completion;
- (void)handleUnauthenticatedWithError:(MXError *)error isSoftLogout:(BOOL)isSoftLogout isRefreshTokenAuth:(BOOL)isRefreshTokenAuth andCompletion:(void (^)(void))completion;
@end

View file

@ -36,6 +36,8 @@
#import "MXKSwiftHeader.h"
#import "GeneratedInterface-Swift.h"
NSString *const kMXKAccountUserInfoDidChangeNotification = @"kMXKAccountUserInfoDidChangeNotification";
NSString *const kMXKAccountAPNSActivityDidChangeNotification = @"kMXKAccountAPNSActivityDidChangeNotification";
NSString *const kMXKAccountPushKitActivityDidChangeNotification = @"kMXKAccountPushKitActivityDidChangeNotification";
@ -1700,17 +1702,18 @@ static NSArray<NSNumber*> *initialSyncSilentErrorsHTTPStatusCodes;
} andPersistentTokenDataHandler:^(void (^handler)(NSArray<MXCredentials *> *credentials, void (^completion)(BOOL didUpdateCredentials))) {
[MXKAccountManager.sharedManager readAndWriteCredentials:handler];
} andUnauthenticatedHandler:^(MXError *error, void (^completion)(void)) {
} andUnauthenticatedHandler:^(MXError *error, BOOL isSoftLogout, BOOL isRefreshTokenAuth, void (^completion)(void)) {
MXStrongifyAndReturnIfNil(self);
[self handleUnauthenticated:error andCompletion:completion];
[self handleUnauthenticatedWithError:error isSoftLogout:isSoftLogout isRefreshTokenAuth:isRefreshTokenAuth andCompletion:completion];
}];
}
- (void)handleUnauthenticated:(MXError *)error andCompletion:(void (^)(void))completion
- (void)handleUnauthenticatedWithError:(MXError *)error isSoftLogout:(BOOL)isSoftLogout isRefreshTokenAuth:(BOOL)isRefreshTokenAuth 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.");
[[MXKAccountManager sharedManager] softLogout:self];

View file

@ -58,8 +58,8 @@ class NotificationService: UNNotificationServiceExtension {
}
let restClient = MXRestClient(credentials: userAccount.mxCredentials, unrecognizedCertificateHandler: nil, persistentTokenDataHandler: { persistTokenDataHandler in
MXKAccountManager.shared().readAndWriteCredentials(persistTokenDataHandler)
}, unauthenticatedHandler: { error, completion in
userAccount.handleUnauthenticated(error, andCompletion: completion)
}, unauthenticatedHandler: { error, softLogout, refreshTokenAuth, completion in
userAccount.handleUnauthenticatedWithError(error, isSoftLogout: softLogout, isRefreshTokenAuth: refreshTokenAuth, andCompletion: completion)
})
return restClient
}()
@ -97,6 +97,8 @@ class NotificationService: UNNotificationServiceExtension {
// log memory at the beginning of the process
logMemory()
setupAnalytics()
UNUserNotificationCenter.current().removeUnwantedNotifications()
// 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) {
MXKAccountManager.sharedManager(withReload: true)
self.userAccount = MXKAccountManager.shared()?.activeAccounts.first
@ -180,8 +189,8 @@ class NotificationService: UNNotificationServiceExtension {
self.logMemory()
NotificationService.backgroundSyncService = MXBackgroundSyncService(withCredentials: userAccount.mxCredentials, persistTokenDataHandler: { persistTokenDataHandler in
MXKAccountManager.shared().readAndWriteCredentials(persistTokenDataHandler)
}, unauthenticatedHandler: { error, completion in
userAccount.handleUnauthenticated(error, andCompletion: completion)
}, unauthenticatedHandler: { error, softLogout, refreshTokenAuth, completion in
userAccount.handleUnauthenticatedWithError(error, isSoftLogout: softLogout, isRefreshTokenAuth: refreshTokenAuth, andCompletion: completion)
})
MXLog.debug("[NotificationService] setup: MXBackgroundSyncService init: AFTER")
self.logMemory()

View file

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

View file

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

View file

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

View file

@ -52,6 +52,10 @@
[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];

View file

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

View file

@ -53,6 +53,11 @@
}
[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;
}

View file

@ -15,3 +15,4 @@
//
#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/LocaleProvider.swift
- path: ../Riot/Modules/MatrixKit
- path: ../Riot/Modules/Analytics
- path: ../Riot/Managers/AppInfo/
- path: ../Riot/Managers/Locale/LocaleProviderType.swift
- path: ../Riot/Generated/Strings.swift
excludes:
- "**/*.md" # excludes all files with the .md extension
- path: ../Riot/Generated/MatrixKitStrings.swift