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

Add percent progress to event #875

Merged
merged 1 commit into from
Feb 9, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion Sources/Core/Media/Controllers/MediaTrackingImpl.swift
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ class MediaTrackingImpl: MediaTracking {
addEntitiesAndTrack(event: event)
}
if shouldSendPercentProgress() {
addEntitiesAndTrack(event: MediaPercentProgressEvent())
addEntitiesAndTrack(event: MediaPercentProgressEvent(percentProgress: self.player.percentProgress))
}

// update state for events after this one
Expand Down
9 changes: 9 additions & 0 deletions Sources/Core/Media/Events/MediaPercentProgressEvent.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,20 @@ import Foundation
/** Media player event fired when a percentage boundary set in the `boundaries` list in `MediaTrackingConfiguration` is reached. */
class MediaPercentProgressEvent: SelfDescribingAbstract {

let percentProgress: Int?

override var schema: String {
return MediaSchemata.eventSchema("percent_progress")
}

override var payload: [String : Any] {
if let percentProgress = percentProgress {
return ["percentProgress": percentProgress]
}
return [:]
}

public init(percentProgress: Int?) {
self.percentProgress = percentProgress
}
}
19 changes: 19 additions & 0 deletions Tests/Media/TestMediaController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -409,6 +409,25 @@ class TestMediaController: XCTestCase {
XCTAssertEqual(3, trackedEvents.filter { $0.schema?.contains("percent_progress") ?? false }.count)
}

func testProgressEventsShouldHavePercentValue() {
let configuration = MediaTrackingConfiguration(id: "media",
player: MediaPlayerEntity(duration: 100))
.boundaries([5])
let media = mediaController?.startMediaTracking(configuration: configuration)

media?.track(MediaPlayEvent())
for i in 1..<10 {
media?.update(player: MediaPlayerEntity(currentTime: Double(i)))
}

waitForEventsToBeTracked()

XCTAssertEqual(2, trackedEvents.count)
let progressEvents = trackedEvents.filter { $0.schema?.contains("percent_progress") ?? false }
XCTAssertEqual(1, progressEvents.count)
XCTAssertEqual(5, progressEvents[0].payload["percentProgress"] as? Int)
}

func testDoesntSendProgressEventsIfPaused() {
let configuration = MediaTrackingConfiguration(id: "media",
player: MediaPlayerEntity(duration: 100))
Expand Down
Loading