Skip to content

Commit

Permalink
Add percent progress to event (close #662)
Browse files Browse the repository at this point in the history
  • Loading branch information
mscwilson authored Feb 9, 2024
1 parent 18cdf59 commit f755406
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import android.view.View
import android.widget.MediaController
import com.snowplowanalytics.snowplow.Snowplow
import com.snowplowanalytics.snowplow.event.Event
import com.snowplowanalytics.snowplow.media.configuration.MediaTrackingConfiguration
import com.snowplowanalytics.snowplow.media.controller.MediaTracking
import com.snowplowanalytics.snowplow.media.entity.MediaPlayerEntity
import com.snowplowanalytics.snowplow.media.event.*
Expand Down Expand Up @@ -130,9 +131,14 @@ class VideoViewController(activity: Activity, uri: Uri) {
reset()
loaded = true

mediaTracking = Snowplow.defaultTracker?.media?.startMediaTracking(
val configuration = MediaTrackingConfiguration(
id = UUID.randomUUID().toString(),
player = player
player = player,
boundaries = listOf(10, 25, 50, 75),
)

mediaTracking = Snowplow.defaultTracker?.media?.startMediaTracking(
configuration
)

updateThread = UpdateThread()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -523,6 +523,29 @@ class TestMediaController {
assertEquals(3, trackedEvents.filter { it.schema == eventSchema("percent_progress") }.size)
}

@Test
fun progressEventShouldHavePercentValue() {
val configuration = MediaTrackingConfiguration(
id = "media1",
player = MediaPlayerEntity(duration = 100.0),
boundaries = listOf(50),
)
val media = tracker?.media?.startMediaTracking(configuration = configuration)

media?.track(MediaPlayEvent())
for (i in 1 until 60) {
media?.update(player = MediaPlayerEntity(currentTime = i.toDouble()))
}

Thread.sleep(100)

assertEquals(2, trackedEvents.size)

val progressEvents = trackedEvents.filter { it.schema == eventSchema("percent_progress") }
assertEquals(1, progressEvents.size)
assertEquals(50, progressEvents[0].payload["percentProgress"])
}

@Test
fun doesntSendProgressEventsIfPaused() {
val configuration = MediaTrackingConfiguration(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ class MediaTrackingImpl (
// track events
event?.let { addEntitiesAndTrack(it) }
if (shouldSendPercentProgressEvent()) {
addEntitiesAndTrack(MediaPercentProgressEvent())
addEntitiesAndTrack(MediaPercentProgressEvent(this.player.percentProgress))
}

// update state for events after this one
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,12 @@ import com.snowplowanalytics.snowplow.event.AbstractSelfDescribing
/**
* Media player event fired when a percentage boundary set in the `boundaries` list in `MediaTrackingConfiguration` is reached.
*/
class MediaPercentProgressEvent : AbstractSelfDescribing() {
class MediaPercentProgressEvent(val percentProgress: Int?) : AbstractSelfDescribing() {
override val schema: String
get() = MediaSchemata.eventSchema("percent_progress")

override val dataPayload: Map<String, Any?>
get() = emptyMap()
get() = mapOf(
"percentProgress" to percentProgress,
)
}

0 comments on commit f755406

Please sign in to comment.