Instrument metrics for iOS for the IA project #5401

- Added last needed metrics
This commit is contained in:
Gil Eluard
2022-03-16 16:56:27 +01:00
parent 0158373fdd
commit aad08cf6a2
5 changed files with 91 additions and 3 deletions
+8 -3
View File
@@ -59,6 +59,11 @@ import AnalyticsEvents
RiotSettings.shared.hasAcceptedMatomoAnalytics
}
// MARK: - Private
/// keep an instance of `AnalyticsSpaceTracker` to track space metrics when space graph is built.
private let spaceTracker: AnalyticsSpaceTracker = AnalyticsSpaceTracker()
// MARK: - Public
/// Opts in to analytics tracking with the supplied session.
@@ -174,10 +179,10 @@ extension Analytics {
/// Updates any user properties to help with creating cohorts.
///
/// Only non-nil properties will be updated when calling this method.
func updateUserProperties(ftueUseCase: UserSessionProperties.UseCase? = nil) {
func updateUserProperties(ftueUseCase: UserSessionProperties.UseCase? = nil, numFavouriteRooms: Int? = nil, numSpaces: Int? = nil) {
let userProperties = AnalyticsEvent.UserProperties(ftueUseCaseSelection: ftueUseCase?.analyticsName,
numFavouriteRooms: nil,
numSpaces: nil)
numFavouriteRooms: numFavouriteRooms,
numSpaces: numSpaces)
client.updateUserProperties(userProperties)
}
@@ -0,0 +1,72 @@
//
// 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
class AnalyticsSpaceTracker {
// MARK: - Constants
private enum Constants {
static let lastNumberOfSpaces: String = "AnalyticsSpaceTracker.lastNumberOfSpaces"
}
// Last number of spaces tracked
private var lastNumberOfSpaces: Int? {
get {
guard let value = UserDefaults.standard.value(forKey: Constants.lastNumberOfSpaces) as? NSNumber else {
return nil
}
return value.intValue
}
set {
guard let value = newValue else {
UserDefaults.standard.removeObject(forKey: Constants.lastNumberOfSpaces)
return
}
UserDefaults.standard.setValue(NSNumber(value: value), forKey: Constants.lastNumberOfSpaces)
}
}
// MARK: - Setup
init() {
NotificationCenter.default.addObserver(self, selector: #selector(self.spaceGraphDidUpdate(notification:)), name: MXSpaceService.didBuildSpaceGraph, object: nil)
}
@objc private func spaceGraphDidUpdate(notification: Notification) {
guard let spaceService = notification.object as? MXSpaceService else {
return
}
trackSpaceNumber(with: spaceService)
}
// MARK: - Private
private func trackSpaceNumber(with spaceService: MXSpaceService) {
let spaceNumber = spaceService.spaceSummaries.filter { $0.membership == .join }.count
guard lastNumberOfSpaces != spaceNumber else {
return
}
Analytics.shared.updateUserProperties(numSpaces: spaceNumber)
lastNumberOfSpaces = spaceNumber
}
}
@@ -22,6 +22,8 @@ import AnalyticsEvents
case roomThreadSummaryItem
case threadListThreadItem
case threadListFilterItem
case spacePanelSelectedSpace
case spacePanelSwitchSpace
/// The element name reported to the AnalyticsEvent.
var name: AnalyticsEvent.Interaction.Name {
@@ -34,6 +36,10 @@ import AnalyticsEvents
return .MobileThreadListThreadItem
case .threadListFilterItem:
return .MobileThreadListFilterItem
case .spacePanelSelectedSpace:
return .SpacePanelSelectedSpace
case .spacePanelSwitchSpace:
return .SpacePanelSwitchSpace
}
}
}