Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: add video and discussion analytics #93

Merged
merged 4 commits into from
Nov 13, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ public struct StartupView: View {
searchQuery: searchQuery,
sourceScreen: .startup
)
viewModel.logAnalytics(searchQuery: searchQuery)
})
.autocapitalization(.none)
.autocorrectionDisabled()
Expand Down Expand Up @@ -89,6 +90,7 @@ public struct StartupView: View {
searchQuery: searchQuery,
sourceScreen: .startup
)
viewModel.logAnalytics(searchQuery: nil)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggestion:
We can replace:

func logAnalytics(searchQuery: String?)

with:

func logAnalytics(searchQuery: String? = nil)

It will avoid to pass mandatory nil parameter:

viewModel.logAnalytics(searchQuery: nil)

To:

viewModel.logAnalytics()

} label: {
Text(AuthLocalization.Startup.exploreAllCourses)
.underline()
Expand Down
38 changes: 38 additions & 0 deletions Core/Core/Analytics/CoreAnalytics.swift
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,11 @@ public enum AnalyticsEvent: String {
case bulkDownloadVideosSection = "Video:Bulk Download Section"
case bulkDeleteVideosSubsection = "Videos:Delete Subsection Videos"
case bulkDeleteVideosSection = "Videos:Delete Section Videos"
case videoLoaded = "Video:Loaded"
case videoChangeSpeed = "Video:Change Speed"
case videoPlayed = "Video:Played"
case videoPaused = "Video:Paused"
case videoCompleted = "Video:Completed"
case discussionAllPostsClicked = "Discussion:All Posts Clicked"
case discussionFollowingClicked = "Discussion:Following Posts Clicked"
case discussionTopicClicked = "Discussion:Topic Clicked"
Expand All @@ -320,6 +325,13 @@ public enum AnalyticsEvent: String {
case logistrationRegister = "Logistration:Register"
case profileEdit = "Profile:Edit Profile"
case profilehelpUsImprove = "Profile:Feedback Form Clicked"
case discussionPostCreated = "Discussion:Post Created"
case discussionResponseAdded = "Discussion:Response Added"
case discussionCommentAdded = "Discussion:Comment Added"
case discussionFollowToggle = "Dicussion:Post Follow Toggle"
case discussionLikeToggle = "Discussion:Like Toggle"
case discussionReportToggle = "Discussion:Report Toggle"

}

public enum EventBIValue: String {
Expand Down Expand Up @@ -371,6 +383,11 @@ public enum EventBIValue: String {
case bulkDeleteVideosSubsection = "edx.bi.app.video.delete.subsection"
case bulkDownloadVideosSection = "edx.bi.video.section.bulkdownload"
case bulkDeleteVideosSection = "edx.bi.app.video.delete.section"
case videoLoaded = "edx.bi.app.videos.loaded"
case videoChangeSpeed = "edx.bi.app.videos.speed.changed"
case videoPlayed = "edx.bi.app.videos.played"
case videoPaused = "edx.bi.app.videos.paused"
case videoCompleted = "edx.bi.app.videos.completed"
case dashboardCourseClicked = "edx.bi.app.course.dashboard"
case courseOutlineVideosTabClicked = "edx.bi.app.course.video_tab"
case courseOutlineDatesTabClicked = "edx.bi.app.course.dates_tab"
Expand Down Expand Up @@ -420,6 +437,12 @@ public enum EventBIValue: String {
case logistrationRegister = "edx.bi.app.logistration.register"
case profileEdit = "edx.bi.app.profile.edit"
case profilehelpUsImprove = "edx.bi.app.profile.feedback_form.clicked"
case discussionPostCreated = "edx.bi.app.discussion.post_created"
case discussionResponseAdded = "edx.bi.app.discussion.response_added"
case discussionCommentAdded = "edx.bi.app.discussion.comment_added"
case discussionFollowToggle = "edx.bi.app.discussion.follow_toggle"
case discussionLikeToggle = "edx.bi.app.discussion.like_toggle"
case discussionReportToggle = "edx.bi.app.discussion.report_toggle"
}

public struct EventParamKey {
Expand Down Expand Up @@ -463,6 +486,21 @@ public struct EventParamKey {
public static let errorAction = "error_action"
public static let flowType = "flow_type"
public static let alertType = "alert_type"
public static let videoURL = "video_url"
public static let oldSpeed = "old_speed"
public static let newSpeed = "new_speed"
public static let currentTime = "current_time"
public static let duration = "duration"
public static let postType = "post_type"
public static let followPost = "follow_post"
public static let threadID = "thread_id"
public static let responseID = "response_id"
public static let commentID = "comment_id"
public static let author = "author"
public static let follow = "follow"
public static let like = "like"
public static let report = "report"
public static let discussionType = "discussion_type"
}

public struct EventCategory {
Expand Down
60 changes: 60 additions & 0 deletions Course/Course/Presentation/CourseAnalytics.swift
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,36 @@ public protocol CourseAnalytics {
sectionId: String,
videos: Int
)

func videoLoaded(courseID: String, blockID: String, videoURL: String)

func videoPlayed(courseID: String, blockID: String, videoURL: String)

func videoChangeSpeed(
Copy link

@shafqat-muneer shafqat-muneer Nov 7, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wouldn't videoSpeedChange be more appropriate?

courseID: String,
blockID: String,
videoURL: String,
oldSpeed: Float,
newSpeed: Float,
currentTime: Double,
duration: Double
)

func videoPaused(
courseID: String,
blockID: String,
videoURL: String,
currentTime: Double,
duration: Double
)

func videoCompleted(
courseID: String,
blockID: String,
videoURL: String,
currentTime: Double,
duration: Double
)
}

#if DEBUG
Expand Down Expand Up @@ -220,5 +250,35 @@ class CourseAnalyticsMock: CourseAnalytics {
sectionId: String,
videos: Int
) {}

public func videoLoaded(courseID: String, blockID: String, videoURL: String) {}

public func videoPlayed(courseID: String, blockID: String, videoURL: String) {}

public func videoChangeSpeed(
courseID: String,
blockID: String,
videoURL: String,
oldSpeed: Float,
newSpeed: Float,
currentTime: Double,
duration: Double
) {}

public func videoPaused(
courseID: String,
blockID: String,
videoURL: String,
currentTime: Double,
duration: Double
) {}

public func videoCompleted(
courseID: String,
blockID: String,
videoURL: String,
currentTime: Double,
duration: Double
) {}
}
#endif
4 changes: 3 additions & 1 deletion Course/Course/Presentation/Video/EncodedVideoPlayer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,9 @@ struct EncodedVideoPlayer_Previews: PreviewProvider {
languages: [],
playerStateSubject: CurrentValueSubject<VideoPlayerState?, Never>(nil),
connectivity: Connectivity(),
playerHolder: PlayerViewControllerHolder.mock
playerHolder: PlayerViewControllerHolder.mock,
appStorage: CoreStorageMock(),
analytics: CourseAnalyticsMock()
),
isOnScreen: true
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public protocol PlayerViewControllerHolderProtocol: AnyObject {
var isPlaying: Bool { get }
var isPlayingInPip: Bool { get }
var isOtherPlayerInPipPlaying: Bool { get }
var duration: TimeInterval { get }

init(
url: URL?,
Expand All @@ -34,6 +35,7 @@ public protocol PlayerViewControllerHolderProtocol: AnyObject {
func getErrorPublisher() -> AnyPublisher<Error, Never>
func getRatePublisher() -> AnyPublisher<Float, Never>
func getReadyPublisher() -> AnyPublisher<Bool, Never>
func getFinishPublisher() -> AnyPublisher<Void, Never>
func getService() -> PlayerServiceProtocol
func sendCompletion() async
}
Expand Down Expand Up @@ -187,6 +189,10 @@ public class PlayerViewControllerHolder: PlayerViewControllerHolderProtocol {
public func getReadyPublisher() -> AnyPublisher<Bool, Never> {
playerTracker.getReadyPublisher()
}

public func getFinishPublisher() -> AnyPublisher<Void, Never> {
playerTracker.getFinishPublisher()
}

public func getService() -> PlayerServiceProtocol {
playerService
Expand Down
4 changes: 3 additions & 1 deletion Course/Course/Presentation/Video/SubtitlesView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,9 @@ struct SubtittlesView_Previews: PreviewProvider {
languages: [],
playerStateSubject: CurrentValueSubject<VideoPlayerState?, Never>(nil),
connectivity: Connectivity(),
playerHolder: PlayerViewControllerHolder.mock
playerHolder: PlayerViewControllerHolder.mock,
appStorage: CoreStorageMock(),
analytics: CourseAnalyticsMock()
), scrollTo: {_ in }
)
}
Expand Down
84 changes: 83 additions & 1 deletion Course/Course/Presentation/Video/VideoPlayerViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -38,17 +38,23 @@ public class VideoPlayerViewModel: ObservableObject {
}
public let playerHolder: PlayerViewControllerHolderProtocol
internal var subscription = Set<AnyCancellable>()
private var appStorage: CoreStorage?
private var analytics: CourseAnalytics?

public init(
languages: [SubtitleUrl],
playerStateSubject: CurrentValueSubject<VideoPlayerState?, Never>? = nil,
connectivity: ConnectivityProtocol,
playerHolder: PlayerViewControllerHolderProtocol
playerHolder: PlayerViewControllerHolderProtocol,
appStorage: CoreStorage?,
analytics: CourseAnalytics?
) {
self.languages = languages
self.connectivity = connectivity
self.playerHolder = playerHolder
self.prepareLanguages()
self.appStorage = appStorage
self.analytics = analytics

observePlayer(with: playerStateSubject)
}
Expand Down Expand Up @@ -88,6 +94,20 @@ public class VideoPlayerViewModel: ObservableObject {
.sink {[weak self] isReady in
guard isReady else { return }
self?.isLoading = false
self?.trackVideoLoaded()
}
.store(in: &subscription)

playerHolder.getRatePublisher()
.sink {[weak self] rate in
guard self?.isLoading == false else { return }
self?.trackVideoChangeSpeed(rate: rate)
}
.store(in: &subscription)

playerHolder.getFinishPublisher()
.sink { [weak self] in
self?.trackVideoCompleted()
}
.store(in: &subscription)

Expand Down Expand Up @@ -184,4 +204,66 @@ public class VideoPlayerViewModel: ObservableObject {
})
}
}

private func trackVideoLoaded() {
analytics?.videoLoaded(
courseID: playerHolder.courseID,
blockID: playerHolder.blockID,
videoURL: playerHolder.url?.absoluteString ?? ""
)
}

private func trackVideoPlayed() {
analytics?.videoLoaded(
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe we should use videoPlayed here?

courseID: playerHolder.courseID,
blockID: playerHolder.blockID,
videoURL: playerHolder.url?.absoluteString ?? ""
)
}

private func trackVideoChangeSpeed(rate: Float) {
if rate == 0 {
analytics?.videoPaused(
courseID: playerHolder.courseID,
blockID: playerHolder.blockID,
videoURL: playerHolder.url?.absoluteString ?? "",
currentTime: currentTime,
duration: playerHolder.duration
)
} else {
guard let storage = appStorage,
let userSettings = storage.userSettings
else {
return
}

if userSettings.videoPlaybackSpeed == rate {
analytics?.videoPlayed(
courseID: playerHolder.courseID,
blockID: playerHolder.blockID,
videoURL: playerHolder.url?.absoluteString ?? ""
)
} else {
analytics?.videoChangeSpeed(
courseID: playerHolder.courseID,
blockID: playerHolder.blockID,
videoURL: playerHolder.url?.absoluteString ?? "",
oldSpeed: userSettings.videoPlaybackSpeed,
newSpeed: rate,
currentTime: currentTime,
duration: playerHolder.duration
)
}
}
}

private func trackVideoCompleted() {
analytics?.videoCompleted(
courseID: playerHolder.courseID,
blockID: playerHolder.blockID,
videoURL: playerHolder.url?.absoluteString ?? "",
currentTime: currentTime,
duration: playerHolder.duration
)
}
}
4 changes: 3 additions & 1 deletion Course/Course/Presentation/Video/YouTubeVideoPlayer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,9 @@ struct YouTubeVideoPlayer_Previews: PreviewProvider {
languages: [],
playerStateSubject: CurrentValueSubject<VideoPlayerState?, Never>(nil),
connectivity: Connectivity(),
playerHolder: YoutubePlayerViewControllerHolder.mock
playerHolder: YoutubePlayerViewControllerHolder.mock,
appStorage: CoreStorageMock(),
analytics: CourseAnalyticsMock()
),
isOnScreen: true
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,10 @@ public class YoutubePlayerViewControllerHolder: PlayerViewControllerHolderProtoc
public func getReadyPublisher() -> AnyPublisher<Bool, Never> {
playerTracker.getReadyPublisher()
}

public func getFinishPublisher() -> AnyPublisher<Void, Never> {
playerTracker.getFinishPublisher()
}

public func getService() -> PlayerServiceProtocol {
playerService
Expand Down
Loading
Loading