From 813cab07fd8122f3fa50b0e16199ab6226d2ea9b Mon Sep 17 00:00:00 2001 From: Daan van den Bergh <18595395+Dan0sz@users.noreply.github.com> Date: Mon, 23 Mar 2026 18:58:37 +0100 Subject: [PATCH 1/4] Improved: Updated JS snippet --- src/Assets.php | 20 +++++--------------- 1 file changed, 5 insertions(+), 15 deletions(-) diff --git a/src/Assets.php b/src/Assets.php index aa9d7ba2..ed210510 100644 --- a/src/Assets.php +++ b/src/Assets.php @@ -32,10 +32,10 @@ private function init() { /** * Register main JS if this user should be tracked. * - * @return void - * @throws \Exception * @since 1.0.0 * @access public + * @return void + * @throws \Exception */ public function maybe_enqueue_main_script() { $settings = Helpers::get_settings(); @@ -44,7 +44,7 @@ public function maybe_enqueue_main_script() { /** * This is a dummy script that will allow us to attach inline scripts further down the line. */ - wp_register_script( 'plausible-analytics', '' ); + wp_register_script( 'plausible-analytics', $this->get_js_url( true ), [], null, apply_filters( 'plausible_load_js_in_footer', false ) ); /** * Bail if tracked_user_roles is empty (which means no roles should be tracked) or if the current role should not be tracked. @@ -53,19 +53,9 @@ public function maybe_enqueue_main_script() { return; // @codeCoverageIgnore } - wp_enqueue_script( - 'plausible-analytics', - '', - [], - null, - apply_filters( 'plausible_load_js_in_footer', false ) - ); + wp_enqueue_script( 'plausible-analytics' ); - $url = $this->get_js_url( true ); - $script = sprintf( - 'window.plausible=window.plausible||function(){(window.plausible.q=window.plausible.q||[]).push(arguments)},window.plausible.init=function(i){window.plausible.o=i||{}};var script=document.createElement("script");script.type="text/javascript",script.defer=!0,script.src="%s";var r=document.getElementsByTagName("script")[0];r.parentNode.insertBefore(script,r);', - $url - ); + $script = 'window.plausible=window.plausible||function(){(plausible.q=plausible.q||[]).push(arguments)},plausible.init=plausible.init||function(i){plausible.o=i||{}};'; $options = wp_json_encode( apply_filters( 'plausible_analytics_init_options', [] ) ); // transformRequest and customProperties can contain a JS function. $options = preg_replace( From 389d4a997c3fd59a3eea24142799d353edabb590 Mon Sep 17 00:00:00 2001 From: Daan van den Bergh <18595395+Dan0sz@users.noreply.github.com> Date: Tue, 24 Mar 2026 10:46:16 +0100 Subject: [PATCH 2/4] Improved: added data-no-minify attribute to script to make sure it's not optimized by optimization plugins which respect this attribute, i.e., WP Rocket and WP-Optimize. --- src/Compatibility.php | 39 ++++++++++++++++++++++++++++++--------- 1 file changed, 30 insertions(+), 9 deletions(-) diff --git a/src/Compatibility.php b/src/Compatibility.php index 1bef0f9e..622621d3 100644 --- a/src/Compatibility.php +++ b/src/Compatibility.php @@ -30,6 +30,9 @@ public function __construct() { // Cloudflare Rocket Loader add_filter( 'script_loader_tag', [ $this, 'exclude_from_cloudflare_rocket_loader' ], 10, 2 ); + // Global Ecxlusion from Minification + add_filter( 'plausible_analytics_script_params', [ $this, 'exclude_from_minification' ] ); + // LiteSpeed Cache if ( defined( 'LSCWP_V' ) ) { add_filter( 'litespeed_optimize_js_excludes', [ $this, 'exclude_plausible_js' ] ); @@ -83,6 +86,7 @@ public function __construct() { * @param mixed $exclude_js * * @return string + * @throws Exception */ public function exclude_plausible_js_as_string( $exclude_js ) { $exclude_js .= ', window.plausible, ' . Helpers::get_js_url( true ); @@ -93,13 +97,14 @@ public function exclude_plausible_js_as_string( $exclude_js ) { /** * Add Plausible Analytics attributes. * - * @param string $handle Script handle. - * @param string $tag Script tag. - * - * @return string * @since 1.0.0 * @access public * + * @param string $tag Script tag. + * + * @param string $handle Script handle. + * + * @return string */ public function exclude_from_cloudflare_rocket_loader( $tag, $handle ) { // Bail if it's not our script. @@ -107,8 +112,6 @@ public function exclude_from_cloudflare_rocket_loader( $tag, $handle ) { return $tag; // @codeCoverageIgnore } - $settings = Helpers::get_settings(); - /** * 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/ */ @@ -118,6 +121,22 @@ public function exclude_from_cloudflare_rocket_loader( $tag, $handle ) { return str_replace( ' src', " {$params} src", $tag ); } + /** + * Append a global no-minify attribute to the script tag, to make sure optimization plugins who respect this attribute don't minify our script. + * + * Tested on: + * - WP Rocket + * + * @param $params + * + * @return string + */ + public function exclude_from_minification( $params ) { + $params .= ' data-no-minify="true"'; + + return $params; + } + /** * Dear WP Rocket/SG Optimizer/Etc., don't minify/combine our inline JS, please. * @@ -129,7 +148,7 @@ public function exclude_from_cloudflare_rocket_loader( $tag, $handle ) { * @return array */ public function exclude_plausible_inline_js( $inline_js ) { - $inline_js[ 'plausible' ] = 'window.plausible'; + $inline_js['plausible'] = 'window.plausible'; return $inline_js; } @@ -144,6 +163,7 @@ public function exclude_plausible_inline_js( $inline_js ) { * @param array $excluded_js * * @return array + * @throws Exception */ public function exclude_plausible_js( $excluded_js ) { $excluded_js[] = Helpers::get_js_url( true ); @@ -181,6 +201,7 @@ function ( $tag ) { * @param array $excluded_js * * @return array + * @throws Exception */ public function exclude_plausible_js_by_relative_url( $excluded_js ) { $excluded_js[] = preg_replace( '/http[s]?:\/\/.*?(\/)/', '$1', Helpers::get_js_url( true ) ); @@ -213,7 +234,7 @@ public function exclude_by_proxy_endpoint( $excluded_js ) { * @filter rocket_minify_excluded_external_js * @since 1.2.5 * - * @param array $excluded_js + * @param $excluded_handles * * @return array */ @@ -239,7 +260,7 @@ public function exclude_js_by_handle( $excluded_handles ) { public function multilingual_compatibility( $url ) { $rest_endpoint = Helpers::get_rest_endpoint( false ); - if ( strpos( $url, $rest_endpoint ) !== false ) { + if ( str_contains( $url, $rest_endpoint ) ) { return get_option( 'home' ) . $rest_endpoint; } From 5d1eda7e3033ea78b480befb2ef9634242ac50b1 Mon Sep 17 00:00:00 2001 From: Daan van den Bergh <18595395+Dan0sz@users.noreply.github.com> Date: Tue, 24 Mar 2026 10:47:59 +0100 Subject: [PATCH 3/4] Typo. --- src/Compatibility.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Compatibility.php b/src/Compatibility.php index 622621d3..21a542ee 100644 --- a/src/Compatibility.php +++ b/src/Compatibility.php @@ -30,7 +30,7 @@ public function __construct() { // Cloudflare Rocket Loader add_filter( 'script_loader_tag', [ $this, 'exclude_from_cloudflare_rocket_loader' ], 10, 2 ); - // Global Ecxlusion from Minification + // Global Exclusion from Minification add_filter( 'plausible_analytics_script_params', [ $this, 'exclude_from_minification' ] ); // LiteSpeed Cache From a137b76fcf77120eddaea0cc79d5501eb5bde879 Mon Sep 17 00:00:00 2001 From: Daan van den Bergh <18595395+Dan0sz@users.noreply.github.com> Date: Tue, 24 Mar 2026 17:10:22 +0100 Subject: [PATCH 4/4] Improved: exclude JS from optimization using data attributes. --- src/Compatibility.php | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/src/Compatibility.php b/src/Compatibility.php index 21a542ee..2163ba8f 100644 --- a/src/Compatibility.php +++ b/src/Compatibility.php @@ -122,17 +122,25 @@ public function exclude_from_cloudflare_rocket_loader( $tag, $handle ) { } /** - * Append a global no-minify attribute to the script tag, to make sure optimization plugins who respect this attribute don't minify our script. + * Append attributes to the script tag, to make sure optimization plugins who respect this attribute don't minify our script. * - * Tested on: + * "data-no-minify" is respected by: * - WP Rocket + * - WP Optimize + * - WP Fastest Cache + * + * "data-no-optimize" is respected by: + * - LiteSpeed Cache + * + * "data-noptimize" is respected by: + * - Autoptimize * * @param $params * * @return string */ public function exclude_from_minification( $params ) { - $params .= ' data-no-minify="true"'; + $params .= ' data-no-minify="true" data-no-optimize="1" data-noptimize="1"'; return $params; }