Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add post ID for the od_url_metrics post to the tag visitor context #1847

Merged
merged 8 commits into from
Feb 6, 2025

Conversation

westonruter
Copy link
Member

@westonruter westonruter commented Feb 5, 2025

This adds a $url_metrics_post_id property to the OD_Tag_Visitor_Context class for the sake of extensions which may be attaching postmeta to the od_url_metrics post during the od_url_metric_stored action. For example, the Optimization Detective Content Visibility plugin does this. The OD_URL_Metric_Store_Request_Context instance passed to the od_url_metric_stored action includes such a post_id, but the context passed to the tag visitors did not. This necessitated manually calculating the slug to obtain the post ID. By making the post ID available on the OD_Tag_Visitor_Context then this is greatly simplified, as seen in westonruter/od-content-visibility#1.

This PR also introduces a OD_URL_Metric_Group::get_collection() method to provide a way to access the private $collection variable. This parallels how OD_URL_Metric::get_group() provides access to the private $group variable a level lower.

This PR facilitates the ability for extensions to persist data outside of the URL Metric itself and in the postmeta of the od_url_metrics post type. This is required when data being collected in URL Metrics is not always available. For example, with Embed Optimizer, not every collected URL Metric will include the resized height for an embed since a user may not scroll down to see it in the viewport to cause it to be lazily-loaded. And in the case of Content Visibility, once an element has content-visibility applied, it is not longer able to be measured during detection unless, again, the user happens to scroll down to view the element that has CV applied. So we need to capture the initial height of the element in addition to the height of the element when users happen to scroll to it. Persisting this information in postmeta outside of URL Metrics is a way to do this. We should explore further if the core URL Metric construct should have a way to persist some data as new URL Metrics push out old ones, but in the meantime using postmeta as an ad hoc storage area is a way for us to experiment.

@westonruter westonruter added [Type] Enhancement A suggestion for improvement of an existing feature [Plugin] Optimization Detective Issues for the Optimization Detective plugin labels Feb 5, 2025
Copy link

codecov bot commented Feb 5, 2025

Codecov Report

Attention: Patch coverage is 98.63014% with 1 line in your changes missing coverage. Please review.

Project coverage is 66.21%. Comparing base (68c6deb) to head (145f89e).
Report is 16 commits behind head on trunk.

Files with missing lines Patch % Lines
plugins/optimization-detective/load.php 0.00% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##            trunk    #1847      +/-   ##
==========================================
+ Coverage   65.97%   66.21%   +0.23%     
==========================================
  Files          88       88              
  Lines        6895     6943      +48     
==========================================
+ Hits         4549     4597      +48     
  Misses       2346     2346              
