You are here

function GoogleAnalyticsBasicTest::testGoogleAnalyticsTracking in Google Analytics 6

Same name and namespace in other branches
  1. 6.2 tests/googleanalytics_basic.test \GoogleAnalyticsBasicTest::testGoogleAnalyticsTracking()

File

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

Class

GoogleAnalyticsBasicTest
@file Test file for Google Analytics module.

Code

function testGoogleAnalyticsTracking() {

  // Set visibility to hide tracking code on admin page only,
  // track authenticated users.
  variable_set('googleanalytics_visibility', 0);
  variable_set('googleanalytics_pages', 'admin*');
  variable_set('googleanalytics_roles', array(
    DRUPAL_AUTHENTICATED_RID => DRUPAL_AUTHENTICATED_RID,
  ));
  $ua_code = 'UA-123456-7';
  variable_set('googleanalytics_account', $ua_code);

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

  /* Sample JS code as added to page:
     <script type="text/javascript" src="/sites/all/modules/google_analytics/googleanalytics.js?w"></script>
     <script type="text/javascript">var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));</script>
     <script type="text/javascript">var pageTracker = _gat._getTracker("UA-123456-7");pageTracker._trackPageview();</script>
     */

  // Test whether tracking code uses latest JS.
  variable_set('googleanalytics_cache', 0);
  variable_set('googleanalytics_legacy_version', 0);
  $this
    ->drupalGet('');
  $this
    ->assertRaw('google-analytics.com/ga.js', '[testGoogleAnalyticsTracking]: Latest tracking code used.');

  // Test whether tracking code uses legacy JS.
  variable_set('googleanalytics_legacy_version', 1);
  $this
    ->drupalGet('');
  $this
    ->assertRaw('google-analytics.com/urchin.js', '[testGoogleAnalyticsTracking]: Legacy tracking code used.');

  // Test whether tracking code is not included on pages to omit.
  $this
    ->drupalGet('admin');
  $this
    ->assertNoRaw($ua_code, '[testGoogleAnalyticsTracking]: 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/urchin.js', '[testGoogleAnalyticsTracking]: 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, '[testGoogleAnalyticsTracking]: 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/urchin.js', '[testGoogleAnalyticsTracking]: Tracking code is displayed on admin subpage.');
  $this
    ->drupalGet('');
  $this
    ->assertNoRaw($ua_code, '[testGoogleAnalyticsTracking]: Tracking code is not displayed on front page.');

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