2018-06-27 07:55:06 +00:00
|
|
|
/*
|
|
|
|
Copyright 2018 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 "Analytics.h"
|
|
|
|
|
|
|
|
#import "AppDelegate.h"
|
2018-07-02 12:51:47 +00:00
|
|
|
#import "Riot-Swift.h"
|
2018-06-27 07:55:06 +00:00
|
|
|
|
2018-06-27 16:11:04 +00:00
|
|
|
// All metrics are store under a Piwik category called "Metrics".
|
|
|
|
// Then, there are 2 Piwik actions: "iOS.startup" and "iOS.stats" (these actions
|
|
|
|
// are namespaced by plaform to have a nice rendering on the Piwik website).
|
2018-06-27 15:57:40 +00:00
|
|
|
// Then, we use constants defined by the Matrix SDK as Piwik Names (ex:"mountData")
|
2018-06-29 10:04:49 +00:00
|
|
|
NSString *const kAnalyticsMetricsCategory = @"Metrics";
|
|
|
|
NSString *const kAnalyticsMetricsActionPattern = @"iOS.%@";
|
2018-06-27 15:57:40 +00:00
|
|
|
|
2018-06-29 05:35:31 +00:00
|
|
|
// E2E telemetry is stored under a Piwik category called "E2E".
|
2018-06-29 10:04:49 +00:00
|
|
|
NSString *const kAnalyticsE2eCategory = @"E2E";
|
|
|
|
NSString *const kAnalyticsE2eDecryptionFailureAction = @"Decryption failure";
|
2018-06-29 09:45:49 +00:00
|
|
|
|
2018-06-29 05:35:31 +00:00
|
|
|
|
2019-01-07 17:42:08 +00:00
|
|
|
@import MatomoTracker;
|
|
|
|
|
|
|
|
@interface MatomoTracker (MatomoTrackerMigration)
|
|
|
|
+ (MatomoTracker *)shared;
|
|
|
|
|
2019-01-07 18:24:09 +00:00
|
|
|
- (void)migrateFromFourPointFourSharedInstance;
|
2019-01-07 17:42:08 +00:00
|
|
|
@end
|
|
|
|
|
|
|
|
@implementation MatomoTracker (MatomoTrackerMigration)
|
|
|
|
+ (MatomoTracker *)shared
|
|
|
|
{
|
|
|
|
NSDictionary *piwikConfig = [[NSUserDefaults standardUserDefaults] objectForKey:@"piwik"];
|
|
|
|
MatomoTracker *matomoTracker = [[MatomoTracker alloc] initWithSiteId:piwikConfig[@"siteId"] baseURL:[NSURL URLWithString:piwikConfig[@"url"]] userAgent:@"iOSMatomoTracker"];
|
2019-01-07 18:24:09 +00:00
|
|
|
[matomoTracker migrateFromFourPointFourSharedInstance];
|
2019-01-07 17:42:08 +00:00
|
|
|
return matomoTracker;
|
|
|
|
}
|
|
|
|
|
2019-01-07 18:24:09 +00:00
|
|
|
- (void)migrateFromFourPointFourSharedInstance
|
2019-01-07 17:42:08 +00:00
|
|
|
{
|
|
|
|
if ([[NSUserDefaults standardUserDefaults] boolForKey:@"migratedFromFourPointFourSharedInstance"]) return;
|
2019-01-07 22:02:03 +00:00
|
|
|
[self copyFromOldSharedInstance];
|
2019-01-07 17:42:08 +00:00
|
|
|
[[NSUserDefaults standardUserDefaults] setBool:true forKey:@"migratedFromFourPointFourSharedInstance"];
|
|
|
|
}
|
|
|
|
@end
|
2018-06-27 07:55:06 +00:00
|
|
|
|
|
|
|
@implementation Analytics
|
|
|
|
|
|
|
|
+ (instancetype)sharedInstance
|
|
|
|
{
|
|
|
|
static Analytics *sharedInstance = nil;
|
|
|
|
static dispatch_once_t onceToken;
|
|
|
|
|
|
|
|
dispatch_once(&onceToken, ^{
|
|
|
|
sharedInstance = [[Analytics alloc] init];
|
|
|
|
});
|
|
|
|
|
|
|
|
return sharedInstance;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)start
|
|
|
|
{
|
|
|
|
// Check whether the user has enabled the sending of crash reports.
|
2018-07-02 12:51:47 +00:00
|
|
|
if (RiotSettings.shared.enableCrashReport)
|
2018-06-27 07:55:06 +00:00
|
|
|
{
|
2019-01-07 17:42:08 +00:00
|
|
|
[MatomoTracker shared].isOptedOut = NO;
|
2018-06-27 07:55:06 +00:00
|
|
|
|
2019-01-07 17:42:08 +00:00
|
|
|
[[MatomoTracker shared] setCustomVariableWithIndex:1 name:@"App Platform" value:@"iOS Platform"];
|
|
|
|
[[MatomoTracker shared] setCustomVariableWithIndex:2 name:@"App Version" value:[AppDelegate theDelegate].appVersion];
|
2018-06-27 07:55:06 +00:00
|
|
|
|
|
|
|
// The language is either the one selected by the user within the app
|
|
|
|
// or, else, the one configured by the OS
|
|
|
|
NSString *language = [NSBundle mxk_language] ? [NSBundle mxk_language] : [[NSBundle mainBundle] preferredLocalizations][0];
|
2019-01-07 17:42:08 +00:00
|
|
|
[[MatomoTracker shared] setCustomVariableWithIndex:4 name:@"Chosen Language" value:language];
|
2018-06-27 07:55:06 +00:00
|
|
|
|
|
|
|
MXKAccount* account = [MXKAccountManager sharedManager].activeAccounts.firstObject;
|
|
|
|
if (account)
|
|
|
|
{
|
2019-01-07 17:42:08 +00:00
|
|
|
[[MatomoTracker shared] setCustomVariableWithIndex:7 name:@"Homeserver URL" value:account.mxCredentials.homeServer];
|
|
|
|
[[MatomoTracker shared] setCustomVariableWithIndex:8 name:@"Identity Server URL" value:account.identityServerURL];
|
2018-06-27 07:55:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: We should also track device and os version
|
|
|
|
// But that needs to be decided for all platforms
|
|
|
|
|
|
|
|
// Catch and log crashes
|
|
|
|
[MXLogger logCrashes:YES];
|
|
|
|
[MXLogger setBuildVersion:[AppDelegate theDelegate].build];
|
|
|
|
|
|
|
|
#ifdef DEBUG
|
|
|
|
// Disable analytics in debug as it pollutes stats
|
2019-01-07 17:42:08 +00:00
|
|
|
[MatomoTracker shared].isOptedOut = YES;
|
2018-06-27 07:55:06 +00:00
|
|
|
#endif
|
|
|
|
}
|
2018-06-27 09:00:26 +00:00
|
|
|
else
|
2018-06-27 07:55:06 +00:00
|
|
|
{
|
2018-06-27 09:00:26 +00:00
|
|
|
NSLog(@"[AppDelegate] The user decided to not send analytics");
|
2019-01-07 17:42:08 +00:00
|
|
|
[MatomoTracker shared].isOptedOut = YES;
|
2018-06-27 07:55:06 +00:00
|
|
|
[MXLogger logCrashes:NO];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)stop
|
|
|
|
{
|
2019-01-07 17:42:08 +00:00
|
|
|
[MatomoTracker shared].isOptedOut = YES;
|
2018-06-27 07:55:06 +00:00
|
|
|
[MXLogger logCrashes:NO];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)trackScreen:(NSString *)screenName
|
|
|
|
{
|
|
|
|
// Use the same pattern as Android
|
2019-01-07 23:24:11 +00:00
|
|
|
NSString *appName = [[NSBundle mainBundle] infoDictionary][@"CFBundleDisplayName"];
|
2018-06-27 07:55:06 +00:00
|
|
|
NSString *appVersion = [AppDelegate theDelegate].appVersion;
|
|
|
|
|
2019-01-07 17:42:08 +00:00
|
|
|
[[MatomoTracker shared] trackWithView:@[@"ios", appName, appVersion, screenName]
|
2018-06-27 07:55:06 +00:00
|
|
|
url:nil];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)dispatch
|
|
|
|
{
|
2019-01-07 17:42:08 +00:00
|
|
|
[[MatomoTracker shared] dispatch];
|
2018-06-27 07:55:06 +00:00
|
|
|
}
|
|
|
|
|
2018-06-27 15:57:40 +00:00
|
|
|
- (void)trackLaunchScreenDisplayDuration:(NSTimeInterval)seconds
|
|
|
|
{
|
2018-06-29 10:04:49 +00:00
|
|
|
NSString *action = [NSString stringWithFormat:kAnalyticsMetricsActionPattern, kMXAnalyticsStartupCategory];
|
2018-06-27 15:57:40 +00:00
|
|
|
|
2019-01-07 17:42:08 +00:00
|
|
|
[[MatomoTracker shared] trackWithEventWithCategory:kAnalyticsMetricsCategory
|
2018-06-27 15:57:40 +00:00
|
|
|
action:action
|
|
|
|
name:kMXAnalyticsStartupLaunchScreen
|
|
|
|
number:@(seconds * 1000)
|
|
|
|
url:nil];
|
|
|
|
}
|
|
|
|
|
|
|
|
#pragma mark - MXAnalyticsDelegate
|
|
|
|
|
|
|
|
- (void)trackStartupStorePreloadDuration: (NSTimeInterval)seconds
|
|
|
|
{
|
2018-06-29 10:04:49 +00:00
|
|
|
NSString *action = [NSString stringWithFormat:kAnalyticsMetricsActionPattern, kMXAnalyticsStartupCategory];
|
2018-06-27 15:57:40 +00:00
|
|
|
|
2019-01-07 17:42:08 +00:00
|
|
|
[[MatomoTracker shared] trackWithEventWithCategory:kAnalyticsMetricsCategory
|
2018-06-27 15:57:40 +00:00
|
|
|
action:action
|
|
|
|
name:kMXAnalyticsStartupStorePreload
|
|
|
|
number:@(seconds * 1000)
|
|
|
|
url:nil];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)trackStartupMountDataDuration: (NSTimeInterval)seconds
|
|
|
|
{
|
2018-06-29 10:04:49 +00:00
|
|
|
NSString *action = [NSString stringWithFormat:kAnalyticsMetricsActionPattern, kMXAnalyticsStartupCategory];
|
2018-06-27 15:57:40 +00:00
|
|
|
|
2019-01-07 17:42:08 +00:00
|
|
|
[[MatomoTracker shared] trackWithEventWithCategory:kAnalyticsMetricsCategory
|
2018-06-27 15:57:40 +00:00
|
|
|
action:action
|
|
|
|
name:kMXAnalyticsStartupMountData
|
|
|
|
number:@(seconds * 1000)
|
|
|
|
url:nil];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)trackStartupSyncDuration: (NSTimeInterval)seconds isInitial: (BOOL)isInitial
|
|
|
|
{
|
2018-06-29 10:04:49 +00:00
|
|
|
NSString *action = [NSString stringWithFormat:kAnalyticsMetricsActionPattern, kMXAnalyticsStartupCategory];
|
2018-06-27 15:57:40 +00:00
|
|
|
|
2019-01-07 17:42:08 +00:00
|
|
|
[[MatomoTracker shared] trackWithEventWithCategory:kAnalyticsMetricsCategory
|
2018-06-27 15:57:40 +00:00
|
|
|
action:action
|
|
|
|
name:isInitial ? kMXAnalyticsStartupInititialSync : kMXAnalyticsStartupIncrementalSync
|
|
|
|
number:@(seconds * 1000)
|
|
|
|
url:nil];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)trackRoomCount: (NSUInteger)roomCount
|
|
|
|
{
|
2018-06-29 10:04:49 +00:00
|
|
|
NSString *action = [NSString stringWithFormat:kAnalyticsMetricsActionPattern, kMXAnalyticsStatsCategory];
|
2018-06-27 15:57:40 +00:00
|
|
|
|
2019-01-07 17:42:08 +00:00
|
|
|
[[MatomoTracker shared] trackWithEventWithCategory:kAnalyticsMetricsCategory
|
2018-06-27 15:57:40 +00:00
|
|
|
action:action
|
|
|
|
name:kMXAnalyticsStatsRooms
|
|
|
|
number:@(roomCount)
|
|
|
|
url:nil];
|
|
|
|
}
|
|
|
|
|
2018-06-29 05:35:31 +00:00
|
|
|
#pragma mark - MXDecryptionFailureDelegate
|
|
|
|
|
2018-07-02 15:14:39 +00:00
|
|
|
- (void)trackFailures:(NSDictionary<NSString *,NSNumber *> *)failuresCounts
|
2018-06-29 05:35:31 +00:00
|
|
|
{
|
2018-07-02 15:14:39 +00:00
|
|
|
for (NSString *reason in failuresCounts)
|
|
|
|
{
|
2019-01-07 17:42:08 +00:00
|
|
|
[[MatomoTracker shared] trackWithEventWithCategory:kAnalyticsE2eCategory
|
2018-07-02 15:14:39 +00:00
|
|
|
action:kAnalyticsE2eDecryptionFailureAction
|
|
|
|
name:reason
|
|
|
|
number:failuresCounts[reason]
|
|
|
|
url:nil];
|
|
|
|
}
|
2018-06-29 05:35:31 +00:00
|
|
|
}
|
|
|
|
|
2018-06-27 07:55:06 +00:00
|
|
|
@end
|