Flag Coverage Δ
multisite 66.21% <98.63%> (+0.23%) ⬆️
single 37.90% <0.00%> (-0.27%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Copy link

github-actions bot commented Feb 5, 2025

The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the props-bot label.

If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message.

Co-authored-by: westonruter <[email protected]>
Co-authored-by: felixarntz <[email protected]>

To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook.

Copy link
Member

@felixarntz felixarntz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@westonruter This looks good to me, just some minor notes.

Comment on lines 51 to 60
/**
* Post ID for the od_url_metrics post which provided the URL Metrics in the collection.
*
* May be null if no post has been created yet.
*
* @since n.e.x.t
* @var positive-int|null
* @readonly
*/
public $url_metrics_post_id;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should omit the post from the name, as it can lead to confusion. Calling it url_metrics_id makes it clear it's the ID of the URL metrics, whereas if it contains "post", one could think this is referring to the post that lives at that URL.

It's okay to mention "post" when referring to it in combination with the od_url_metrics post type, because then it's obvious. But otherwise I'd refrain from it to avoid confusion.

Suggested change
/**
* Post ID for the od_url_metrics post which provided the URL Metrics in the collection.
*
* May be null if no post has been created yet.
*
* @since n.e.x.t
* @var positive-int|null
* @readonly
*/
public $url_metrics_post_id;
/**
* ID for the `od_url_metrics` post which provided the URL Metrics in the collection.
*
* May be null if no post has been created yet.
*
* @since n.e.x.t
* @var positive-int|null
* @readonly
*/
public $url_metrics_id;

Aside: Should it be url_metrics_id or url_metric_id?

Copy link
Member Author

@westonruter westonruter Feb 5, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It should definitely not be url_metric_id since if you're talking about a single URL Metric then you would be using its UUID. An od_url_metrics post always contains an array of URL Metrics, so this should be plural.

Copy link
Member Author

@westonruter westonruter Feb 5, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should omit the post from the name, as it can lead to confusion. Calling it url_metrics_id makes it clear it's the ID of the URL metrics, whereas if it contains "post", one could think this is referring to the post that lives at that URL.

I'm not sure. The od_url_metric_stored action includes a context object that references post_id. In that case the url_metrics_ prefix is not present, but that is more clear from context. For a tag visitor context, adding the prefix is needed because it wouldn't be clear from the tag visitor what the ID is referring to. But in both cases I thought it made sense to mention post_id. Probably the former should be updated to have the same as the latter for consistently. But back to the main point about whether to mention "post". I think it is helpful to mention "post" so that the developer understands what kind of ID is being referred to and what you can do with it. Namely, you can use it to access the post meta. If it's just a bare url_metrics_id then the developer would need to look deeper to figure out what it is exactly.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that it's a very common pattern in plugins to refer to this data as just "ID" (instead of "post ID") in combination with the plugin's specific purpose, so I'm not concerned that people wouldn't understand it's a post ID. Consider for example WooCommerce referring to a "product ID" - it's not saying "product post ID". It's so common that I think plugin developers would actually assume it's a post ID anyway. And by omitting the word "post" it's clear that it's the ID of the URL metrics themselves, not the post ID of the content in that URL.

Of course in theory it wouldn't hurt to include the word "post", but since URL metrics can also refer to a URL of a specific post, in this case that could be confusing. So IMO the downsides of using "URL metrics post ID" would be greater than the benefits.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've renamed url_metrics_post_id to url_metrics_id in deebfd7.

In 145f89e I've renamed OD_URL_Metric_Store_Request_Context::$post_id to OD_URL_Metric_Store_Request_Context::$url_metrics_id for consistency.

Copy link
Member

@felixarntz felixarntz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@westonruter LGTM!

One unrelated question, that we can discuss here and either discard or handle elsewhere.

* @property-read OD_URL_Metric_Group_Collection $url_metric_group_collection URL Metric group collection.
* @property-read OD_Link_Collection $link_collection Link collection.
* @property-read positive-int|null $url_metrics_id ID for the od_url_metrics post which provided the URL Metrics in the collection.
* @property-read OD_URL_Metric_Group_Collection $url_metrics_group_collection Deprecated alias for the $url_metric_group_collection property.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just seeing this now and I know it's not related to the change here, but why is url_metric_group better than url_metrics_group? In your previous comment, you mentioned that we should definitely use $url_metrics_id instead of $url_metric_id.

I get that it's fuzzy when referring to a group of that, but for better understanding, I would still find it more intuitive to use url_metrics_group, since each group also covers multiple URL metrics, and it's aligned with the url_metrics_id terminology.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It was originally OD_URL_Metrics_Group and OD_URL_Metrics_Group_Collection. This was undone in 942b397 as part of #1574.

In that PR description I had noted:

Aside: Perhaps OD_URL_Metrics_Group_Collection should be renamed to OD_URL_Metric_Group_Collection as well. The "metrics" plural here is redundant with the group in the name.

And you concurred:

+1 for that. Since it seems nobody other than us is adopting the plugin yet, this is probably fine to just rename. Once we document this further and encourage external usage, we'll need to be more careful about this kind of change.

For $url_metrics_id it should specifically be plural because it's referring to an instance of the od_url_metrics post type. If we went with plural then it would seem for consistency that it would have to be OD_URL_Metrics_Groups_Collection (with Groups also being plural). But the plural is not needed because the "group" and "collection" here both indicate that it is a plurality.

*
* @property-read WP_REST_Request<array<string, mixed>> $request Request.
* @property-read positive-int $url_metrics_id ID for the od_url_metrics post.
* @property-read OD_URL_Metric_Group_Collection $url_metric_group_collection URL Metric group collection.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See above.

@westonruter westonruter merged commit ab6b54e into trunk Feb 6, 2025
20 of 21 checks passed
@westonruter westonruter deleted the add/context-post-id branch February 6, 2025 17:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Plugin] Optimization Detective Issues for the Optimization Detective plugin [Type] Enhancement A suggestion for improvement of an existing feature
Projects
Status: Done 😃
Development

Successfully merging this pull request may close these issues.

2 participants