Skip to content

Stats: Deprioritize stats script with fetchpriority=low and remove dns-prefetch#47936

Open
kraftbj wants to merge 3 commits intotrunkfrom
enhance/stats-fetchpriority-low-HOG-123
Open

Stats: Deprioritize stats script with fetchpriority=low and remove dns-prefetch#47936
kraftbj wants to merge 3 commits intotrunkfrom
enhance/stats-fetchpriority-low-HOG-123

Conversation

@kraftbj
Copy link
Copy Markdown
Contributor

@kraftbj kraftbj commented Apr 3, 2026

Fixes #43631

Proposed changes

  • Add fetchpriority="low" to the Jetpack Stats script tag via the WP core wp_script_attributes filter, reducing network contention with LCP resources in Safari and Firefox (which don't automatically deprioritize async/defer scripts like Chrome does).
  • Remove the automatic dns-prefetch resource hint for stats.wp.com via the wp_resource_hints filter, since front-loading DNS resolution for a deliberately deprioritized resource is counterproductive.

Other information

This follows the approach from Weston Ruter's demo plugin and writeup. Uses the WP core wp_script_attributes filter (available since WP 6.1) instead of string-replacing on script_loader_tag.

Related product discussion/links

Does this pull request change what data or activity we track or use?

No. This only changes the priority/timing of the existing stats script loading — no changes to what is tracked.

Testing instructions

  • Activate Jetpack with Stats enabled on a test site.
  • Load a frontend page and inspect the HTML source.
  • Verify the jetpack-stats script tag includes fetchpriority="low".
  • Verify there is no <link rel="dns-prefetch" href="//stats.wp.com"> in the <head>.
  • Run the stats package unit tests: jp docker phpunit projects/packages/stats — all 105 tests should pass.

kraftbj added 3 commits April 3, 2026 10:47
… filter

Use the WP core wp_script_attributes filter to add fetchpriority=low to the
jetpack-stats script tag. This reduces network contention with LCP resources
in Safari and Firefox, which don't automatically deprioritize async/defer scripts.

See HOG-123
Remove the automatic dns-prefetch resource hint for stats.wp.com since we're
deprioritizing the stats script with fetchpriority=low. Front-loading DNS
resolution for a deliberately delayed resource is counterproductive.

See HOG-123
@kraftbj kraftbj added the [Status] Needs Review This PR is ready for review. label Apr 3, 2026
@kraftbj kraftbj self-assigned this Apr 3, 2026
@kraftbj kraftbj added the [Status] Needs Review This PR is ready for review. label Apr 3, 2026
@github-actions
Copy link
Copy Markdown
Contributor

github-actions bot commented Apr 3, 2026

Are you an Automattician? Please test your changes on all WordPress.com environments to help mitigate accidental explosions.

  • To test on WoA, go to the Plugins menu on a WoA dev site. Click on the "Upload" button and follow the upgrade flow to be able to upload, install, and activate the Jetpack Beta plugin. Once the plugin is active, go to Jetpack > Jetpack Beta, select your plugin (Jetpack or WordPress.com Site Helper), and enable the enhance/stats-fetchpriority-low-HOG-123 branch.
  • To test on Simple, run the following command on your sandbox:
bin/jetpack-downloader test jetpack enhance/stats-fetchpriority-low-HOG-123
bin/jetpack-downloader test jetpack-mu-wpcom-plugin enhance/stats-fetchpriority-low-HOG-123

Interested in more tips and information?

  • In your local development environment, use the jetpack rsync command to sync your changes to a WoA dev blog.
  • Read more about our development workflow here: PCYsg-eg0-p2
  • Figure out when your changes will be shipped to customers here: PCYsg-eg5-p2

@github-actions
Copy link
Copy Markdown
Contributor

github-actions bot commented Apr 3, 2026

Thank you for your PR!

When contributing to Jetpack, we have a few suggestions that can help us test and review your patch:

  • ✅ Include a description of your PR changes.
  • ✅ Add a "[Status]" label (In Progress, Needs Review, ...).
  • ✅ Add testing instructions.
  • ✅ Specify whether this PR includes any changes to data or privacy.
  • ✅ Add changelog entries to affected projects

This comment will be updated as you work on your PR and make changes. If you think that some of those checks are not needed for your PR, please explain why you think so. Thanks for cooperation 🤖


Follow this PR Review Process:

  1. Ensure all required checks appearing at the bottom of this PR are passing.
  2. Make sure to test your changes on all platforms that it applies to. You're responsible for the quality of the code you ship.
  3. You can use GitHub's Reviewers functionality to request a review.
  4. When it's reviewed and merged, you will be pinged in Slack to deploy the changes to WordPress.com simple once the build is done.

If you have questions about anything, reach out in #jetpack-developers for guidance!

@kraftbj
Copy link
Copy Markdown
Contributor Author

kraftbj commented Apr 3, 2026

Note: The wp_resource_hints filter to remove the dns-prefetch for stats.wp.com is needed because wp_dependencies_unique_hosts() doesn't check fetchpriority data when building dns-prefetch hints — the two systems don't talk to each other yet. Weston flagged this as a TODO in his demo plugin (wp_dependencies_unique_hosts() could omit adding any resource hints for dependencies with low priority). Once core addresses that gap, our filter becomes a harmless no-op.

@jp-launch-control
Copy link
Copy Markdown

Code Coverage Summary

Coverage changed in 1 file.

File Coverage Δ% Δ Uncovered
projects/packages/stats/src/class-tracking-pixel.php 100/132 (75.76%) 0.97% 2 ❤️‍🩹

Full summary · PHP report · JS report

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR improves frontend performance for Jetpack Stats by lowering the network priority of the Stats script and avoiding an early DNS lookup that would work against that deprioritization.

Changes:

  • Add fetchpriority="low" to the jetpack-stats script tag via the wp_script_attributes filter.
  • Remove the automatic dns-prefetch hint for stats.wp.com via the wp_resource_hints filter.
  • Add unit tests for the new attribute/hint-filtering helpers and include a changelog fragment.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 3 comments.

File Description
projects/packages/stats/src/class-tracking-pixel.php Adds the new filters to set low fetch priority on the Stats script and remove the stats.wp.com DNS prefetch hint.
projects/packages/stats/tests/php/Tracking_Pixel_Test.php Adds tests covering the new helper callbacks for script attributes and resource hints.
projects/packages/stats/changelog/enhance-stats-fetchpriority-low-HOG-123 Documents the behavioral change in the Stats package changelog.

$this->assertContains( '//stats.wp.com', $result );
}

