Skip to content

Commit

Permalink
Merge pull request #5836 from vector-im/gil/5401_Instrument_metrics_f…
Browse files Browse the repository at this point in the history
…or_the_IA_project

Instrument metrics for iOS for the IA project #5401
  • Loading branch information
gileluard authored Mar 17, 2022
2 parents 0a5164a + 21d252a commit 164be25
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 3 deletions.
6 changes: 6 additions & 0 deletions Riot/Managers/Settings/RiotSettings.swift
Original file line number Diff line number Diff line change
Expand Up @@ -355,4 +355,10 @@ final class RiotSettings: NSObject {

@UserDefault(key: "slideMenuRoomsCoachMessageHasBeenDisplayed", defaultValue: false, storage: defaults)
var slideMenuRoomsCoachMessageHasBeenDisplayed

// MARK: - Metrics

/// Number of spaces previously tracked by the `AnalyticsSpaceTracker` instance.
@UserDefault(key: "lastNumberOfTrackedSpaces", defaultValue: nil, storage: defaults)
var lastNumberOfTrackedSpaces: Int?
}
11 changes: 8 additions & 3 deletions Riot/Modules/Analytics/Analytics.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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)
}

Expand Down
47 changes: 47 additions & 0 deletions Riot/Modules/Analytics/AnalyticsSpaceTracker.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
//
// 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: - 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 RiotSettings.shared.lastNumberOfTrackedSpaces != spaceNumber else {
return
}

Analytics.shared.updateUserProperties(numSpaces: spaceNumber)
RiotSettings.shared.lastNumberOfTrackedSpaces = spaceNumber
}
}
6 changes: 6 additions & 0 deletions Riot/Modules/Analytics/AnalyticsUIElement.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -34,6 +36,10 @@ import AnalyticsEvents
return .MobileThreadListThreadItem
case .threadListFilterItem:
return .MobileThreadListFilterItem
case .spacePanelSelectedSpace:
return .SpacePanelSelectedSpace
case .spacePanelSwitchSpace:
return .SpacePanelSwitchSpace
}
}
}
4 changes: 4 additions & 0 deletions Riot/Modules/Spaces/SpaceList/SpaceListViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -73,13 +73,16 @@ final class SpaceListViewModel: SpaceListViewModelType {
self.loadData()
case .selectRow(at: let indexPath, from: let sourceView):
guard self.selectedIndexPath != indexPath else {
Analytics.shared.trackInteraction(.spacePanelSelectedSpace)
return
}

let section = self.sections[indexPath.section]
switch section {
case .home:
self.selectHome()
self.selectedIndexPath = indexPath
Analytics.shared.trackInteraction(.spacePanelSwitchSpace)
self.update(viewState: .selectionChanged(indexPath))
case .spaces(let viewDataList):
let spaceViewData = viewDataList[indexPath.row]
Expand All @@ -88,6 +91,7 @@ final class SpaceListViewModel: SpaceListViewModelType {
} else {
self.selectSpace(with: spaceViewData.spaceId)
self.selectedIndexPath = indexPath
Analytics.shared.trackInteraction(.spacePanelSwitchSpace)
self.update(viewState: .selectionChanged(indexPath))
}
case .addSpace:
Expand Down
1 change: 1 addition & 0 deletions changelog.d/5401.change
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Instrument metrics for the IA project.

0 comments on commit 164be25

Please sign in to comment.