GoogleChrome / web-vitals

Essential metrics for a healthy site.
https://web.dev/vitals
Apache License 2.0
7.47k stars 410 forks source link

Save INP target after interactions to reduce null values when removed from the DOM #477

Closed tunetheweb closed 4 months ago

tunetheweb commented 4 months ago

Makes progress on #335

When a target element is removed from the DOM it cannot be looked up to get the CSS selector.

There are instances when the first-input and pointer-down events provide a target element, but the later pointerup and click events do not as the element has been removed. This is even more apparent in v4 where we wait to attribute INP until idle.

This PR saves a target reference for each event with an interaction id, so it can be retrieved later if the target is then null.

This will not help if the element is removed before the first event entry from that interactionId is reported so is not a full solution, but will help in some cases.

gilbertococchi commented 4 months ago

Thanks for fixing this Barry! Lots of Target nulls due to CMP UI Click should go away with this change!

tunetheweb commented 4 months ago

Don't get too excited just yet. I'm having dropping making this work in the general case. Not sure how it works on your site to be honest :-(

gilbertococchi commented 4 months ago

Don't get too excited just yet. I'm having dropping making this work in the general case. Not sure how it works on your site to be honest :-(

Thanks Barry, you know me well :)

I just did test out this change on few CMP scenarios and it seems work correctly, happy to test out more scenarios as well today.

tunetheweb commented 4 months ago

@philipwalton with the latest change to keep the element, I think it makes sense to expose this as requested in #456 so I added that in https://github.com/GoogleChrome/web-vitals/pull/477/commits/663461df255f0a17f91cba26626e3d0654fb4f18. Technically another change to the API but as it's an addition I don't think we need another release candidate for this. WDYT?