Add trigger to join and view room metrics events #5769

- Added trigger for JoinedRoom event
- Tracking ViewRoom event for rooms and events
This commit is contained in:
Gil Eluard
2022-03-15 10:31:24 +01:00
parent 59a6f2d356
commit 0df41c3b90
34 changed files with 355 additions and 5 deletions
+58 -1
View File
@@ -45,6 +45,8 @@ import AnalyticsEvents
/// The service used to interact with account data settings.
private var service: AnalyticsService?
private var viewRoomActiveSpace: AnalyticsViewRoomActiveSpace = .home
/// Whether or not the object is enabled and sending events to the server.
var isRunning: Bool { client.isRunning }
@@ -59,6 +61,24 @@ import AnalyticsEvents
RiotSettings.shared.hasAcceptedMatomoAnalytics
}
var joinedRoomTrigger: AnalyticsJoinedRoomTrigger = .unknown
var viewRoomTrigger: AnalyticsViewRoomTrigger = .unknown
var activeSpace: MXSpace? {
didSet {
MXLog.debug("[Analytics] activeSpace space \(activeSpace?.summary?.displayname ?? "home")")
updateViewRoomActiveSpace()
}
}
var exploringSpace: MXSpace? {
didSet {
MXLog.debug("[Analytics] exploring space \(exploringSpace?.summary?.displayname ?? "none")")
updateViewRoomActiveSpace()
}
}
// MARK: - Public
/// Opts in to analytics tracking with the supplied session.
@@ -165,6 +185,19 @@ import AnalyticsEvents
private func capture(event: AnalyticsEventProtocol) {
client.capture(event)
}
/// Update `viewRoomActiveSpace` property according to the current value of `exploringSpace` and `activeSpace` properties.
private func updateViewRoomActiveSpace() {
let space = exploringSpace ?? activeSpace
guard let spaceRoom = space?.room else {
viewRoomActiveSpace = .home
return
}
spaceRoom.state { roomState in
self.viewRoomActiveSpace = roomState?.isJoinRulePublic == true ? .public : .private
}
}
}
// MARK: - Public tracking methods
@@ -242,6 +275,28 @@ extension Analytics {
func trackIdentityServerAccepted(_ accepted: Bool) {
// Do we still want to track this?
}
/// Track view room event triggered when the user changes rooms.
/// - Parameters:
/// - room: the room being viewed
func trackViewRoom(_ room: MXRoom) {
trackViewRoom(asDM: room.isDirect, isSpace: room.summary?.roomType == .space)
}
/// Track view room event triggered when the user changes rooms.
/// - Parameters:
/// - isDM: Whether the room is a DM.
/// - isSpace: Whether the room is a Space.
func trackViewRoom(asDM isDM: Bool, isSpace: Bool) {
MXLog.debug("[Analytics] trackViewRoom (asDM:\(isDM), isSpace:\(isSpace), trigger:\(viewRoomTrigger.trigger?.rawValue ?? "unknown"), activeSpace:\(viewRoomActiveSpace.space?.rawValue ?? "unknown"))")
let event = AnalyticsEvent.ViewRoom(activeSpace: viewRoomActiveSpace.space,
isDM: isDM,
isSpace: isSpace,
trigger: viewRoomTrigger.trigger,
viaKeyboard: nil)
viewRoomTrigger = .unknown
capture(event: event)
}
}
// MARK: - MXAnalyticsDelegate
@@ -284,8 +339,10 @@ extension Analytics: MXAnalyticsDelegate {
return
}
let event = AnalyticsEvent.JoinedRoom(isDM: isDM, isSpace: isSpace, roomSize: roomSize, trigger: nil)
let event = AnalyticsEvent.JoinedRoom(isDM: isDM, isSpace: isSpace, roomSize: roomSize, trigger: joinedRoomTrigger.trigger)
capture(event: event)
self.joinedRoomTrigger = .unknown
}
/// **Note** This method isn't currently implemented.
@@ -0,0 +1,50 @@
//
// Copyright 2022 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 Foundation
import AnalyticsEvents
@objc enum AnalyticsJoinedRoomTrigger: Int {
case unknown
case invite
case notification
case roomDirectory
case roomPreview
case slashCommand
case spaceHierarchy
case timeline
var trigger: AnalyticsEvent.JoinedRoom.Trigger? {
switch self {
case .unknown:
return nil
case .invite:
return .Invite
case .notification:
return .Notification
case .roomDirectory:
return .RoomDirectory
case .roomPreview:
return .RoomPreview
case .slashCommand:
return .SlashCommand
case .spaceHierarchy:
return .SpaceHierarchy
case .timeline:
return .Timeline
}
}
}
@@ -0,0 +1,41 @@
//
// Copyright 2022 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 Foundation
import AnalyticsEvents
@objc enum AnalyticsViewRoomActiveSpace: Int {
case unknown
case home
case meta
case `private`
case `public`
var space: AnalyticsEvent.ViewRoom.ActiveSpace? {
switch self {
case .unknown:
return nil
case .home:
return .Home
case .meta:
return .Meta
case .private:
return .Private
case .public:
return .Public
}
}
}
@@ -0,0 +1,89 @@
//
// Copyright 2022 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 Foundation
import AnalyticsEvents
@objc enum AnalyticsViewRoomTrigger: Int {
case unknown
case created
case messageSearch
case messageUser
case notification
case predecessor
case roomDirectory
case roomList
case spaceHierarchy
case timeline
case tombstone
case verificationRequest
case widget
case roomMemberDetail
case fileSearch
case roomSearch
case searchContactDetail
case spaceMemberDetail
case inCall
case spaceMenu
case spaceSettings
var trigger: AnalyticsEvent.ViewRoom.Trigger? {
switch self {
case .unknown:
return nil
case .created:
return .Created
case .messageSearch:
return .MessageSearch
case .messageUser:
return .MessageUser
case .notification:
return .Notification
case .predecessor:
return .Predecessor
case .roomDirectory:
return .RoomDirectory
case .roomList:
return .RoomList
case .spaceHierarchy:
return .SpaceHierarchy
case .timeline:
return .Timeline
case .tombstone:
return .Tombstone
case .verificationRequest:
return .VerificationRequest
case .widget:
return .Widget
case .fileSearch:
return .MobileFileSearch
case .roomSearch:
return .MobileRoomSearch
case .roomMemberDetail:
return .MobileRoomMemberDetail
case .searchContactDetail:
return .MobileSearchContactDetail
case .spaceMemberDetail:
return .MobileSpaceMemberDetail
case .inCall:
return .MobileInCall
case .spaceMenu:
return .MobileSpaceMenu
case .spaceSettings:
return .MobileSpaceSettings
}
}
}