Apply AMP analytics ad tracking events to an ad without requiring a selector
Currently, AMP analytics tags for ads require a selector (e.g. "selector": "#anim-id"). However, the amp-ad's id isn't necessarily known when the analytics tags are generated. Instead of requiring a selector, when the analytics events are added to the page as the result of an ad request, they should apply to the ad with which they are served.
Along with that, when shadow-DOM isn't supported, the analytics tags need to be hoisted from the ad response and placed in the main AMP page, not the iframe. Then, the AMP runtime can correctly track the viewability of the ad and fire the events.
/cc @rudygalfi @kashyapnitin
I'm not sure I'm following this. Is it related to A4A? Can you provide an example page that demonstrates the difficultly with how amp-analytics currently works.
cc a bunch of people who may also be aware of the gap here @jasti @avimehta @cramforce
Yes, this is related to a4a. I don't have a sample page but I have been discussing with avimehta and he's aware of it.
- If I want to add analytics tags to a creative that I don't own/serve (e.g. 3P verification services), I don't know what the selector should be. In that case, the AMP runtime should be able to associate the analytics tag with the creative with which it is served.
- When shadow-DOM isn't supported, the creative is dropped into an iframe. My understanding is that there isn't an AMP runtime in that frame that can handle the analytics tags. In case case, the analytics tags will need to be lifted from the creative and added in the parent window where the runtime is present.
@michaelkleber FYI
I think we should probably discuss the hoisting separately.
The summary of the other feature request is basically:
Support a "closest" selector coming from the amp-analytics tag instead of a document level selector.
How about
{
"selector": "amp-ad",
"closest": true,
}
A selector targeting the closest amp-ad parent seems fine to me. I think that plays nicely with hoisting too, as long as we place the hoisted amp-analytics config right outside the iframe (but still within the enclosing amp-ad element).
@michaelkleber we can make closest inclusive :)
what about siblings? If the dom structure is something like:
<amp-ad id="#parent">
<amp-ad id="#before"></amp-ad>
<amp-analytics></amp-analytics>
<amp-ad id="#after"></amp-ads>
</amp-ad>
What would https://github.com/ampproject/amphtml/issues/4018#issuecomment-232508277 select in this case?
#parent. I don't think siblings are needed. We certainly don't support amp-ad nesting.
I added basic support for this in #4368. I couldn't think of what closest=false would do. So I skipped implementing the logic for closest completely. Here's my thinking:
We should make searching for closest element implicit. In future, if we want to expand the way searching for element is done, we can add something like searchMethod with possible values closest, first, and topmost.
Does that sound good?
ps: I need to wait before I can merge the PR because a couple of other PRs(mentioned in #4368) change the same codebase and are going to cause conflicts.
I agree with searchMethod = scope|closest or closest: true. These will map very clearly to the standard Element.querySelector and Element.closest APIs.