diff --git a/externs/shaka/ads.js b/externs/shaka/ads.js index f8b6ed27c..fb543b8a6 100644 --- a/externs/shaka/ads.js +++ b/externs/shaka/ads.js @@ -12,9 +12,11 @@ /** * @typedef {{ * loadTimes: !Array., + * averageLoadTime: number, * started: number, * playedCompletely: number, - * skipped: number + * skipped: number, + * errors: number * }} * * @description @@ -22,12 +24,16 @@ * * @property {number} loadTimes * The set of amounts of time it took to get the final manifest. + * @property {number} averageLoadTime + * The average time it took to get the final manifest. * @property {number} started * The number of ads started. * @property {number} playedCompletely * The number of ads played completely. * @property {number} skipped * The number of ads skipped. + * @property {number} errors + * The number of ads with errors. * @exportDoc */ shaka.extern.AdsStats; diff --git a/lib/ads/ad_manager.js b/lib/ads/ad_manager.js index c5cc0df74..d0c007aec 100644 --- a/lib/ads/ad_manager.js +++ b/lib/ads/ad_manager.js @@ -852,6 +852,9 @@ shaka.ads.AdManager = class extends shaka.util.FakeEventTarget { case shaka.ads.AdManager.AD_SKIPPED: this.stats_.incrementSkipped(); break; + case shaka.ads.AdManager.AD_ERROR: + this.stats_.incrementErrors(); + break; } } this.dispatchEvent(event); diff --git a/lib/ads/ads_stats.js b/lib/ads/ads_stats.js index 548dbe68d..dd7670870 100644 --- a/lib/ads/ads_stats.js +++ b/lib/ads/ads_stats.js @@ -24,6 +24,8 @@ shaka.ads.AdsStats = class { this.playedCompletely_ = 0; /** @private {number} */ this.skipped_ = 0; + /** @private {number} */ + this.errors_ = 0; } /** @@ -56,6 +58,26 @@ shaka.ads.AdsStats = class { this.skipped_++; } + /** + * Increase the number of ads with error by one. + */ + incrementErrors() { + this.errors_++; + } + + /** + * @return {number} + * @private + */ + getAverageLoadTime_() { + if (!this.loadTimes_.length) { + return 0; + } + const sum = this.loadTimes_.reduce( + (accumulator, currentValue) => accumulator + currentValue, 0); + return sum / this.loadTimes_.length; + } + /** * Create a stats blob that we can pass up to the app. This blob will not * reference any internal data. @@ -65,9 +87,11 @@ shaka.ads.AdsStats = class { getBlob() { return { loadTimes: this.loadTimes_, + averageLoadTime: this.getAverageLoadTime_(), started: this.started_, playedCompletely: this.playedCompletely_, skipped: this.skipped_, + errors: this.errors_, }; } }; diff --git a/ui/ad_statistics_button.js b/ui/ad_statistics_button.js index a49af8a40..aecd9c657 100644 --- a/ui/ad_statistics_button.js +++ b/ui/ad_statistics_button.js @@ -98,9 +98,11 @@ shaka.ui.AdStatisticsButton = class extends shaka.ui.Element { /** @private {!Object.} */ this.parseFrom_ = { 'loadTimes': parseLoadTimes, + 'averageLoadTime': showNumber, 'started': showNumber, 'playedCompletely': showNumber, 'skipped': showNumber, + 'errors': showNumber, }; /** @private {shaka.util.Timer} */ diff --git a/ui/ui.js b/ui/ui.js index a5bf07213..ea63d7617 100644 --- a/ui/ui.js +++ b/ui/ui.js @@ -219,9 +219,11 @@ shaka.ui.Overlay = class { ], adStatisticsList: [ 'loadTimes', + 'averageLoadTime', 'started', 'playedCompletely', 'skipped', + 'errors', ], contextMenuElements: [ 'loop',