You are here

public function GoogleAnalyticsBasicTest::testGoogleAnalyticsPageVisibility in Google Analytics 4.x

Same name and namespace in other branches
  1. 8.3 tests/src/Functional/GoogleAnalyticsBasicTest.php \Drupal\Tests\google_analytics\Functional\GoogleAnalyticsBasicTest::testGoogleAnalyticsPageVisibility()
  2. 8.2 tests/src/Functional/GoogleAnalyticsBasicTest.php \Drupal\Tests\google_analytics\Functional\GoogleAnalyticsBasicTest::testGoogleAnalyticsPageVisibility()

Tests if page visibility works.

File

tests/src/Functional/GoogleAnalyticsBasicTest.php, line 135

Class

GoogleAnalyticsBasicTest
Test basic functionality of Google Analytics module.

Namespace

Drupal\Tests\google_analytics\Functional

Code

public function testGoogleAnalyticsPageVisibility() {

  // Verify that no tracking code is embedded into the webpage; if there is
  // only the module installed, but UA code not configured. See #2246991.
  $this
    ->drupalGet('');
  $this
    ->assertNoRaw('https://www.googletagmanager.com/gtag/js?id=');
  $ua_code = 'UA-123456-1';
  $this
    ->config('google_analytics.settings')
    ->set('account', $ua_code)
    ->save();

  // Show tracking on "every page except the listed pages".
  $this
    ->config('google_analytics.settings')
    ->set('visibility.request_path_mode', 0)
    ->save();

  // Disable tracking on "admin*" pages only.
  $this
    ->config('google_analytics.settings')
    ->set('visibility.request_path_pages', "/admin\n/admin/*")
    ->save();

  // Enable tracking only for authenticated users only.
  $this
    ->config('google_analytics.settings')
    ->set('visibility.user_role_roles', [
    AccountInterface::AUTHENTICATED_ROLE => AccountInterface::AUTHENTICATED_ROLE,
  ])
    ->save();

  // Check tracking code visibility.
  $this
    ->drupalGet('');
  $this
    ->assertRaw('gtag("config", "' . $ua_code . '"');

  // Test whether tracking code is not included on pages to omit.
  $this
    ->drupalGet('admin');
  $this
    ->assertNoRaw($ua_code);
  $this
    ->drupalGet('admin/config/services/google-analytics');

  // Checking for tracking URI here, as $ua_code is displayed in the form.
  $this
    ->assertNoRaw('https://www.googletagmanager.com/gtag/js?id=');

  // Test whether tracking code display is properly flipped.
  $this
    ->config('google_analytics.settings')
    ->set('visibility.request_path_mode', 1)
    ->save();
  $this
    ->drupalGet('admin');
  $this
    ->assertRaw($ua_code);
  $this
    ->drupalGet('admin/config/services/google-analytics');

  // Checking for tracking URI here, as $ua_code is displayed in the form.
  $this
    ->assertRaw('https://www.googletagmanager.com/gtag/js?id=');
  $this
    ->drupalGet('');
  $this
    ->assertNoRaw($ua_code);

  // Test whether tracking code is not display for anonymous.
  $this
    ->drupalLogout();
  $this
    ->drupalGet('');
  $this
    ->assertNoRaw($ua_code);

  // Switch back to every page except the listed pages.
  $this
    ->config('google_analytics.settings')
    ->set('visibility.request_path_mode', 0)
    ->save();

  // Enable tracking code for all user roles.
  $this
    ->config('google_analytics.settings')
    ->set('visibility.user_role_roles', [])
    ->save();
}