diff --git a/Sources/Core/Media/Controllers/MediaTrackingImpl.swift b/Sources/Core/Media/Controllers/MediaTrackingImpl.swift index 59079b4e1..ee3ed9d00 100644 --- a/Sources/Core/Media/Controllers/MediaTrackingImpl.swift +++ b/Sources/Core/Media/Controllers/MediaTrackingImpl.swift @@ -136,7 +136,9 @@ class MediaTrackingImpl: MediaTracking { addEntitiesAndTrack(event: event) } if shouldSendPercentProgress() { - addEntitiesAndTrack(event: MediaPercentProgressEvent()) + let duration = player?.duration + let test = player?.percentProgress + addEntitiesAndTrack(event: MediaPercentProgressEvent(percentProgress: self.player.percentProgress)) } // update state for events after this one diff --git a/Sources/Core/Media/Events/MediaPercentProgressEvent.swift b/Sources/Core/Media/Events/MediaPercentProgressEvent.swift index b46447f1b..ec73060a5 100644 --- a/Sources/Core/Media/Events/MediaPercentProgressEvent.swift +++ b/Sources/Core/Media/Events/MediaPercentProgressEvent.swift @@ -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 + } } diff --git a/Tests/Media/TestMediaController.swift b/Tests/Media/TestMediaController.swift index b1c7ef3ab..56009c0e4 100644 --- a/Tests/Media/TestMediaController.swift +++ b/Tests/Media/TestMediaController.swift @@ -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))