Skip to content

Commit 6df5f4e

Browse files
authored
Merge pull request #332 from plausible/hummingbird_compatibility
Exclude the plugin's scripts from Hummingbird's optimizations
2 parents 1d10b1a + 9870914 commit 6df5f4e

3 files changed

Lines changed: 77 additions & 0 deletions

File tree

‎readme.txt‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,7 @@ Please make sure you make a backup of your database before updating any version
280280

281281
= 2.6.3 =
282282
* 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.
283+
* Added: Hummingbird compatibility. Its Asset Optimization and Delay JavaScript no longer touch the plugin's scripts, which could cause a "plausible is not a function" error (e.g. on search results pages).
283284

284285
= 2.6.2 =
285286
* *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.

‎src/Compatibility.php‎

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,12 @@ public function __construct() {
3333
// Global Exclusion from Minification
3434
add_filter( 'plausible_analytics_script_params', [ $this, 'exclude_from_minification' ] );
3535

36+
// Hummingbird
37+
if ( defined( 'WPHB_VERSION' ) ) {
38+
add_filter( 'wphb_dont_add_handle_to_collection', [ $this, 'exclude_from_hummingbird_asset_optimization' ], 10, 4 );
39+
add_filter( 'wphb_delay_js_exclusions', [ $this, 'exclude_from_hummingbird_delay_js' ] );
40+
}
41+
3642
// LiteSpeed Cache
3743
if ( defined( 'LSCWP_V' ) ) {
3844
add_filter( 'litespeed_optimize_js_excludes', [ $this, 'exclude_plausible_js' ] );
@@ -151,6 +157,49 @@ public function exclude_from_minification( $params ) {
151157
return $params;
152158
}
153159

160+
/**
161+
* Dear Hummingbird, leave our scripts alone, please. Its Asset Optimization combines the tracker with other scripts
162+
* and moves (or duplicates) the inline script that initializes it, which can leave calls to plausible() without the
163+
* initialization ("plausible is not a function"), and serves its own copy of the tracker, which goes stale.
164+
*
165+
* Handles that aren't added to the collection are returned to WordPress untouched.
166+
*
167+
* @filter wphb_dont_add_handle_to_collection
168+
* @since 2.6.3
169+
*
170+
* @param bool $value Whether to leave the handle alone.
171+
* @param string $handle Resource handle.
172+
* @param string $source_url Resource URL.
173+
* @param string $type scripts|styles
174+
*
175+
* @return bool
176+
*/
177+
public function exclude_from_hummingbird_asset_optimization( $value, $handle, $source_url, $type ) {
178+
if ( $type === 'scripts' && strpos( (string) $handle, 'plausible-' ) === 0 ) {
179+
return true;
180+
}
181+
182+
return $value;
183+
}
184+
185+
/**
186+
* Dear Hummingbird, don't delay our scripts, please. The exclusions are matched against each script tag, including
187+
* its inline code, so this covers our handles (plausible-*), our URLs (plausible.io, /plugins/plausible-analytics/)
188+
* and the inline code calling window.plausible.
189+
*
190+
* @filter wphb_delay_js_exclusions
191+
* @since 2.6.3
192+
*
193+
* @param array $exclusions
194+
*
195+
* @return array
196+
*/
197+
public function exclude_from_hummingbird_delay_js( $exclusions ) {
198+
$exclusions[] = 'plausible';
199+
200+
return $exclusions;
201+
}
202+
154203
/**
155204
* Dear WP Rocket/SG Optimizer/Etc., don't minify/combine our inline JS, please.
156205
*

‎tests/integration/CompatibilityTest.php‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,31 @@ public function testTrackerScriptLoadsAsync() {
2525
$this->assertStringContainsString( " async data-cfasync='false'", $tag );
2626
$this->assertStringNotContainsString( 'defer', $tag );
2727
}
28+
29+
/**
30+
* Hummingbird's Asset Optimization should leave our scripts alone, and only ours.
31+
*
32+
* @see Compatibility::exclude_from_hummingbird_asset_optimization()
33+
* @return void
34+
*/
35+
public function testExcludeFromHummingbirdAssetOptimization() {
36+
$class = new Compatibility();
37+
38+
$this->assertTrue( $class->exclude_from_hummingbird_asset_optimization( false, 'plausible-analytics', '', 'scripts' ) );
39+
$this->assertTrue( $class->exclude_from_hummingbird_asset_optimization( false, 'plausible-form-submit-integration', '', 'scripts' ) );
40+
$this->assertFalse( $class->exclude_from_hummingbird_asset_optimization( false, 'jquery-core', '', 'scripts' ) );
41+
$this->assertFalse( $class->exclude_from_hummingbird_asset_optimization( false, 'plausible-analytics', '', 'styles' ) );
42+
// Another plugin's decision is kept.
43+
$this->assertTrue( $class->exclude_from_hummingbird_asset_optimization( true, 'jquery-core', '', 'scripts' ) );
44+
}
45+
46+
/**
47+
* Hummingbird's Delay JavaScript should leave our scripts alone.
48+
*
49+
* @see Compatibility::exclude_from_hummingbird_delay_js()
50+
* @return void
51+
*/
52+
public function testExcludeFromHummingbirdDelayJs() {
53+
$this->assertEquals( [ 'existing', 'plausible' ], ( new Compatibility() )->exclude_from_hummingbird_delay_js( [ 'existing' ] ) );
54+
}
2855
}

0 commit comments

Comments
 (0)