You are here

function GoogleAnalyticsBasicTest::testGoogleAnalyticsTrackingCode in Google Analytics 6.4

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

File

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

Class

GoogleAnalyticsBasicTest
@file Test file for Google Analytics module.

Code

function testGoogleAnalyticsTrackingCode() {
  $ua_code = 'UA-123456-2';
  variable_set('googleanalytics_account', $ua_code);

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

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

  /* Sample JS code as added to page:
     <script type="text/javascript" src="/sites/all/modules/google_analytics/googleanalytics.js?w"></script>
     <script>
     (function(q,u,i,c,k){window['GoogleAnalyticsObject']=q;
     window[q]=window[q]||function(){(window[q].q=window[q].q||[]).push(arguments)},
     window[q].l=1*new Date();c=i.createElement(u),k=i.getElementsByTagName(u)[0];
     c.async=true;c.src='//www.google-analytics.com/analytics.js';
     k.parentNode.insertBefore(c,k)})('ga','script',document);
     ga('create', 'UA-123456-7');
     ga('send', 'pageview');
     </script>
     <!-- End Google Analytics -->
     */

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

  // Test whether anonymize visitors IP address feature has been enabled.
  variable_set('googleanalytics_tracker_anonymizeip', 0);
  $this
    ->drupalGet('');
  $this
    ->assertNoRaw('ga("set", "anonymizeIp", true);', '[testGoogleAnalyticsTrackingCode]: Anonymize visitors IP address not found on frontpage.');

  // Enable anonymizing of IP addresses.
  variable_set('googleanalytics_tracker_anonymizeip', 1);
  $this
    ->drupalGet('');
  $this
    ->assertRaw('ga("set", "anonymizeIp", true);', '[testGoogleAnalyticsTrackingCode]: Anonymize visitors IP address found on frontpage.');

  // Test if track Enhanced Link Attribution is enabled.
  variable_set('googleanalytics_tracklinkid', 1);
  $this
    ->drupalGet('');
  $this
    ->assertRaw('ga("require", "linkid", "linkid.js");', '[testGoogleAnalyticsTrackingCode]: Tracking code for Enhanced Link Attribution is enabled.');

  // Test if track Enhanced Link Attribution is disabled.
  variable_set('googleanalytics_tracklinkid', 0);
  $this
    ->drupalGet('');
  $this
    ->assertNoRaw('ga("require", "linkid", "linkid.js");', '[testGoogleAnalyticsTrackingCode]: Tracking code for Enhanced Link Attribution is not enabled.');

  // Test if tracking of User ID is enabled.
  variable_set('googleanalytics_trackuserid', 1);
  $this
    ->drupalGet('');
  $this
    ->assertRaw(', { "cookieDomain": "auto", "userId": "', '[testGoogleAnalyticsTrackingCode]: Tracking code for User ID is enabled.');

  // Test if tracking of User ID is disabled.
  variable_set('googleanalytics_trackuserid', 0);
  $this
    ->drupalGet('');
  $this
    ->assertNoRaw(', { "cookieDomain": "auto", "userId": "', '[testGoogleAnalyticsTrackingCode]: Tracking code for User ID is disabled.');

  // Test if tracking of url fragments is enabled.
  variable_set('googleanalytics_trackurlfragments', 1);
  $this
    ->drupalGet('');
  $this
    ->assertRaw('ga("set", "page", location.pathname + location.search + location.hash);', '[testGoogleAnalyticsTrackingCode]: Tracking code for url fragments is enabled.');

  // Test if tracking of url fragments is disabled.
  variable_set('googleanalytics_trackurlfragments', 0);
  $this
    ->drupalGet('');
  $this
    ->assertNoRaw('ga("set", "page", location.pathname + location.search + location.hash);', '[testGoogleAnalyticsTrackingCode]: Tracking code for url fragments is not enabled.');

  // Test if track display features is enabled.
  variable_set('googleanalytics_trackdoubleclick', 1);
  $this
    ->drupalGet('');
  $this
    ->assertRaw('ga("require", "displayfeatures");', '[testGoogleAnalyticsTrackingCode]: Tracking code for display features is enabled.');

  // Test if track display features is disabled.
  variable_set('googleanalytics_trackdoubleclick', 0);
  $this
    ->drupalGet('');
  $this
    ->assertNoRaw('ga("require", "displayfeatures");', '[testGoogleAnalyticsTrackingCode]: Tracking code for display features is not enabled.');

  // Test whether single domain tracking is active.
  $this
    ->drupalGet('');
  $this
    ->assertRaw('{ "cookieDomain": "auto" }', '[testGoogleAnalyticsTrackingCode]: Single domain tracking is active.');

  // Enable "One domain with multiple subdomains".
  variable_set('googleanalytics_domain_mode', 1);
  $this
    ->drupalGet('');

  // Test may run on localhost, an ipaddress or real domain name.
  // TODO: Workaround to run tests successfully. This feature cannot tested reliable.
  global $cookie_domain;
  if (count(explode('.', $cookie_domain)) > 2 && !is_numeric(str_replace('.', '', $cookie_domain))) {
    $this
      ->assertRaw('{ "cookieDomain": "' . $cookie_domain . '" }', '[testGoogleAnalyticsTrackingCode]: One domain with multiple subdomains is active on real host.');
  }
  else {

    // Special cases, Localhost and IP addresses don't show '_setDomainName'.
    $this
      ->assertNoRaw('{ "cookieDomain": "' . $cookie_domain . '" }', '[testGoogleAnalyticsTrackingCode]: One domain with multiple subdomains may be active on localhost (test result is not reliable).');
  }

  // Enable "Multiple top-level domains" tracking.
  variable_set('googleanalytics_domain_mode', 2);
  variable_set('googleanalytics_cross_domains', "www.example.com\nwww.example.net");
  $this
    ->drupalGet('');
  $this
    ->assertRaw('ga("create", "' . $ua_code . '", { "cookieDomain": "auto", "allowLinker": true', '[testGoogleAnalyticsTrackingCode]: "allowLinker" has been found. Cross domain tracking is active.');
  $this
    ->assertRaw('ga("require", "linker");', '[testGoogleAnalyticsTrackingCode]: Require linker has been found. Cross domain tracking is active.');
  $this
    ->assertRaw('ga("linker:autoLink", [ "www.example.com", "www.example.net" ]);', '[testGoogleAnalyticsTrackingCode]: "linker:autoLink" has been found. Cross domain tracking is active.');
  $this
    ->assertRaw('"trackDomainMode":2,', '[testGoogleAnalyticsTrackingCode]: Domain mode value is of type integer.');
  $this
    ->assertRaw('"trackCrossDomains": [ "www.example.com", "www.example.net" ]', '[testGoogleAnalyticsTrackingCode]: Cross domain tracking with www.example.com and www.example.net is active.');
  variable_set('googleanalytics_domain_mode', 0);

  // Test whether debugging script has been enabled.
  variable_set('googleanalytics_debug', 1);
  $this
    ->drupalGet('');
  $this
    ->assertRaw('//www.google-analytics.com/analytics_debug.js', '[testGoogleAnalyticsTrackingCode]: Google debugging script has been enabled.');

  // Check if text and link is shown on 'Status Reports' page.
  // Requires 'administer site configuration' permission.
  $this
    ->drupalGet('admin/reports/status');
  $this
    ->assertRaw(t('Google Analytics module has debugging enabled. Please disable debugging setting in production sites from the <a href="@url">Google Analytics settings page</a>.', array(
    '@url' => url('admin/settings/googleanalytics'),
  )), '[testGoogleAnalyticsConfiguration]: Debugging enabled is shown on Status Reports page.');

  // Test whether debugging script has been disabled.
  variable_set('googleanalytics_debug', 0);
  $this
    ->drupalGet('');
  $this
    ->assertRaw('//www.google-analytics.com/analytics.js', '[testGoogleAnalyticsTrackingCode]: Google debugging script has been disabled.');

  // Test whether the CREATE and BEFORE and AFTER code is added to the tracker.
  $codesnippet_create = array(
    'cookieDomain' => 'foo.example.com',
    'cookieName' => 'myNewName',
    'cookieExpires' => 20000,
    'allowAnchor' => TRUE,
    'sampleRate' => 4.3,
  );
  variable_set('googleanalytics_codesnippet_create', $codesnippet_create);
  variable_set('googleanalytics_codesnippet_before', 'ga("set", "forceSSL", true);');
  variable_set('googleanalytics_codesnippet_after', 'ga("create", "UA-123456-3", { "name": "newTracker" });ga("newTracker.send", "pageview");');
  $this
    ->drupalGet('');
  $this
    ->assertRaw('ga("create", "' . $ua_code . '", { "cookieDomain": "foo.example.com", "cookieName": "myNewName", "cookieExpires": 20000, "allowAnchor": true, "sampleRate": 4.3 });', '[testGoogleAnalyticsTrackingCode]: Create only fields have been found.');
  $this
    ->assertRaw('ga("set", "forceSSL", true);', '[testGoogleAnalyticsTrackingCode]: Before codesnippet will force http pages to also send all beacons using https.');
  $this
    ->assertRaw('ga("create", "UA-123456-3", { "name": "newTracker" });', '[testGoogleAnalyticsTrackingCode]: After codesnippet with "newTracker" tracker has been found.');
}