Skip to content

Commit

Permalink
Add percent progress to event
Browse files Browse the repository at this point in the history
  • Loading branch information
mscwilson committed Feb 7, 2024
1 parent c361f24 commit cb3004e
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 1 deletion.
4 changes: 3 additions & 1 deletion Sources/Core/Media/Controllers/MediaTrackingImpl.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
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

0 comments on commit cb3004e

Please sign in to comment.