amphtml icon indicating copy to clipboard operation
amphtml copied to clipboard

Stop using IntersectionObserver polyfill in ads if native support is present

Open cramforce opened this issue 10 years ago • 37 comments

Now that stable Chrome has IntersectionObserver we should use it instead of the postMessage mechanism.

CC @lannka

cramforce avatar Oct 14 '16 00:10 cramforce

Should we just leave it to 3p creative to handle? Or you are thinking about us doing it in integration.js?

lannka avatar Oct 14 '16 00:10 lannka

I was thinking to do it in integration.js. The 3ps are unlikely to do the right thing across the board.

cramforce avatar Oct 14 '16 00:10 cramforce

I noticed several differences between the polyfill and native impl:

  1. IntersectionObserverEntry.time in the spec is the elapsed time in ms since page load. Our polyfill uses Date.time()
  2. Our polyfill triggers whenever the intersection has a change, and throttled to 10/s. The native impl only triggers when the intersection ratio cross some predefined thresholds.
  3. As shown in the README, our polyfill also provides the initial intersection (on page load). However, the native impl only triggers when it has intersection. So it doesn't trigger if the iframe is BTF on page load.

When a listener is registered, it will be called 2x in short order. Once with the position of the ad when its iframe was created and then again with the current position.

The value window.context.initialIntersection contains the initial intersection record at the time the iframe was created.

For 1), shall we change the polyfill? Or shall we compensate to the native impl? I prefer the latter, but not sure if anyone is relying on it. For 2), I suggest we specify a fine grained threshold array as [0. 0.05, 0.10, ..., 0.95, 1]. To make sure it gets triggered on every 5%. For 3), we can install the IntersectionObserver on page load (no matter a listener will be registered or not. assume this is cheap), and approximate the initial intersection to the 1st intersection record if it's received within 500ms. Otherwise, the initial intersection is considered as 0. Once a listener get registered, we will send the initial record and also the previous record to mimic the "2x in short order" behavior.

lannka avatar Oct 20 '16 05:10 lannka

  1. We're not even generating a DOMHighResTimeStamp.
  2. Maybe allow them to pass the thresholds they're interested in?
  3. I don't understand this change.

jridgewell avatar Oct 20 '16 19:10 jridgewell

@jridgewell

  1. performance.now() will give the value. It has a good browser coverage.
  2. sure, we can allow a param passed in. To keep the backward compatibility, we can make the current behavior as default (no param given).
  3. which part? the problem or the proposed solution?

lannka avatar Oct 20 '16 20:10 lannka

  1. With that, we don't need any relative stuff (performance.now() is already)
  2. Sure, that'd simplify some of the IntersectionObserver code
  3. The solution. Why install an observer if none is requested? Or anything else in it.

jridgewell avatar Oct 20 '16 20:10 jridgewell

  1. it's to provide the initialIntersection. I reconsidered this, my proposal won't work. We have to pass the info from host doc via URL fragment.
  2. the starting time is different though. in host doc, it's relative to the page load. inside iframe, it's relative to the iframe load. though most likely the creative only cares about the duration, it might still be good to consolidate. 3) requires the time to be relative to page load. That means we will intercept the InOb and subtract an offset.

lannka avatar Oct 20 '16 21:10 lannka

Things become more straightforward now as we consolidated the behavior between native impl and polyfill due to @zhouyx 's work.

Assign to Yuxuan.

lannka avatar Nov 29 '16 00:11 lannka

working on this. have two questions in advance.

  1. In the case of ad iframe resize, but amp-ad doesn't. Native impl and our polyfill could result in very different changeEntries. Are we good with this?
  2. Since we will do our test in Chrome for most cases. Do we want to disable using native impl in test mode?

zhouyx avatar Dec 06 '16 02:12 zhouyx

Reopen this because native IntersectionObserver doesn't give rootBounds inside x-origin iframe. Some ad networks are currently relying on it:

Taboola @nitzanvolman Teads @RonanDrouglazet Yieldmo @yieldmo-rao Kargo @andyrusiecki

Are you able to make changes to not relying on this value?

lannka avatar Dec 15 '16 00:12 lannka

@lannka We use rootBounds to get the scroll position of the top window. In normal environments, we do window.top, but since we do not have access to that, we use rootBounds. We don't mind switching it out, but what is the alternative API you would recommend to get the window's current scroll position at any given point in time?

ghost avatar Dec 15 '16 17:12 ghost

@yieldmo-rao thanks for the reply. Can you further explain what do you use the scroll position for?

lannka avatar Dec 15 '16 17:12 lannka

@lannka , we use it for calculating a bunch of metrics that we report back to our server. Additionally, many of our formats are interactive (parallax effects based on scroll position), so that calculation requires us to know where we are on the screen currently.

ghost avatar Dec 15 '16 17:12 ghost

Oh, forgot to add; in addition to the "push" based model that is used with event listeners (we get updates when the user is actively scrolling), we also need to be able to "pull" the position at any given point in time (when the user may not be actively scrolling, but the format is on the screen), We only do this once or twice, but I do know that we definitely need that ability as well. With window.scrollX and scrollY, we always have that data. We did that in AMP by saving the latest intersection upon getting a scroll event.

ghost avatar Dec 15 '16 17:12 ghost

@lannka We're also going for a deployment freeze until after the new year. Owing to that, I hope that this is not something that will be changed/updated before the end of year.

ghost avatar Dec 16 '16 17:12 ghost

hey @lannka is this change in production? We're currently seeing problems with our format animation that may be related to this change.

harquail avatar Jan 03 '17 16:01 harquail

Specifically it looks like we are only getting events on intersection with the viewport, rather than on scroll as with the native intersection observer (we rely on these for format positioning). I can open a new issue once I've done more investigation.

harquail avatar Jan 03 '17 16:01 harquail

Adding on to @harquail , all our client side reporting is also broken. :(

ghost avatar Jan 03 '17 16:01 ghost

We are not using native intersection observer now. But our polyfill may be a breaking change if you rely on intersection observer for scroll event. we polyfill the behavior of native intersection observer and only send intersection change entry when ratio cross thresholds. (will not send on every scroll). @lannka Should we revert the change?

zhouyx avatar Jan 03 '17 17:01 zhouyx

Ok that seems like the cause of our issue. We need a way to get our actual scroll position on the page — and ideally notifications of position change. We're open to using another API for this if available, but this is currently a breaking change for us.

harquail avatar Jan 03 '17 17:01 harquail