You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
* or even a class with an `__invoke` method defined). The tag visitor callback is invoked by passing an instance
260
260
* of the `OD_Tag_Visitor_Context` object which includes the following read-only properties:
261
261
*
262
-
* - `$processor` (`OD_HTML_Tag_Processor`): The processor with the cursor at the current tag.
262
+
* - `$processor` (`OD_HTML_Tag_Processor`): The processor with the cursor at the current open tag.
263
263
* - `$url_metric_group_collection` (`OD_URL_Metric_Group_Collection`): The URL Metrics which may include information about the current tag to inform what optimizations the callback performs.
264
264
* - `$link_collection` (`OD_Link_Collection`): Collection of links which will be added to the `HEAD` when the page is served. This allows you to add preload links and preconnect links as needed.
265
+
* - `$url_metrics_id` (`positive-int|null`): The post ID for the `od_url_metrics` post from which the URL Metrics were loaded (if any). For advanced usage.
265
266
*
266
-
* Note that you are free to call `next_tag()` in the callback (such as to walk over any child elements) since the
267
-
* cursor will be reset to the tag after the callback finishes.
267
+
* Note that you are free to call `$processor->next_tag()` in the callback (such as to walk over any child elements)
268
+
* since the tag processor's cursor will be reset to the tag after the callback finishes.
268
269
*
269
-
* A tag visitor callback returns a boolean value. When it returns `true`, then Optimization Detective will mark the
270
-
* tag as needing to be included among the elements stored in URL Metrics. The element data includes properties such
271
-
* as intersectionRatio, intersectionRect, and boundingClientRect as well as whether it is the LCP element. If the
272
-
* current tag is not relevant for the tag visitor or if the tag visitor callback does not need to query the provided
273
-
* `OD_URL_Metric_Group_Collection` instance to apply the desired optimizations, it can just return `false`. An
274
-
* element will be tracked in URL Metrics if _any_ tag visitor callback returns `true` when visiting the tag.
270
+
* When a tag visitor sees it is at a relevant open tag (e.g. by checking `$processor->get_tag()`), it can call the
271
+
* `$context->track_tag()` method to indicate that the tag should be measured during detection. This will cause the
272
+
* tag to be included among the `elements` in the stored URL Metrics. The element data includes properties such
273
+
* as `intersectionRatio`, `intersectionRect`, and `boundingClientRect` (provided by an `IntersectionObserver`) as
274
+
* well as whether the tag is the LCP element (`isLCP`) or LCP element candidate (`isLCPCandidate`). This method
275
+
* should not be called if the current tag is not relevant for the tag visitor or if the tag visitor callback does
276
+
* not need to query the provided `OD_URL_Metric_Group_Collection` instance to apply the desired optimizations. (In
277
+
* addition to calling the `$context->track_tag()`, a callback may also return `true` to indicate the tag should be
278
+
* tracked.)
275
279
*
276
280
* Here's an example tag visitor that depends on URL Metrics data:
277
281
*
278
282
* $tag_visitor_registry->register(
279
283
* 'lcp-img-fetchpriority-high',
280
-
* static function ( OD_Tag_Visitor_Context $context ): bool {
284
+
* static function ( OD_Tag_Visitor_Context $context ): void {
281
285
* if ( $context->processor->get_tag() !== 'IMG' ) {
282
-
* return false; // Tag is not relevant for this tag visitor.
286
+
* return; // Tag is not relevant for this tag visitor.
283
287
* }
284
288
*
285
-
* // Make sure fetchpriority=high is added to LCP IMG elements.
289
+
* // Mark the tag for measurement during detection so it is included among the elements stored in URL Metrics.
290
+
* $context->track_tag();
291
+
*
292
+
* // Make sure fetchpriority=high is added to LCP IMG elements based on the captured URL Metrics.
0 commit comments