Stop using IntersectionObserver polyfill in ads if native support is present
Now that stable Chrome has IntersectionObserver we should use it instead of the postMessage mechanism.
CC @lannka
Should we just leave it to 3p creative to handle? Or you are thinking about us doing it in integration.js?
I was thinking to do it in integration.js. The 3ps are unlikely to do the right thing across the board.
I noticed several differences between the polyfill and native impl:
- IntersectionObserverEntry.time in the spec is the elapsed time in ms since page load. Our polyfill uses Date.time()
- 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.
- 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.
- We're not even generating a
DOMHighResTimeStamp. - Maybe allow them to pass the thresholds they're interested in?
- I don't understand this change.
@jridgewell
performance.now()will give the value. It has a good browser coverage.- sure, we can allow a param passed in. To keep the backward compatibility, we can make the current behavior as default (no param given).
- which part? the problem or the proposed solution?
- With that, we don't need any relative stuff (
performance.now()is already) - Sure, that'd simplify some of the IntersectionObserver code
- The solution. Why install an observer if none is requested? Or anything else in it.
- 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. - 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.
Things become more straightforward now as we consolidated the behavior between native impl and polyfill due to @zhouyx 's work.
Assign to Yuxuan.
working on this. have two questions in advance.
- In the case of ad iframe resize, but
amp-addoesn't. Native impl and our polyfill could result in very different changeEntries. Are we good with this? - Since we will do our test in Chrome for most cases. Do we want to disable using native impl in test mode?
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 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?
@yieldmo-rao thanks for the reply. Can you further explain what do you use the scroll position for?
@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.
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.
@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.
hey @lannka is this change in production? We're currently seeing problems with our format animation that may be related to this change.
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.
Adding on to @harquail , all our client side reporting is also broken. :(
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?
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.