/**
Copy link

Copilot AI Apr 3, 2026

Choose a reason for hiding this comment

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

Current tests validate the helper callbacks directly, but they don't verify that enqueue_stats_script() actually registers the wp_script_attributes / wp_resource_hints filters. Adding an integration-style test that calls enqueue_stats_script() and asserts has_filter(...) (and/or runs apply_filters(...)) would prevent regressions where the new behavior is accidentally not hooked up.

Suggested change
/**
/**
* Test that enqueue_stats_script registers the expected filters and they run when applied.
*/
public function test_enqueue_stats_script_registers_filters() {
$tracking_pixel = new Tracking_Pixel();
remove_filter( 'wp_script_attributes', array( Tracking_Pixel::class, 'add_low_fetchpriority' ), 10 );
remove_filter( 'wp_resource_hints', array( Tracking_Pixel::class, 'remove_stats_dns_prefetch' ), 10 );
$tracking_pixel->enqueue_stats_script();
$this->assertNotFalse(
has_filter( 'wp_script_attributes', array( Tracking_Pixel::class, 'add_low_fetchpriority' ) )
);
$this->assertNotFalse(
has_filter( 'wp_resource_hints', array( Tracking_Pixel::class, 'remove_stats_dns_prefetch' ) )
);
$script_attributes = apply_filters(
'wp_script_attributes',
array(
'id' => 'jetpack-stats-js',
'src' => 'https://stats.wp.com/e-202445.js',
)
);
$this->assertSame( 'low', $script_attributes['fetchpriority'] );
$resource_hints = apply_filters(
'wp_resource_hints',
array( '//stats.wp.com', '//example.com' ),
'dns-prefetch'
);
$this->assertNotContains( '//stats.wp.com', $resource_hints );
$this->assertContains( '//example.com', $resource_hints );
remove_filter( 'wp_script_attributes', array( Tracking_Pixel::class, 'add_low_fetchpriority' ), 10 );
remove_filter( 'wp_resource_hints', array( Tracking_Pixel::class, 'remove_stats_dns_prefetch' ), 10 );
}
/**

Copilot uses AI. Check for mistakes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Enhancement: Improve performance by deprioritizing Stats script

2 participants