Render <amp-analytics> within <amp-ad> for A4A in the parent doc instead of the shadow dom/iframe
Short description of your issue:
When an A4A ad is returned, it's cut off from the parent doc either by using a shadow DOM or an iframe. This prevents
When rendering, the analytics tag should be separated from the ad content and written outside of the shadow DOM/iframe.
How do we reproduce the issue?
Serve an A4A with an
What browsers are affected?
All browsers
Which AMP version is affected?
All
/cc @avimehta @tdrl
@dvoytenko Currently I use closestByTag to get the closest parent element with a particular tag name. The call does not work across shadowDOM boundary. For example, in a tree like the following, <amp-ad-body>.__AMP__RESOURCE is null so amp-ad-body can't be used as a selector and <amp-ad> is not never found by element.closest() call.
<amp-ad>
#shadowRoot
<amp-ad-body>
<amp-analytics>
I think the solution is to detect if the element is in shadowDOM and cross the boundary if needed. An alternative solution could be for amp-ad rendering to hoist amp-analytics outside the amp-ad and place it next to it. preference?
Yes, our goal is to hoist the <amp-analytics> tag out. This shouldn't be too hard if the Validation server can provide us a bit of metadata about its location within the creative. /cc @Gregable for comments on feasibility of that?
Assuming that they can do so, what is your goal state for the DOM in your example? Something like this?
<amp-analytics>
<!-- amp-analytics config info -->
</amp-analytics>
<amp-ad>
#shadowRoot
<amp-ad-body>
<!-- creative body content -->
</amp-ad-body>
</amp-ad>
Forgive me for knowing very little about analytics, but: how does it know which of the (potentially many) amp-ads on the page it should apply to in this case? Also, if the analytics tag appears both places (inside and outside the shadow DOM), is that problematic? (Affects whether we can copy from the creative or have to do surgery on it.)
Also, what about the valid-AMP-rendered-in-iframe case? (This happens when the browser doesn't support shadow DOM.) The issue request suggests hoisting the analytics tag out of the iframe altogether. Is that the right thing to do in that case, or can it run happily inside the iframe? Note that in this case, the creative will be a fully-formed AMP (more precisely, A4A) document, with its own html and body tags.
Could we not place the amp-analytics tag within the amp-ad element to ensure it knows which amp-ad is the proper one to which it should listen? It should be able to co-exist with the shadowRoot.
Not if the shadow root is on the amp-ad tag itself, as it currently is. IIUC, shadow roots hide / suppress the DOM tree below their host element. (Though, I guess I don't know for sure whether those tags would execute in their tree, even though it is suppressed for display and other DOM operations.)
Yes, I think it should be within the amp-ad element.
I'm currently seeing:
<amp-ad...>
#shadow-root
<style amp-runtime...>
<style amp-custom...>
<amp-ad-body...>
...
<amp-analytics...>...
</amp-ad-body>
</amp-ad>
It's not that hard to go over the shadow root boundary. We were planning to always use open shadow roots (and even if we didn't, it's still easy enough). We just need to make sure that we don't do this by accident and start triggering wrong analytics requests. But if we keep amp-analytics inside the ad, that should not happen, I don't think.
I personally agree with many of the comments in the thread. It would be preferable to cross shadow boundary instead of moving the tag itself. Moving the tag itself will cause confusion in terms of selectors to use. Also, if the ad itself comes with amp-analytics tag and is selecting something within the tag, moving the tag will break this.
Will send out a PR to enable crossing of shadow boundary.
@tdrl To answer your question, amp-analytics allows people to select an element for viewability measurements. The selection happens using two properties:
'selector': <tag name or element Id>
'selectionMethod': <undefined or 'closest' or 'scope'>
when the selectionMethod is closest, the search is limited to the ancestors that satisfy the selector property. When the selectionMethod is 'scope', only elements within the parent are searched. When a selectionMethod is not provided, just the selector is used. More details about this are in extensions/amp-analytics/amp-analytics.md. Examples are in examples/analytics.amp.html.
Is shadow DOM supported in all browsers today? Or will the change also cross the friendly-iframe boundary?
Shadow DOM is supported only in Chrome at the moment, AFAIK. But Safari support is in the works and coming soon. Right now, I'm seeing about an equal number of "with shadow DOM" and "without shadow DOM" queries from A4A.
And I should have clarified: it's not a friendly iframe in the "render w/o shadow DOM" case. We load the creative from cache via <iframe src="original request URL">, making it a cross-domain iframe.
I think shadow dom/iframe is a good question. How does the same analytics tag would work in iframe case? Also, can someone provide a good example of the analytics tag in this case?
I'm having trouble getting a live rendering with Chrome caching but here's roughly how it would look.
<amp-ad...>
<iframe src="..">
<html... >
<head...>... </head>
<body... >
..... <!-- ad content -->
<amp-analytics>...</amp-analytics>
</body>
</html>
</iframe>
</amp-ad>```
cc/ @rudygalfi
@j-e-shaw Ok, the example looks good, but could you elaborate a bit on this and show the minimal amp-analytics setup that should go over the boundary to amp-ad tag? I'm still trying to get a very good idea, given that an Ad is essentially an independent document, what's the right expectations for event propagation here.
Here's a sample analytics block:
<amp-analytics>
<script type="application/json">
{
"transport": {"beacon": false, "xhrpost": false},
"requests": {
"visibility1": "..."
},
"triggers": {
"continuousVisible": {
"on": "visible",
"request": "visibility1",
"visibilitySpec": {
"selector": "amp-ad",
"selectionMethod": "closest",
"visiblePercentageMin": 50,
"continuousTimeMin": 1000
}
}
}
}
</script>
</amp-analytics>
So if I'm following right, the desire is that we not hoist the analytics tag out, regardless of whether the ad is rendered in shadow DOM or iframe, yes? In that case, is there anything for A4A to do, or is this an AMP/AMP-Analytics wiring question entirely?
Yes, I'd definitely like to avoid hoisting out and I'd say this is certainly b/w runtime and analytics and a4a just happens to be a first customer of something like this.
As for the format, I'm a bit concerned about configuring boundary traversal this way b/c:
- It invites mistakes where we'd look for the wrongly set selectors in the parent DOM.
- It potentially could go too far by trying to traverse even outside of
amp-adand getting possibly more information than we'd expect. - I'd like to ensure that a4a documents stand on their own as much as possible as AMP documents, that only happen to be embedded normally.
What I'm wondering if we it'd be better to further borrow into CSS spec and support this use case either as :root or :host selector.
/cc @cramforce
Is there any path around hoisting when we have to fallback to 3p iframes? I guess that might be out of scope (no pun intended) here.
Direct support for simple forms of :host seem like a good idea, but I'm not sure we can really avoid amp-analytics being aware of being inside of A4A and managing is specifically.
I think the right thing here is for analytics package to be smart enough and look for 3p intersection events we pass in the window.context when :host selector is specified.
Is there any timeline on a resolution? Thanks