From 1d80444fd3d7c53b9b93c585dd0a14710cf6bb4a Mon Sep 17 00:00:00 2001 From: Daan van den Bergh <18595395+Dan0sz@users.noreply.github.com> Date: Wed, 30 Sep 2026 17:23:32 +0200 Subject: [PATCH] Load the tracker script async instead of deferred Deferred scripts run before DOMContentLoaded, so while the browser waits for a tracker script that can't be reached (e.g. a proxied script on a server that has gone offline), DOMContentLoaded doesn't fire and everything waiting for it stalls: menus, sliders, preloader overlays. Load it async, like Plausible's own snippet. The tracker handles loading before or after the inline plausible.init(): options set before it loads are picked up from plausible.o, and afterwards plausible.init is the tracker's own. Events sent before it has loaded are queued, as before. --- readme.txt | 3 +++ src/Compatibility.php | 8 ++++++- tests/integration/CompatibilityTest.php | 28 +++++++++++++++++++++++++ 3 files changed, 38 insertions(+), 1 deletion(-) create mode 100644 tests/integration/CompatibilityTest.php diff --git a/readme.txt b/readme.txt index c5b2d594..a44374c8 100644 --- a/readme.txt +++ b/readme.txt @@ -278,6 +278,9 @@ Please make sure you make a backup of your database before updating any version == Changelog == += 2.6.3 = +* Improved: the tracker script now loads asynchronously (like Plausible Analytics' own snippet) instead of deferred, so it can no longer hold up the rest of the page when it can't be reached. + = 2.6.2 = * *Important!* This release rebuilds the WooCommerce (and EDD) purchase funnel for sites using WPML with a translated product base. If you built your own funnels using the same goals, please rebuild them after installing this update. * Added: TranslatePress "different domain per language" (Multiple Domains) compatibility. Each language domain can be mapped to its own Plausible Analytics dashboard, just like WPML. diff --git a/src/Compatibility.php b/src/Compatibility.php index 5fa66217..ff761c76 100644 --- a/src/Compatibility.php +++ b/src/Compatibility.php @@ -113,9 +113,15 @@ public function exclude_from_cloudflare_rocket_loader( $tag, $handle ) { } /** + * async (like Plausible Analytics' own snippet) instead of defer: a deferred script holds up DOMContentLoaded + * until it has loaded, so a script that can't be reached (e.g. a proxied script on a server that's gone offline) + * stalls everything waiting for that event. The tracker handles loading before or after plausible.init(). + * * the data-cfasync ensures this script isn't processed by CF Rocket Loader @see https://developers.cloudflare.com/speed/optimization/content/rocket-loader/ignore-javascripts/ + * + * @since 2.6.3 async instead of defer. */ - $params = "defer data-cfasync='false'"; + $params = "async data-cfasync='false'"; $params = apply_filters( 'plausible_analytics_script_params', $params ); return str_replace( ' src', " {$params} src", $tag ); diff --git a/tests/integration/CompatibilityTest.php b/tests/integration/CompatibilityTest.php new file mode 100644 index 00000000..2e69e04f --- /dev/null +++ b/tests/integration/CompatibilityTest.php @@ -0,0 +1,28 @@ +exclude_from_cloudflare_rocket_loader( + '', + 'plausible-analytics' + ); + + $this->assertStringContainsString( " async data-cfasync='false'", $tag ); + $this->assertStringNotContainsString( 'defer', $tag ); + } +}