function GoogleAnalyticsBasicTest::testGoogleAnalyticsPageVisibility in Google Analytics 6.4
Same name and namespace in other branches
- 6.3 googleanalytics.test \GoogleAnalyticsBasicTest::testGoogleAnalyticsPageVisibility()
- 7.2 googleanalytics.test \GoogleAnalyticsBasicTest::testGoogleAnalyticsPageVisibility()
- 7 googleanalytics.test \GoogleAnalyticsBasicTest::testGoogleAnalyticsPageVisibility()
File
- ./
googleanalytics.test, line 47 - 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('//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', 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/settings/googleanalytics');
// Checking for tracking code URI here, as $ua_code is displayed in the form.
$this
->assertNoRaw('//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', 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('//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', 0);
// Enable tracking code for all user roles.
variable_set('googleanalytics_roles', array());
// Test whether 403 forbidden tracking code is shown if user has no access.
$this
->drupalGet('admin');
$this
->assertRaw('/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('/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.');
}