From 5c7998e2409325a572fd577d5c5f55477e2bde05 Mon Sep 17 00:00:00 2001 From: JanNiklas Grabowski Date: Wed, 21 Jun 2023 06:24:46 +0000 Subject: [PATCH] Feature/4795 feature tracking matomo --- Riot/Modules/Room/RoomViewController.h | 1 + Riot/Modules/Room/RoomViewController.m | 18 +++- .../Shared/ForwardingShareItemSender.swift | 12 ++- .../Coordinator/PollEditFormCoordinator.swift | 25 +++++ bwi/MatomoAnalytics/BWIAnalytics.swift | 11 +++ bwi/MatomoAnalytics/BWIAnalyticsHelper.swift | 94 +++++++++++++++++++ .../PerformanceProfile.swift | 28 +----- 7 files changed, 155 insertions(+), 34 deletions(-) create mode 100644 bwi/MatomoAnalytics/BWIAnalyticsHelper.swift diff --git a/Riot/Modules/Room/RoomViewController.h b/Riot/Modules/Room/RoomViewController.h index 3a4ae5f81..1244e838c 100644 --- a/Riot/Modules/Room/RoomViewController.h +++ b/Riot/Modules/Room/RoomViewController.h @@ -36,6 +36,7 @@ @class VoiceBroadcastService; @class ComposerLinkActionBridgePresenter; @class PerformanceProfile; +@class BWIAnalyticsHelper; NS_ASSUME_NONNULL_BEGIN diff --git a/Riot/Modules/Room/RoomViewController.m b/Riot/Modules/Room/RoomViewController.m index e1562b51c..f6c3cd596 100644 --- a/Riot/Modules/Room/RoomViewController.m +++ b/Riot/Modules/Room/RoomViewController.m @@ -8179,6 +8179,7 @@ static CGSize kThreadListBarButtonItemImageSize; { [self.roomDataSource sendVoiceMessage:url additionalContentParams:nil mimeType:nil duration:duration samples:samples success:^(NSString *eventId) { MXLogDebug(@"Success with event id %@", eventId); + [self trackVoiceMessage: duration]; completion(YES); } failure:^(NSError *error) { MXLogError(@"Failed sending voice message"); @@ -8210,17 +8211,24 @@ static CGSize kThreadListBarButtonItemImageSize; if( [profile isLogable] ) { [self.roomDataSource.room members:^(MXRoomMembers *roomMembers) { NSUInteger noOfUsers = roomMembers.joinedMembers.count; - NSUInteger noOfDevices = 0; - for (MXRoomMember* member in roomMembers.joinedMembers) { - noOfDevices += [self.mainSession.crypto devicesForUser:member.userId].count; - } - [profile log2AnalyticsWithUsers:noOfUsers devices:noOfDevices]; + [BWIAnalyticsHelper getRoomDeviceCountWithRoom:self.roomDataSource.room completion:^(NSInteger deviceCount) { + [profile log2AnalyticsWithUsers:noOfUsers devices:deviceCount]; + }]; } failure:^(NSError *error) { }]; } } +// Bwi #4795: voice message +- (void) trackVoiceMessage:(NSInteger)duration { + [BWIAnalyticsHelper getRoomDeviceCountWithRoom:self.roomDataSource.room completion:^(NSInteger deviceCount) { + NSString *deviceCountString = [BWIAnalyticsHelper dimensionForDeviceCount: deviceCount]; + NSNumber *durationInSeconds = [NSNumber numberWithInteger:(duration / 1000)]; + [BWIAnalytics.sharedTracker trackEventWithDimensionWithCategory:@"Feature" action:@"SendVoiceMessage" dimension:deviceCountString value:durationInSeconds name:nil]; + }]; +} + #pragma mark - BWI Emoji History - (void) bwiAddedEmoji:(NSString*)emoji { diff --git a/RiotShareExtension/Shared/ForwardingShareItemSender.swift b/RiotShareExtension/Shared/ForwardingShareItemSender.swift index 3909812db..e87fa8c3f 100644 --- a/RiotShareExtension/Shared/ForwardingShareItemSender.swift +++ b/RiotShareExtension/Shared/ForwardingShareItemSender.swift @@ -53,10 +53,10 @@ class ForwardingShareItemSender: NSObject, ShareItemSenderProtocol { var localEcho: MXEvent? room.sendMessage(withContent: event.content, threadId: nil, localEcho: &localEcho) { result in switch result { + case .success(_): + self.trackForwardMessage(room: room) case .failure(let innerError): errors.append(innerError) - default: - break } dispatchGroup.leave() @@ -72,4 +72,12 @@ class ForwardingShareItemSender: NSObject, ShareItemSenderProtocol { success() } } + + func trackForwardMessage(room: MXRoom) { + BWIAnalyticsHelper.getRoomDeviceCount(room: room) { deviceCount in + let deviceCountString = BWIAnalyticsHelper.dimensionForDeviceCount(deviceCount) + let messageType = BWIAnalyticsHelper.getForwardingType(event: self.event) + BWIAnalytics.sharedTracker.trackEventWithDimension(category: "Feature", action: "ForwardMessage", dimension: deviceCountString, value: nil, name: messageType) + } + } } diff --git a/RiotSwiftUI/Modules/Room/PollEditForm/Coordinator/PollEditFormCoordinator.swift b/RiotSwiftUI/Modules/Room/PollEditForm/Coordinator/PollEditFormCoordinator.swift index 3c0db09b2..c3f44b40a 100644 --- a/RiotSwiftUI/Modules/Room/PollEditForm/Coordinator/PollEditFormCoordinator.swift +++ b/RiotSwiftUI/Modules/Room/PollEditForm/Coordinator/PollEditFormCoordinator.swift @@ -79,6 +79,7 @@ final class PollEditFormCoordinator: Coordinator, Presentable { guard let self = self else { return } self.pollEditFormViewModel.stopLoading() + self.log2Analytics(details: details, room: self.parameters.room) self.completion?() } failure: { [weak self] error in guard let self = self else { return } @@ -154,4 +155,28 @@ final class PollEditFormCoordinator: Coordinator, Presentable { return mapping[key] ?? EditFormPollType.disclosed } + + + // MARK: Bwi tracking + private func log2Analytics(details: EditFormPollDetails, room: MXRoom) { + BWIAnalyticsHelper.getRoomDeviceCount(room: room) { deviceCount in + var eventName: String + switch details.type { + case .undisclosed: + if details.showParticipants { + eventName = "undisclosed_show_participants" + } else { + eventName = "undisclosed" + } + case .disclosed: + if details.showParticipants { + eventName = "disclosed_show_participants" + } else { + eventName = "disclosed" + } + } + + BWIAnalytics.sharedTracker.trackEventWithDimension(category: "Feature", action: "SendPoll", dimension: BWIAnalyticsHelper.dimensionForDeviceCount(deviceCount), value: NSNumber(value: details.answerOptions.count), name: eventName) + } + } } diff --git a/bwi/MatomoAnalytics/BWIAnalytics.swift b/bwi/MatomoAnalytics/BWIAnalytics.swift index 5b82d975e..4a4e1c69c 100644 --- a/bwi/MatomoAnalytics/BWIAnalytics.swift +++ b/bwi/MatomoAnalytics/BWIAnalytics.swift @@ -195,6 +195,17 @@ import MatomoTracker } } } + + func trackEventWithDimension(category: String, action: String, dimension: String, value: NSNumber?, name: String?) { + if fastRunning { + // bwi: Analytics use custom config + if let dimensionIndex = analyticsConfig.selectedSendMessageDimensionIndex() { + matomo?.setDimension(dimension, forIndex: dimensionIndex) + matomo?.track(eventWithCategory: category, action: action, name: name, number: value, url:nil) // name optional unwrap? + matomo?.remove(dimensionAtIndex: dimensionIndex) + } + } + } } extension BWIAnalytics : MXAnalyticsDelegate { diff --git a/bwi/MatomoAnalytics/BWIAnalyticsHelper.swift b/bwi/MatomoAnalytics/BWIAnalyticsHelper.swift new file mode 100644 index 000000000..62f4f7f96 --- /dev/null +++ b/bwi/MatomoAnalytics/BWIAnalyticsHelper.swift @@ -0,0 +1,94 @@ +// +/* + * Copyright (c) 2023 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 MatrixSDK +@objc class BWIAnalyticsHelper: NSObject { + + @objc static func getRoomDeviceCount(room: MXRoom, completion: @escaping(Int) -> ()) { + room.members { roomMembers in + var noOfDevices = 0 + for member in roomMembers?.joinedMembers ?? [MXRoomMember]() { + noOfDevices += room.mxSession.crypto.devices(forUser: member.userId).count + } + completion(noOfDevices) + } lazyLoadedMembers: { _ in + completion(0) + } failure: { error in + MXLog.error("[RoomAnalyticsHelper] Failed loading room", context: error) + completion(0) + } + } + + @objc static func getForwardingType(event: MXEvent) -> String? { + guard let messageType: MXMessageType = event.messageType else { + return nil + } + switch messageType { + case .text: + return "text" + case .image: + return "image" + case .video: + return "video" + case .audio: + if (event.content["org.matrix.msc2516.voice"] != nil) || (event.content["org.matrix.msc3245.voice"] != nil) { + return "voice_message" + } else { + return "audio" + } + case .emote: + return nil + case .notice: + return nil + case .location: + return "location" + case .file: + return "file" + case .custom(_): + return nil + } + } + + // MARK: custom dimensions + + @objc static func dimensionForDeviceCount(_ deviceCount: Int) -> String { + if deviceCount <= 0 { + return "Undefiniert" + } + if deviceCount <= 10 { + return "1 bis 10" + } + if deviceCount <= 100 { + return "11 bis 100" + } + if deviceCount <= 200 { + return "101 bis 200" + } + if deviceCount <= 500 { + return "201 bis 500" + } + if deviceCount <= 1000 { + return "501 bis 1000" + } + if deviceCount <= 2500 { + return "1001 bis 2500" + } else { + return "mehr als 2500" + } + } +} diff --git a/bwi/PerformanceProfiles/PerformanceProfile.swift b/bwi/PerformanceProfiles/PerformanceProfile.swift index b48aefe52..98b9b6d86 100644 --- a/bwi/PerformanceProfiles/PerformanceProfile.swift +++ b/bwi/PerformanceProfiles/PerformanceProfile.swift @@ -53,33 +53,7 @@ import Foundation func log2Analytics(users: Int, devices: Int) { if isLogable() { - BWIAnalytics.sharedTracker.trackSlowMessage(dimension: dimensionForDeviceCount(devices), value: Int(timeInterval*1000)) - } - } - - func dimensionForDeviceCount(_ deviceCount: Int) -> String { - if deviceCount <= 0 { - return "Undefiniert" - } - if deviceCount <= 10 { - return "1 bis 10" - } - if deviceCount <= 100 { - return "11 bis 100" - } - if deviceCount <= 200 { - return "101 bis 200" - } - if deviceCount <= 500 { - return "201 bis 500" - } - if deviceCount <= 1000 { - return "501 bis 1000" - } - if deviceCount <= 2500 { - return "1001 bis 2500" - } else { - return "mehr als 2500" + BWIAnalytics.sharedTracker.trackSlowMessage(dimension: BWIAnalyticsHelper.dimensionForDeviceCount(devices), value: Int(timeInterval*1000)) } } }