Skip to content

Commit

Permalink
Revert timeout for tracking
Browse files Browse the repository at this point in the history
  • Loading branch information
mimcz committed Dec 4, 2024
1 parent 701e73e commit e56903c
Showing 1 changed file with 45 additions and 49 deletions.
94 changes: 45 additions & 49 deletions src/ts/ConvivaAnalyticsTracker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,15 +111,14 @@ export interface EventAttributes {

export class ConvivaAnalyticsTracker {
private static readonly VERSION: string = '{{VERSION}}';

public static readonly AD_BREAK_FINISHED_DELAY_MS = 250;
public static readonly STALL_TRACKING_DELAY_MS = 100;

private _player: PlayerAPI;

private get player(): PlayerAPI {
if (!this._player) {
throw new Error('Player is not initialized, either pass it to the constructor or attach it via `attachPlayer` before using the integration.');
throw new Error(
'Player is not initialized, either pass it to the constructor or attach it via `attachPlayer` before using the integration.',
);
}
return this._player;
}
Expand Down Expand Up @@ -174,7 +173,7 @@ export class ConvivaAnalyticsTracker {
}

public attachPlayer(player: PlayerAPI): void {
const {canAttach} = this.canAttachPlayer(player);
const { canAttach } = this.canAttachPlayer(player);

if (!canAttach) {
return;
Expand Down Expand Up @@ -277,7 +276,10 @@ export class ConvivaAnalyticsTracker {

public initializeSession(): void {
if (this.isSessionActive()) {
this.logger.consoleLog('[ ConvivaAnalyticsTracker ] There is already a session running.', Conviva.SystemSettings.LogLevel.WARNING);
this.logger.consoleLog(
'[ ConvivaAnalyticsTracker ] There is already a session running.',
Conviva.SystemSettings.LogLevel.WARNING,
);
return;
}

Expand Down Expand Up @@ -488,7 +490,10 @@ export class ConvivaAnalyticsTracker {
this.setPlayerInfo();
// It's required to correctly track VST. There must be BUFFERING or STOPPED metric reported before PLAYING.
// In some cases BUFFERING does not fire before PLAYING, so we report STOPPED right after session initialization to cover all edge cases.
this.convivaVideoAnalytics.reportPlaybackMetric(Conviva.Constants.Playback.PLAYER_STATE, Conviva.Constants.PlayerState.STOPPED);
this.convivaVideoAnalytics.reportPlaybackMetric(
Conviva.Constants.Playback.PLAYER_STATE,
Conviva.Constants.PlayerState.STOPPED,
);

this.convivaVideoAnalytics.setCallback(() => {
if (!this.isPlayerAttached) {
Expand Down Expand Up @@ -526,7 +531,9 @@ export class ConvivaAnalyticsTracker {
*/
private buildContentMetadata() {
if (!this.isPlayerAttached) {
this.debugLog('[ ConvivaAnalyticsTracker ] Player is not attached, skipping default content metadata initialization, it will be initialized on source loaded event');
this.debugLog(
'[ ConvivaAnalyticsTracker ] Player is not attached, skipping default content metadata initialization, it will be initialized on source loaded event',
);
return;
}

Expand Down Expand Up @@ -644,7 +651,6 @@ export class ConvivaAnalyticsTracker {
this.stallTrackingTimeout.clear();
}


if (playerState) {
if (this._isAdBreakActive) {
this.debugLog('[ ConvivaAnalyticsTracker ] report ad playback state', playerState);
Expand Down Expand Up @@ -733,33 +739,35 @@ export class ConvivaAnalyticsTracker {
this.debugLog('[ ConvivaAnalyticsTracker ] report ad break started', { type });
this.convivaVideoAnalytics.reportAdBreakStarted(
type,
type === Conviva.Constants.AdType.CLIENT_SIDE ? Conviva.Constants.AdPlayer.SEPARATE : Conviva.Constants.AdPlayer.CONTENT,
type === Conviva.Constants.AdType.CLIENT_SIDE
? Conviva.Constants.AdPlayer.SEPARATE
: Conviva.Constants.AdPlayer.CONTENT,
);
};

public trackAdStarted = (adInfo: Conviva.ConvivaMetadata, type: Conviva.valueof<Conviva.ConvivaConstants['AdType']>, bitrateKbps?: number) => {
// Clear the timeout that may have been scheduled by a previous ad finished event, as the ad break is not actually over yet.
this.adBreakFinishedTimeout.clear();

public trackAdStarted = (
adInfo: Conviva.ConvivaMetadata,
type: Conviva.valueof<Conviva.ConvivaConstants['AdType']>,
bitrateKbps?: number,
) => {
if (!this.isSessionActive()) {
return;
}

if (!this.isAdBreakActive) {
// If no ad break is active, it must mean that the `adBreakFinishedTimeout` ran before AdStarted was emitted.
// Then we need to report this as the start of a new ad break.
this.trackAdBreakStarted(type);
}

this.debugLog('[ ConvivaAnalyticsTracker ] report ad started', {
adInfo,
type,
bitrateKbps,
});
this.convivaAdAnalytics.reportAdStarted(adInfo);

this.debugLog(`[ ConvivaAnalyticsTracker ] report ${PlayerStateHelper.getPlayerState(this.player)} ad playback state`);
this.convivaAdAnalytics.reportAdMetric(Conviva.Constants.Playback.PLAYER_STATE, PlayerStateHelper.getPlayerState(this.player));
this.debugLog(
`[ ConvivaAnalyticsTracker ] report ${PlayerStateHelper.getPlayerState(this.player)} ad playback state`,
);
this.convivaAdAnalytics.reportAdMetric(
Conviva.Constants.Playback.PLAYER_STATE,
PlayerStateHelper.getPlayerState(this.player),
);

if (type === Conviva.Constants.AdType.SERVER_SIDE) {
const playbackVideoData = this.player.getPlaybackVideoData();
Expand All @@ -770,15 +778,27 @@ export class ConvivaAnalyticsTracker {

if (playbackVideoData.frameRate) {
this.debugLog('[ ConvivaAnalyticsTracker ] report framerate', playbackVideoData.frameRate);
this.convivaAdAnalytics.reportAdMetric(Conviva.Constants.Playback.RENDERED_FRAMERATE, playbackVideoData.frameRate);
this.convivaAdAnalytics.reportAdMetric(
Conviva.Constants.Playback.RENDERED_FRAMERATE,
playbackVideoData.frameRate,
);
}
}

if (bitrateKbps) {
this.debugLog('[ ConvivaAnalyticsTracker ] report ad bitrate', bitrateKbps);
this.convivaAdAnalytics.reportAdMetric(Conviva.Constants.Playback.BITRATE, bitrateKbps);
}
}
};

public trackAdFinished = () => {
if (!this.isSessionActive()) {
return;
}

this.debugLog('[ ConvivaAnalyticsTracker ] report ad ended');
this.convivaAdAnalytics.reportAdEnded();
};

public trackAdSkipped = () => {
if (!this.isSessionActive()) {
Expand All @@ -789,39 +809,15 @@ export class ConvivaAnalyticsTracker {
this.convivaAdAnalytics.reportAdSkipped();
};

public trackAdFinished = () => {
public trackAdBreakFinished = () => {
if (!this.isSessionActive()) {
return;
}

this.debugLog('[ ConvivaAnalyticsTracker ] report ad ended');
this.convivaAdAnalytics.reportAdEnded();

// Start timer to report ad break finished, as waiting for the event will cause VST to be too low.
this.adBreakFinishedTimeout.start();
}

private reportAdBreakEnded = () => {
this._isAdBreakActive = false;

this.debugLog('[ ConvivaAnalyticsTracker ] report ad break ended');
this.convivaVideoAnalytics.reportAdBreakEnded();
}

private adBreakFinishedTimeout = new Timeout(ConvivaAnalyticsTracker.AD_BREAK_FINISHED_DELAY_MS, this.reportAdBreakEnded);

public trackAdBreakFinished = () => {
// Clear the timeout to prevent the ad break finished event from being reported twice
this.adBreakFinishedTimeout.clear();

if (!this.isSessionActive()) {
return;
}

if (this.isAdBreakActive) {
// If ad break is still active, it must mean that the event was faster than the `adBreakFinishedTimeout`
this.reportAdBreakEnded();
}

this.debugLog(`[ ConvivaAnalyticsTracker ] report ${PlayerStateHelper.getPlayerState(this.player)} playback state`);
this.convivaVideoAnalytics.reportPlaybackMetric(
Expand Down

0 comments on commit e56903c

Please sign in to comment.