You are here

function GoogleAnalyticsBasicTest::testGoogleAnalyticsPageVisibility in Google Analytics 6.3

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

File

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

Class

GoogleAnalyticsBasicTest
@file Test file for Google Analytics module.

Code

function testGoogleAnalyticsPageVisibility() {
  $ua_code = 'UA-123456-1';
  variable_set('googleanalytics_account', $ua_code);

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

  // Disable tracking one "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/settings/googleanalytics');

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

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

  // Checking for tracking code URI here, as $ua_code is displayed in the form.
  $this
    ->assertRaw('google-analytics.com/ga.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', 0);

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

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

  // DNT Tests:
  // Enable caching of pages 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('_gaq.push(["_trackPageview"]);', '[testGoogleAnalyticsDNTVisibility]: DNT header send from client, but page caching is enabled and tracker cannot removed.');

  // DNT works only with caching of pages for anonymous users disabled.
  variable_set('cache', 0);
  $this
    ->drupalGet('');
  $this
    ->assertRaw('_gaq.push(["_trackPageview"]);', '[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('_gaq.push(["_trackPageview"]);', '[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('_gaq.push(["_trackPageview"]);', '[testGoogleAnalyticsDNTVisibility]: DNT feature is disabled, DNT header from browser has been ignored.');
}