Request: window.context is accessible from within a DFP iframe context
Our formats rely on the APIs provided by window.context, such as the intersection observer API, to do some internal processing. Without access to the APIs provided by window.context, we cannot serve our formats on a page.
Currently, when we are served through DFP, window.context does not exist within the context of DFPs iframe. Accessing window.parent.context results in a security exception and window.top is not allowed. Owing to that, we are currently unable to support serving through DFP on AMP pages.
/to @lannka
@ampproject/ads any chance you folks came across use cases wherein you needed access to window.context when serving through DFP?
Also, in the event that a network works as a mediation partner (and this might be a topic for a separate thread/issue), would standardizing passing around the window.context reference make sense?
@yieldmo-rao #3019 is being actively worked on to solve this issue. Let us know if that satisfies your usecase?
@jasti #3019 looks like it covers most use cases (assuming that intersectionObserver emulation is information apropos the top level document, and not one of the sub-documents). However, I see that isMaster will not be supported. We currently don't use it, but were planning to add support for it (to help with optimization and other data sharing amongst our formats).
So from my understanding:
In the event we're directly on page as a 3p, use window.context.
OR
If we detect (somehow) that we're a nested 3p, we use postMessageAPI?
Discussed with @lannka and were wondering if AMP could provide a single postMessage API for all creatives. @nekodo thoughts please?
That is what #3019 is about. I have already added a postMessage API for IntersectionObserver and page visibility and have PRs outstanding for resize and remaining context metadata (e.g. URLs). There is, by necessity, a little work required from the ad provider to pass some data to the creative, we already did that work for Glade for DFP. Basically, the postMessages are accepted by the AMP runtime if they carry the correct sentinel which is unique per 3P-iframe. The sentinel can be read from window.context.amp3pSentinel. Glade includes it in the fragment part of the ad iframe URL (i.e. #amp3pSentinel=123-456) and creatives can read it from there.
The same API can be used whether you are in a nested iframe or not so you will be able to standardise on a single implementation. The only difference might be where you find the amp3pSentinel value.
Unfortunately, master iframe is not something that can be supported by a postMessage-based API, as it relies on direct access to frames on the same domain and AMP runtime doing master election, which is non-trivial for nested x-domain frames. I'm not currently planning on implementing such API, interested parties are free to build support themselves (by crawling the window tree and electing their own master) or to contribute a standard implementation to AMP itself.
Once the remaining PRs are in I will document the postMessage API and we can write a JS wrapper around it if desired.
Thanks for that explanation, @nekodo . I think I might still have a gap in my understanding. From what I've got till now (and also by reading #3019), the logic flow will look something like this (subject to change based on how the #3019 PR shapes):
if window.context && window.context.amp3pSentinel, we are top level, code on page (not served via some other ad server), and we use the postMessageApi using the window.ampContext object.
Otherwise
if !window.context, we are being served via a 3p. Regardless, continue using the postMessageApi using the window.ampContext object.
This way, regardless of whether we're directly on the page, or being served through another third party, we can standardize the APIs and implementations via the postMessageApi.
Is that a correct assumption?
Roughly yes, with the following exceptions:
- ampContext (the new JS API) will need to be loaded using a separate script included by the creative.
- All the functionality of ampContext will also be accessible through sending postMessages to AMP runtime directly without a need to load extra scripts. This is already supported for IntersectionObserver and page visibility, and will be coming soon for resize as well.
What I can see here is that perhaps it's time I added some example pages with ads that use those APIs to make things a bit clearer;)
Ha! Fair enough. Thank you very much, @nekodo, for the explanation! Thank you other folks as well - I'm happy to close this issue if y'all are okay with it, since #3019 is tracking this.
Good discussion :-). I learned as well. Closing it for now, since I don't see any further action item in this thread now.