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 @@
+<?php
+
+/**
+ * @file
+ * Hooks specific to the Google Analytics module.
+ */
+
+/**
+ * @addtogroup hooks
+ * @{
+ */
+
+/**
+ * Alter $page_match to allow displaying Google Analytics JS code in the page.
+ *
+ * A module may not want to add the Google Analytics JS tracking code for its
+ * page and set $page_match to FALSE.
+ *
+ * @param bool $page_match
+ *   TRUE if the Google Analytics JS code should be added. FALSE otherwise.
+ */
+function hook_google_analytics_grants_alter(&$page_match) {
+  if (\Drupal::request()->query->has('no_ga')) {
+    $page_match = FALSE;
+  }
+}
+
+/**
+ * @} End of "addtogroup hooks".
+ */
diff --git a/google_analytics.module b/google_analytics.module
index 4004ed1..ce1fb77 100644
--- a/google_analytics.module
+++ b/google_analytics.module
@@ -642,6 +642,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 db69825..4f09312 100644
--- a/src/Tests/GoogleAnalyticsRolesTest.php
+++ b/src/Tests/GoogleAnalyticsRolesTest.php
@@ -22,7 +22,11 @@ class GoogleAnalyticsRolesTest extends WebTestBase {
    *
    * @var array
    */
-  public static $modules = ['google_analytics'];
+  public static $modules = [
+    'google_analytics',
+    'google_analytics_test',
+    'node',
+  ];
 
   /**
    * {@inheritdoc}
@@ -109,4 +113,19 @@ 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() {
+    $this->drupalGet('admin/content');
+
+    $this->createContentType(['type' => 'page']);
+    $node = $this->drupalCreateNode(['type' => 'page']);
+
+    $this->drupalGet('node/' . $node->id(), ['query' => ['magic_ga_disable' => '']]);
+    $this->assertNoRaw('www.google-analytics.com/analytics.js');
+    $this->drupalGet('node/' . $node->id());
+    $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 @@
+<?php
+/**
+ * @file
+ * Helper module for google_analytics test.
+ */
+
+/**
+ * Implements hook_google_analytics_visibility_pages().
+ *
+ * Depending on a specific active route, this function returns TRUE if JS code
+ * should be added to the current page, otherwise FALSE.
+ */
+function google_analytics_test_google_analytics_visibility_pages_alter(&$page_match) {
+  if ($page_match && \Drupal::request()->query->has('magic_ga_disable')) {
+    $page_match = FALSE;
+  }
+}
