More improvements to the google-ima shim script (#3908)

We have enabled the google-ima shim script again in the DuckDuckGo
Privacy Essentials browser extension, and found a couple more issues:
- Some websites set the enablePreloading[1] option, which should
  cause[2] the AdsManager.init() method to trigger the LOADED AdEvent
  to fire. If the event doesn't fire, those websites can get stuck
  waiting for the event forever.
- When AdsManager.start() method is called, a bunch of events are
  dispatched in order, to simulate ads loading, playing and
  finishing. There was a mistake in that logic though. The
  CONTENT_PAUSE_REQUESTED and CONTENT_RESUME_REQUESTED events[3]
  should fire as the ads start and finish respectively. By firing the
  latter early, and skipping the former, some websites got confused
  and tried to display ad overlays at the same time as playing their
  content, or didn't display they content at all.

1 - https://developers.google.com/interactive-media-ads/docs/sdks/html5/client-side/reference/js/google.ima.AdsRenderingSettings#enablePreloading
2 - https://developers.google.com/interactive-media-ads/docs/sdks/html5/client-side/preload#timing
3 - https://developers.google.com/interactive-media-ads/docs/sdks/html5/client-side/reference/js/google.ima.AdEvent
This commit is contained in:
Dave Vandyke 2023-11-16 14:41:07 +00:00 committed by GitHub
parent 8df6c3243c
commit c1d8f5908d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -13,6 +13,8 @@
* - Added missing event dispatcher functionality
* - Corrected return type of `Ad.getUniversalAdIds()`
* - Corrected typo in `UniversalAdIdInfo.getAdIdValue()` method name
* - Corrected dispatch of LOAD event when preloading is enabled
* - Corrected dispatch of CONTENT_PAUSE/RESUME_REQUESTED events
*
* Related issue:
* - https://github.com/uBlockOrigin/uBlock-issues/issues/2158
@ -412,6 +414,7 @@ if (!window.google || !window.google.ima || !window.google.ima.VERSION) {
constructor() {
super();
this.volume = 1;
this._enablePreloading = false;
}
collapse() {}
configureAdsManager() {}
@ -437,7 +440,11 @@ if (!window.google || !window.google.ima || !window.google.ima.VERSION) {
getVolume() {
return this.volume;
}
init(/*w, h, m, e*/) {}
init(/*w, h, m, e*/) {
if (this._enablePreloading) {
this._dispatch(new ima.AdEvent(AdEvent.Type.LOADED));
}
}
isCustomClickTrackingUsed() {
return false;
}
@ -457,13 +464,14 @@ if (!window.google || !window.google.ima || !window.google.ima.VERSION) {
for (const type of [
AdEvent.Type.LOADED,
AdEvent.Type.STARTED,
AdEvent.Type.CONTENT_RESUME_REQUESTED,
AdEvent.Type.CONTENT_PAUSE_REQUESTED,
AdEvent.Type.AD_BUFFERING,
AdEvent.Type.FIRST_QUARTILE,
AdEvent.Type.MIDPOINT,
AdEvent.Type.THIRD_QUARTILE,
AdEvent.Type.COMPLETE,
AdEvent.Type.ALL_ADS_COMPLETED,
AdEvent.Type.CONTENT_RESUME_REQUESTED,
]) {
try {
this._dispatch(new ima.AdEvent(type));
@ -743,7 +751,10 @@ if (!window.google || !window.google.ima || !window.google.ima.VERSION) {
constructor(type) {
this.type = type;
}
getAdsManager() {
getAdsManager(c, settings) {
if (settings && settings.enablePreloading) {
manager._enablePreloading = true;
}
return manager;
}
getUserRequestContext() {