Skip to content

Commit

Permalink
fix(FEC-7187): do not send time reached and seek events for live (#17)
Browse files Browse the repository at this point in the history
  • Loading branch information
yairans authored Oct 15, 2017
1 parent 316129a commit b24e311
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 18 deletions.
39 changes: 21 additions & 18 deletions src/kanalytics.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import Event from './event'

const pluginName = "kanalytics";
const SEEK_OFFSET: number = 2000;
const LIVE: string = 'Live';

/**
* @classdesc
Expand Down Expand Up @@ -132,12 +133,12 @@ export default class KAnalytics extends BasePlugin {
*/
_sendSeekAnalytic(): void {
let now = new Date().getTime();
if (this._lastSeekEvent === 0 || this._lastSeekEvent + SEEK_OFFSET < now) {
if ((this._lastSeekEvent + SEEK_OFFSET < now) && (this.player.config.type !== LIVE || this.player.config.dvr)) {
// avoid sending lots of seeking while scrubbing
this._sendAnalytics(EventTypes.SEEK);
this._hasSeeked = true;
}
this._lastSeekEvent = now;
this._hasSeeked = true;
}

/**
Expand All @@ -146,22 +147,24 @@ export default class KAnalytics extends BasePlugin {
* @return {void}
*/
_sendTimePercentAnalytic(): void {
let percent = this.player.currentTime / this.player.duration;
if (!this._timePercentEvent.PLAY_REACHED_25 && percent >= .25) {
this._timePercentEvent.PLAY_REACHED_25 = true;
this._sendAnalytics(EventTypes.PLAY_REACHED_25);
}
if (!this._timePercentEvent.PLAY_REACHED_50 && percent >= .50) {
this._timePercentEvent.PLAY_REACHED_50 = true;
this._sendAnalytics(EventTypes.PLAY_REACHED_50);
}
if (!this._timePercentEvent.PLAY_REACHED_75 && percent >= .75) {
this._timePercentEvent.PLAY_REACHED_75 = true;
this._sendAnalytics(EventTypes.PLAY_REACHED_75);
}
if (!this._timePercentEvent.PLAY_REACHED_100 && percent >= .98) {
this._timePercentEvent.PLAY_REACHED_100 = true;
this._sendAnalytics(EventTypes.PLAY_REACHED_100);
if (this.player.config.type !== LIVE) {
let percent = this.player.currentTime / this.player.duration;
if (!this._timePercentEvent.PLAY_REACHED_25 && percent >= .25) {
this._timePercentEvent.PLAY_REACHED_25 = true;
this._sendAnalytics(EventTypes.PLAY_REACHED_25);
}
if (!this._timePercentEvent.PLAY_REACHED_50 && percent >= .50) {
this._timePercentEvent.PLAY_REACHED_50 = true;
this._sendAnalytics(EventTypes.PLAY_REACHED_50);
}
if (!this._timePercentEvent.PLAY_REACHED_75 && percent >= .75) {
this._timePercentEvent.PLAY_REACHED_75 = true;
this._sendAnalytics(EventTypes.PLAY_REACHED_75);
}
if (!this._timePercentEvent.PLAY_REACHED_100 && percent >= .98) {
this._timePercentEvent.PLAY_REACHED_100 = true;
this._sendAnalytics(EventTypes.PLAY_REACHED_100);
}
}
}

Expand Down
42 changes: 42 additions & 0 deletions test/src/kanalytics.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,33 @@ describe('KAnalyticsPlugin', function () {
player.load();
});

it('should not send seek for live', (done) => {
player._config.type = 'Live';
player.addEventListener(player.Event.FIRST_PLAY, () => {
player.currentTime = player.duration / 2;
});
player.addEventListener(player.Event.SEEKED, () => {
let payload = sendSpy.lastCall.args[0];
payload.event.eventType.should.not.equal(17);
done();
});
player.play();
});

it('should send seek for live + dvr', (done) => {
player._config.type = 'Live';
player._config.dvr = true;
player.addEventListener(player.Event.FIRST_PLAY, () => {
player.currentTime = player.duration / 2;
});
player.addEventListener(player.Event.SEEKED, () => {
let payload = sendSpy.lastCall.args[0];
payload.event.eventType.should.equal(17);
done();
});
player.play();
});

it('should send buffer start', () => {
player.dispatchEvent({type: player.Event.PLAYER_STATE_CHANGED, payload:{
'newState': {
Expand Down Expand Up @@ -243,4 +270,19 @@ describe('KAnalyticsPlugin', function () {
});
player.load();
});

it('should not send 25% - 100% for live', (done) => {
player._config.type = 'Live';
let onTimeUpdate = () => {
player.removeEventListener(player.Event.TIME_UPDATE, onTimeUpdate);
let payload = sendSpy.lastCall.args[0];
payload.event.eventType.should.equal(2);
done();
};
player.addEventListener(player.Event.LOADED_DATA, () => {
player.addEventListener(player.Event.TIME_UPDATE, onTimeUpdate);
player.currentTime = 12.5;
});
player.load();
});
});

0 comments on commit b24e311

Please sign in to comment.