You are here

function GoogleAnalyticsBasicTest::testGoogleAnalyticsTrackingCode in Google Analytics 7

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

File

./googleanalytics.test, line 110
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_pages', 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 type="text/javascript">
        var _gaq = _gaq || [];
        _gaq.push(['_setAccount', 'UA-123456-7']);
        _gaq.push(['_trackPageview']);

        (function() {
          var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
          ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
          var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
        })();
      </script>
      */

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

  // Test whether the alternate doubleclick library is used
  variable_set('googleanalytics_trackdoubleclick', 1);
  $this
    ->drupalGet('');
  $this
    ->assertRaw('stats.g.doubleclick.net/dc.js', '[testGoogleAnalyticsTrackingCode]: Doubleclick tracking code used.');

  // Test whether anonymize visitors IP address feature has been enabled.
  $this
    ->drupalGet('');
  $this
    ->assertNoRaw('_gaq.push(["_gat._anonymizeIp"]);', '[testGoogleAnalyticsTrackingCode]: Anonymize visitors IP address not found on frontpage.');

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

  // Test whether single domain tracking is active.
  $this
    ->drupalGet('');
  $this
    ->assertNoRaw('_gaq.push(["_setDomainName"', '[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('_gaq.push(["_setDomainName",', '[testGoogleAnalyticsTrackingCode]: One domain with multiple subdomains is active on real host.');
  }
  else {

    // Special cases, Localhost and IP addresses don't show '_setDomainName'.
    $this
      ->assertNoRaw('_gaq.push(["_setDomainName",', '[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('_gaq.push(["_setDomainName", "none"]);', '[testGoogleAnalyticsTrackingCode]: _setDomainName: "none" found. Cross domain tracking is active.');
  $this
    ->assertRaw('_gaq.push(["_setAllowLinker", true]);', '[testGoogleAnalyticsTrackingCode]: _setAllowLinker: true found. Cross domain tracking is active.');
  $this
    ->assertRaw('"trackCrossDomains":["www.example.com","www.example.net"]', '[testGoogleAnalyticsTrackingCode]: Cross domain tracking with www.example.com and www.example.net is active.');

  // Test whether the BEFORE and AFTER code is added to the tracker.
  variable_set('googleanalytics_codesnippet_before', '_setDetectFlash(false);');
  variable_set('googleanalytics_codesnippet_after', '_gaq.push(["t2._setAccount", "UA-123456-3"]);_gaq.push(["t2._trackPageview"]);');
  $this
    ->drupalGet('');
  $this
    ->assertRaw('_setDetectFlash(false);', '[testGoogleAnalyticsTrackingCode]: Before codesnippet has been found with "Flash" detection disabled.');
  $this
    ->assertRaw('t2._setAccount', '[testGoogleAnalyticsTrackingCode]: After codesnippet with "t2" tracker has been found.');
}