Skip to content

Commit 345f1b5

Browse files
authored
fix: pause mediaContentTimeSpent when triggering logMediaContentEnd e… (#639)
1 parent bb6273f commit 345f1b5

File tree

2 files changed

+78
-0
lines changed

2 files changed

+78
-0
lines changed

src/session.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,12 @@ export class MediaSession {
298298
*/
299299
logMediaContentEnd(options?: Options) {
300300
this.mediaContentComplete = true;
301+
if (this.currentPlaybackStartTimestamp) {
302+
this.storedPlaybackTime =
303+
this.storedPlaybackTime +
304+
(Date.now() - this.currentPlaybackStartTimestamp);
305+
this.currentPlaybackStartTimestamp = undefined;
306+
}
301307
const event = this.createMediaEvent(MediaEventType.ContentEnd, options);
302308

303309
this.logEvent(event);

test/session.test.ts

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -935,6 +935,42 @@ describe('MediaSession', () => {
935935
);
936936
expect(bond.args[0][0].options.currentPlayheadPosition).to.eq(32);
937937
});
938+
939+
it('should pause mediaContentTimeSpent', async () => {
940+
const bond = sinon.spy(mp, 'logBaseEvent');
941+
942+
const options = {
943+
customAttributes: {
944+
content_rating: 'epic',
945+
},
946+
currentPlayheadPosition: 0,
947+
};
948+
949+
// logPlay is triggered to start media content time tracking.
950+
mpMedia.logPlay(options);
951+
// 100ms delay added to account for the time spent on media content.
952+
await new Promise(f => setTimeout(f, 100));
953+
mpMedia.logPause(options);
954+
// Another 100ms delay added after logPause is triggered to account for time spent on media session (total = +200ms).
955+
await new Promise(f => setTimeout(f, 100));
956+
mpMedia.logMediaSessionEnd(options);
957+
958+
// the 4th event in bond.args is the Media Session Summary which contains the mediaContentTimeSpent and mediaTimeSpent.
959+
const mpMediaContentTimeSpent = bond.args[3][0].options.customAttributes.media_content_time_spent;
960+
const mpMediaTimeSpent = bond.args[3][0].options.customAttributes.media_time_spent;
961+
962+
expect(mpMediaContentTimeSpent).to.not.eql(mpMediaTimeSpent);
963+
964+
// the mediaContentTimeSpent varies in value each test run by a millisecond or two (i,e value is could be 100ms, 101ms, 102ms)
965+
// and we can't determine the exact value, hence the greaterThanOrEqual and lessThanOrEqual tests.
966+
expect(mpMediaContentTimeSpent).to.greaterThanOrEqual(100);
967+
expect(mpMediaContentTimeSpent).to.lessThanOrEqual(200);
968+
969+
// the mediaTimeSpent varies in value each test run by a millisecond or two (i,e value is could be 200ms, 201ms, 202ms)
970+
// and we can't determine the exact value, hence the greaterThanOrEqual and lessThanOrEqual tests.
971+
expect(mpMediaTimeSpent).to.greaterThanOrEqual(200);
972+
expect(mpMediaTimeSpent).to.lessThanOrEqual(300);
973+
});
938974
});
939975

940976
describe('#logMediaContentEnd', () => {
@@ -973,6 +1009,42 @@ describe('MediaSession', () => {
9731009
);
9741010
expect(bond.args[0][0].options.currentPlayheadPosition).to.eq(32);
9751011
});
1012+
1013+
it('should pause mediaContentTimeSpent', async () => {
1014+
const bond = sinon.spy(mp, 'logBaseEvent');
1015+
1016+
const options = {
1017+
customAttributes: {
1018+
content_rating: 'epic',
1019+
},
1020+
currentPlayheadPosition: 0,
1021+
};
1022+
1023+
// logPlay is triggered to start media content time tracking.
1024+
mpMedia.logPlay(options);
1025+
// 100ms delay added to account for the time spent on media content.
1026+
await new Promise(f => setTimeout(f, 100));
1027+
mpMedia.logMediaContentEnd(options);
1028+
// Another 100ms delay added after logMediaContentEnd is triggered to account for time spent on media session (total = +200ms).
1029+
await new Promise(f => setTimeout(f, 100));
1030+
mpMedia.logMediaSessionEnd(options);
1031+
1032+
// the 4th event in bond.args is the Media Session Summary which contains the mediaContentTimeSpent and mediaTimeSpent.
1033+
const mpMediaContentTimeSpent = bond.args[3][0].options.customAttributes.media_content_time_spent;
1034+
const mpMediaTimeSpent = bond.args[3][0].options.customAttributes.media_time_spent;
1035+
1036+
expect(mpMediaContentTimeSpent).to.not.eql(mpMediaTimeSpent);
1037+
1038+
// the mediaContentTimeSpent varies in value each test run by a millisecond or two (i,e value is could be 100ms, 101ms, 102ms)
1039+
// and we can't determine the exact value, hence the greaterThanOrEqual and lessThanOrEqual tests.
1040+
expect(mpMediaContentTimeSpent).to.greaterThanOrEqual(100);
1041+
expect(mpMediaContentTimeSpent).to.lessThanOrEqual(200);
1042+
1043+
// the mediaTimeSpent varies in value each test run by a millisecond or two (i,e value is could be 200ms, 201ms, 202ms)
1044+
// and we can't determine the exact value, hence the greaterThanOrEqual and lessThanOrEqual tests.
1045+
expect(mpMediaTimeSpent).to.greaterThanOrEqual(200);
1046+
expect(mpMediaTimeSpent).to.lessThanOrEqual(300);
1047+
});
9761048
});
9771049

9781050
describe('#logSegmentStart', () => {

0 commit comments

Comments
 (0)