Skip to content

Commit a7d6b18

Browse files
authored
Merge pull request #248 from plausible/ie_compatibility
Improved: only rewrite JS script's ID attribute if IE compatibility is enabled.
2 parents e1b25a1 + 08f583a commit a7d6b18

File tree

3 files changed

+27
-2
lines changed

3 files changed

+27
-2
lines changed

src/Filters.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,11 @@ public function add_plausible_attributes( $tag, $handle ) {
5151
$api_url = Helpers::get_data_api_url();
5252
$domain_name = Helpers::get_domain();
5353

54-
// We need the correct id attribute for IE compatibility.
55-
$tag = preg_replace( "/\sid=(['\"])plausible-analytics-js(['\"])/", " id=$1plausible$2", $tag );
54+
if ( Helpers::is_enhanced_measurement_enabled( 'compat' ) ) {
55+
// We need the correct id attribute for IE compatibility.
56+
$tag = preg_replace( "/\sid=(['\"])plausible-analytics-js(['\"])/", " id=$1plausible$2", $tag );
57+
}
58+
5659
/**
5760
* 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/
5861
*/

tests/TestCase.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,19 @@ public function enableRevenue( $settings ) {
3636
return $settings;
3737
}
3838

39+
/**
40+
* Enable Enhanced Measurements > IE Compatibility
41+
*
42+
* @param $settings
43+
*
44+
* @return mixed
45+
*/
46+
public function enableCompat( $settings ) {
47+
$settings[ 'enhanced_measurements' ] = [ 'compat' ];
48+
49+
return $settings;
50+
}
51+
3952
/**
4053
* Enable form completions by modifying the settings array.
4154
*

tests/integration/FiltersTest.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,15 @@ public function testAddPlausibleAttributes() {
1818

1919
$this->assertStringContainsString( 'example.org', $tag );
2020
$this->assertStringContainsString( 'plausible.io/api/event', $tag );
21+
$this->assertStringContainsString( 'plausible-analytics-js', $tag );
22+
23+
add_filter( 'plausible_analytics_settings', [ $this, 'enableCompat' ] );
24+
25+
$class = new Filters();
26+
$tag = $class->add_plausible_attributes( '<script id="plausible-analytics-js" src="test.js">', 'plausible-analytics' );
27+
28+
remove_filter( 'plausible_analytics_settings', [ $this, 'enableCompat' ] );
29+
2130
$this->assertStringNotContainsString( 'plausible-analytics-js', $tag );
2231
}
2332
}

0 commit comments

Comments
 (0)