You are here

function GoogleAnalyticsBasicTest::testGoogleAnalyticsPageVisibility in Google Analytics 7.2

Same name and namespace in other branches
  1. 6.4 googleanalytics.test \GoogleAnalyticsBasicTest::testGoogleAnalyticsPageVisibility()
  2. 6.3 googleanalytics.test \GoogleAnalyticsBasicTest::testGoogleAnalyticsPageVisibility()
  3. 7 googleanalytics.test \GoogleAnalyticsBasicTest::testGoogleAnalyticsPageVisibility()

File

./googleanalytics.test, line 82
Test file for Google Analytics module.

Class

GoogleAnalyticsBasicTest
@file Test file for Google Analytics module.

Code

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.google-analytics.com/analytics.js', '[testGoogleAnalyticsPageVisibility]: Tracking code is not displayed without UA code configured.');
  $ua_code = 'UA-123456-1';
  variable_set('googleanalytics_account', $ua_code);

  // Show tracking on "every page except the listed pages".
  variable_set('googleanalytics_visibility_pages', 0);

  // Disable tracking on "admin*" pages only.
  variable_set('googleanalytics_pages', "admin\nadmin/*");

  // Enable tracking only for authenticated users only.
  variable_set('googleanalytics_roles', array(
    DRUPAL_AUTHENTICATED_RID => DRUPAL_AUTHENTICATED_RID,
  ));

  // Check tracking code visibility.
  $this
    ->drupalGet('');
  $this
    ->assertRaw($ua_code, '[testGoogleAnalyticsPageVisibility]: Tracking code is displayed for authenticated users.');

  // Test whether tracking code is not included on pages to omit.
  $this
    ->drupalGet('admin');
  $this
    ->assertNoRaw($ua_code, '[testGoogleAnalyticsPageVisibility]: Tracking code is not displayed on admin page.');
  $this
    ->drupalGet('admin/config/system/googleanalytics');

  // Checking for tracking code URI here, as $ua_code is displayed in the form.
  $this
    ->assertNoRaw('https://www.google-analytics.com/analytics.js', '[testGoogleAnalyticsPageVisibility]: Tracking code is not displayed on admin subpage.');

  // Test whether tracking code display is properly flipped.
  variable_set('googleanalytics_visibility_pages', 1);
  $this
    ->drupalGet('admin');
  $this
    ->assertRaw($ua_code, '[testGoogleAnalyticsPageVisibility]: Tracking code is displayed on admin page.');
  $this
    ->drupalGet('admin/config/system/googleanalytics');

  // Checking for tracking code URI here, as $ua_code is displayed in the form.
  $this
    ->assertRaw('https://www.google-analytics.com/analytics.js', '[testGoogleAnalyticsPageVisibility]: Tracking code is displayed on admin subpage.');
  $this
    ->drupalGet('');
  $this
    ->assertNoRaw($ua_code, '[testGoogleAnalyticsPageVisibility]: Tracking code is NOT displayed on front page.');

  // Test whether tracking code is not display for anonymous.
  $this
    ->drupalLogout();
  $this
    ->drupalGet('');
  $this
    ->assertNoRaw($ua_code, '[testGoogleAnalyticsPageVisibility]: Tracking code is NOT displayed for anonymous.');

  // Switch back to every page except the listed pages.
  variable_set('googleanalytics_visibility_pages', 0);

  // Enable tracking code for all user roles.
  variable_set('googleanalytics_roles', array());
  $base_path = base_path();

  // Test whether 403 forbidden tracking code is shown if user has no access.
  $this
    ->drupalGet('admin');
  $this
    ->assertRaw($base_path . '403.html', '[testGoogleAnalyticsPageVisibility]: 403 Forbidden tracking code shown if user has no access.');

  // Test whether 404 not found tracking code is shown on non-existent pages.
  $this
    ->drupalGet($this
    ->randomName(64));
  $this
    ->assertRaw($base_path . '404.html', '[testGoogleAnalyticsPageVisibility]: 404 Not Found tracking code shown on non-existent page.');

  // DNT Tests:
  // Enable system internal page cache for anonymous users.
  variable_set('cache', 1);

  // Test whether DNT headers will fail to disable embedding of tracking code.
  $this
    ->drupalGet('', array(), array(
    'DNT: 1',
  ));
  $this
    ->assertRaw('ga("send", "pageview");', '[testGoogleAnalyticsDNTVisibility]: DNT header send from client, but page caching is enabled and tracker cannot removed.');

  // DNT works only with system internal page cache for anonymous users disabled.
  variable_set('cache', 0);
  $this
    ->drupalGet('');
  $this
    ->assertRaw('ga("send", "pageview");', '[testGoogleAnalyticsDNTVisibility]: Tracking is enabled without DNT header.');

  // Test whether DNT header is able to remove the tracking code.
  $this
    ->drupalGet('', array(), array(
    'DNT: 1',
  ));
  $this
    ->assertNoRaw('ga("send", "pageview");', '[testGoogleAnalyticsDNTVisibility]: DNT header received from client. Tracking has been disabled by browser.');

  // Disable DNT feature and see if tracker is still embedded.
  variable_set('googleanalytics_privacy_donottrack', 0);
  $this
    ->drupalGet('', array(), array(
    'DNT: 1',
  ));
  $this
    ->assertRaw('ga("send", "pageview");', '[testGoogleAnalyticsDNTVisibility]: DNT feature is disabled, DNT header from browser has been ignored.');
}