home | career | drupal | java | mac | mysql | perl | php | scala | uml | unix

Drupal example source code file (googleanalytics.test)

This example Drupal source code file (googleanalytics.test) is included in the DevDaily.com "Drupal Source Code Warehouse" project. The intent of this project is to help you "Learn Drupal by Example".

PHP - Drupal tags/keywords

array, code, displayed, for, frontpage, function, is, not, php, testgoogleanalyticspagevisibility, testgoogleanalyticsrolevisibility, tracking, ua_code, users

The googleanalytics.test Drupal example source code

<?php
// $Id: googleanalytics.test,v 1.3.2.3 2011/02/07 16:28:31 hass Exp $

/**
 * @file
 * Test file for Google Analytics module.
 */
class GoogleAnalyticsBasicTest extends DrupalWebTestCase {

  public static function getInfo() {
    return array(
      'name' => t('Google Analytics basic tests'),
      'description' => t('Test basic functionality of Google Analytics module.'),
      'group' => 'Google Analytics',
    );
  }

  function setUp() {
    parent::setUp('googleanalytics');

    $permissions = array('administer google analytics');

    // User to set up google_analytics.
    $this->admin_user = $this->drupalCreateUser($permissions);
    $this->drupalLogin($this->admin_user);
  }

  function testGoogleAnalyticsConfiguration() {
    // Check for setting page's presence.
    $this->drupalGet('admin/settings/googleanalytics');
    $this->assertRaw(t('Web Property ID'), '[testGoogleAnalyticsConfiguration]: Settings page displayed.');

    // Check for account code validation.
    $edit['googleanalytics_account'] = $this->randomName(2);
    $this->drupalPost('admin/settings/googleanalytics', $edit, 'Save configuration');
    $this->assertRaw(t('A valid Google Analytics Web Property ID is case sensitive and formatted like UA-xxxxxxx-yy.'), '[testGoogleAnalyticsConfiguration]: Invalid Web Property ID number validated.');
  }

  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.');
  }

  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 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 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 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.');
  }

}

class GoogleAnalyticsRolesTest extends DrupalWebTestCase {

  public static function getInfo() {
    return array(
      'name' => t('Google Analytics role tests'),
      'description' => t('Test roles functionality of Google Analytics module.'),
      'group' => 'Google Analytics',
    );
  }

  function setUp() {
    parent::setUp('googleanalytics');

    $permissions = array(
      'access administration pages',
      'administer google analytics',
    );

    // User to set up google_analytics.
    $this->admin_user = $this->drupalCreateUser($permissions);
  }

  function testGoogleAnalyticsRolesTracking() {
    $ua_code = 'UA-123456-4';
    variable_set('googleanalytics_account', $ua_code);

    // Test if the default settings are working as expected.

    // Add to the selected roles only.
    variable_set('googleanalytics_visibility_roles', 0);
    // Enable tracking for all users.
    variable_set('googleanalytics_roles', array());

    // Check tracking code visibility.
    $this->drupalGet('');
    $this->assertRaw($ua_code, '[testGoogleAnalyticsRoleVisibility]: Tracking code is displayed for anonymous users on frontpage with default settings.');

    $this->drupalLogin($this->admin_user);

    $this->drupalGet('');
    $this->assertRaw($ua_code, '[testGoogleAnalyticsRoleVisibility]: Tracking code is displayed for authenticated users on frontpage with default settings.');
    $this->drupalGet('admin');
    $this->assertNoRaw($ua_code, '[testGoogleAnalyticsRoleVisibility]: Tracking code is NOT displayed for authenticated users in admin section with default settings.');

    // Test if the non-default settings are working as expected.

    // Enable tracking only for authenticated users.
    variable_set('googleanalytics_roles', array(DRUPAL_AUTHENTICATED_RID => DRUPAL_AUTHENTICATED_RID));

    $this->drupalGet('');
    $this->assertRaw($ua_code, '[testGoogleAnalyticsRoleVisibility]: Tracking code is displayed for authenticated users only on frontpage.');

    $this->drupalLogout();
    $this->drupalGet('');
    $this->assertNoRaw($ua_code, '[testGoogleAnalyticsRoleVisibility]: Tracking code is NOT displayed for anonymous users on frontpage.');

    // Add to every role except the selected ones.
    variable_set('googleanalytics_visibility_roles', 1);
    // Enable tracking for all users.
    variable_set('googleanalytics_roles', array());

    // Check tracking code visibility.
    $this->drupalGet('');
    $this->assertRaw($ua_code, '[testGoogleAnalyticsRoleVisibility]: Tracking code is added to every role and displayed for anonymous users.');

    $this->drupalLogin($this->admin_user);

    $this->drupalGet('');
    $this->assertRaw($ua_code, '[testGoogleAnalyticsRoleVisibility]: Tracking code is added to every role and displayed on frontpage for authenticated users.');
    $this->drupalGet('admin');
    $this->assertNoRaw($ua_code, '[testGoogleAnalyticsRoleVisibility]: Tracking code is added to every role and NOT displayed in admin section for authenticated users.');

    // Disable tracking for authenticated users.
    variable_set('googleanalytics_roles', array(DRUPAL_AUTHENTICATED_RID => DRUPAL_AUTHENTICATED_RID));

    $this->drupalGet('');
    $this->assertNoRaw($ua_code, '[testGoogleAnalyticsRoleVisibility]: Tracking code is NOT displayed on frontpage for excluded authenticated users.');
    $this->drupalGet('admin');
    $this->assertNoRaw($ua_code, '[testGoogleAnalyticsRoleVisibility]: Tracking code is NOT displayed in admin section for excluded authenticated users.');

    $this->drupalLogout();
    $this->drupalGet('');
    $this->assertRaw($ua_code, '[testGoogleAnalyticsRoleVisibility]: Tracking code is displayed on frontpage for included anonymous users.');
  }

}

Other Drupal examples (source code examples)

Here is a short list of links related to this Drupal googleanalytics.test source code file:

new blog posts

"Drupal" is a registered trademark of Dries Buytaert.

my drupal tutorials and examples  

Copyright 1998-2016 Alvin Alexander, alvinalexander.com
All Rights Reserved.

Beginning in 2016, a portion of the proceeds from pages under the '/drupal-code-examples/' URI will be donated to charity.