diff --git a/google_analytics.api.php b/google_analytics.api.php new file mode 100644 index 0000000..661d3cc --- /dev/null +++ b/google_analytics.api.php @@ -0,0 +1,30 @@ +query->has('no_ga')) { + $page_match = FALSE; + } +} + +/** + * @} End of "addtogroup hooks". + */ diff --git a/google_analytics.module b/google_analytics.module index b1c8c1d..290f1de 100644 --- a/google_analytics.module +++ b/google_analytics.module @@ -652,6 +652,8 @@ function _google_analytics_visibility_pages() { $page_match = TRUE; } + // Allow modules to alter whether Google Analytics JS code should be added. + \Drupal::moduleHandler()->alter('google_analytics_visibility_pages', $page_match); } return $page_match; } diff --git a/src/Tests/GoogleAnalyticsRolesTest.php b/src/Tests/GoogleAnalyticsRolesTest.php index f0390ae..1ed64eb 100644 --- a/src/Tests/GoogleAnalyticsRolesTest.php +++ b/src/Tests/GoogleAnalyticsRolesTest.php @@ -17,7 +17,11 @@ class GoogleAnalyticsRolesTest extends WebTestBase { * * @var array */ - public static $modules = ['google_analytics']; + public static $modules = [ + 'google_analytics', + 'google_analytics_test', + 'node', + ]; /** * {@inheritdoc} @@ -104,4 +108,24 @@ class GoogleAnalyticsRolesTest extends WebTestBase { $this->assertRaw($ua_code, '[testGoogleAnalyticsRoleVisibility]: Tracking code is displayed on frontpage for included anonymous users.'); } + /** + * Tests if the tracking visibility works for a specific route. + */ + public function testGoogleAnalyticsVisibilityTracking() { + $ua_code = 'UA-123456-4'; + $this->config('google_analytics.settings')->set('account', $ua_code)->save(); + + $this->drupalGet('admin/content'); + + $this->createContentType(['type' => 'page']); + $node = $this->drupalCreateNode(['type' => 'page']); + + $this->drupalGet('node/' . $node->id(), ['query' => ['magic_ga_disable' => NULL]]); + $this->assertNoRaw($ua_code); + $this->assertNoRaw('www.google-analytics.com/analytics.js'); + $this->drupalGet('node/' . $node->id()); + $this->assertRaw($ua_code); + $this->assertRaw('www.google-analytics.com/analytics.js'); + } + } diff --git a/tests/modules/google_analytics_test/google_analytics_test.info.yml b/tests/modules/google_analytics_test/google_analytics_test.info.yml new file mode 100644 index 0000000..0b8ac30 --- /dev/null +++ b/tests/modules/google_analytics_test/google_analytics_test.info.yml @@ -0,0 +1,4 @@ +type: module +name: Google Analytics Test +description: 'Required for Google Analytics simpletests only.' +core: 8.x diff --git a/tests/modules/google_analytics_test/google_analytics_test.module b/tests/modules/google_analytics_test/google_analytics_test.module new file mode 100644 index 0000000..9936b07 --- /dev/null +++ b/tests/modules/google_analytics_test/google_analytics_test.module @@ -0,0 +1,17 @@ +query->has('magic_ga_disable')) { + $page_match = FALSE; + } +}