5 * Test file for Google Analytics module.
7 class GoogleAnalyticsBasicTest
extends DrupalWebTestCase
{
9 public static
function getInfo() {
11 'name' => t('Google Analytics basic tests'),
12 'description' => t('Test basic functionality of Google Analytics module.'),
13 'group' => 'Google Analytics',
18 parent
::setUp('googleanalytics');
20 $permissions = array('administer google analytics');
22 // User to set up google_analytics.
23 $this->admin_user
= $this->drupalCreateUser($permissions);
24 $this->drupalLogin($this->admin_user
);
27 function testGoogleAnalyticsConfiguration() {
28 // Check for setting page's presence.
29 $this->drupalGet('admin/settings/googleanalytics');
30 $this->assertRaw(t('Web Property ID'), '[testGoogleAnalyticsConfiguration]: Settings page displayed.');
32 // Check for account code validation.
33 $edit['googleanalytics_account'] = $this->randomName(2);
34 $this->drupalPost('admin/settings/googleanalytics', $edit, 'Save configuration');
35 $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.');
38 function testGoogleAnalyticsPageVisibility() {
39 $ua_code = 'UA-123456-1';
40 variable_set('googleanalytics_account', $ua_code);
42 // Show tracking on "every page except the listed pages".
43 variable_set('googleanalytics_visibility', 0);
44 // Disable tracking one "admin*" pages only.
45 variable_set('googleanalytics_pages', "admin\nadmin/*");
46 // Enable tracking only for authenticated users only.
47 variable_set('googleanalytics_roles', array(DRUPAL_AUTHENTICATED_RID
=> DRUPAL_AUTHENTICATED_RID
));
49 // Check tracking code visibility.
51 $this->assertRaw($ua_code, '[testGoogleAnalyticsPageVisibility]: Tracking code is displayed for authenticated users.');
53 // Test whether tracking code is not included on pages to omit.
54 $this->drupalGet('admin');
55 $this->assertNoRaw($ua_code, '[testGoogleAnalyticsPageVisibility]: Tracking code is not displayed on admin page.');
56 $this->drupalGet('admin/settings/googleanalytics');
57 // Checking for tracking code URI here, as $ua_code is displayed in the form.
58 $this->assertNoRaw('google-analytics.com/ga.js', '[testGoogleAnalyticsPageVisibility]: Tracking code is not displayed on admin subpage.');
60 // Test whether tracking code display is properly flipped.
61 variable_set('googleanalytics_visibility', 1);
62 $this->drupalGet('admin');
63 $this->assertRaw($ua_code, '[testGoogleAnalyticsPageVisibility]: Tracking code is displayed on admin page.');
64 $this->drupalGet('admin/settings/googleanalytics');
65 // Checking for tracking code URI here, as $ua_code is displayed in the form.
66 $this->assertRaw('google-analytics.com/ga.js', '[testGoogleAnalyticsPageVisibility]: Tracking code is displayed on admin subpage.');
68 $this->assertNoRaw($ua_code, '[testGoogleAnalyticsPageVisibility]: Tracking code is NOT displayed on front page.');
70 // Test whether tracking code is not display for anonymous.
71 $this->drupalLogout();
73 $this->assertNoRaw($ua_code, '[testGoogleAnalyticsPageVisibility]: Tracking code is NOT displayed for anonymous.');
75 // Switch back to every page except the listed pages.
76 variable_set('googleanalytics_visibility', 0);
77 // Enable tracking code for all user roles.
78 variable_set('googleanalytics_roles', array());
80 // Test whether 404 not found tracking code is shown on non-existent pages.
81 $this->drupalGet($this->randomName(64));
82 $this->assertRaw('/404.html', '[testGoogleAnalyticsPageVisibility]: 404 Not Found tracking code shown on non-existent page.');
85 // Enable caching of pages for anonymous users.
86 variable_set('cache', 1);
87 // Test whether DNT headers will fail to disable embedding of tracking code.
88 $this->drupalGet('', array(), array('DNT: 1'));
89 $this->assertRaw('_gaq.push(["_trackPageview"]);', '[testGoogleAnalyticsDNTVisibility]: DNT header send from client, but page caching is enabled and tracker cannot removed.');
90 // DNT works only with caching of pages for anonymous users disabled.
91 variable_set('cache', 0);
93 $this->assertRaw('_gaq.push(["_trackPageview"]);', '[testGoogleAnalyticsDNTVisibility]: Tracking is enabled without DNT header.');
94 // Test whether DNT header is able to remove the tracking code.
95 $this->drupalGet('', array(), array('DNT: 1'));
96 $this->assertNoRaw('_gaq.push(["_trackPageview"]);', '[testGoogleAnalyticsDNTVisibility]: DNT header received from client. Tracking has been disabled by browser.');
97 // Disable DNT feature and see if tracker is still embedded.
98 variable_set('googleanalytics_privacy_donottrack', 0);
99 $this->drupalGet('', array(), array('DNT: 1'));
100 $this->assertRaw('_gaq.push(["_trackPageview"]);', '[testGoogleAnalyticsDNTVisibility]: DNT feature is disabled, DNT header from browser has been ignored.');
103 function testGoogleAnalyticsTrackingCode() {
104 $ua_code = 'UA-123456-2';
105 variable_set('googleanalytics_account', $ua_code);
107 // Show tracking code on every page except the listed pages.
108 variable_set('googleanalytics_visibility', 0);
109 // Enable tracking code for all user roles.
110 variable_set('googleanalytics_roles', array());
112 /* Sample JS code as added to page:
113 <script type="text/javascript" src="/sites/all/modules/google_analytics/googleanalytics.js?w"></script>
114 <script type="text/javascript">
115 var _gaq = _gaq || [];
116 _gaq.push(['_setAccount', 'UA-123456-7']);
117 _gaq.push(['_trackPageview']);
120 var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
121 ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
122 var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
127 // Test whether tracking code uses latest JS.
128 variable_set('googleanalytics_cache', 0);
129 $this->drupalGet('');
130 $this->assertRaw('google-analytics.com/ga.js', '[testGoogleAnalyticsTrackingCode]: Latest tracking code used.');
132 // Test whether the alternate doubleclick library is used
133 variable_set('googleanalytics_trackdoubleclick', 1);
134 $this->drupalGet('');
135 $this->assertRaw('stats.g.doubleclick.net/dc.js', '[testGoogleAnalyticsTrackingCode]: Doubleclick tracking code used.');
137 // Test whether anonymize visitors IP address feature has been enabled.
138 $this->drupalGet('');
139 $this->assertNoRaw('_gaq.push(["_gat._anonymizeIp"]);', '[testGoogleAnalyticsTrackingCode]: Anonymize visitors IP address not found on frontpage.');
140 // Enable anonymizing of IP addresses.
141 variable_set('googleanalytics_tracker_anonymizeip', 1);
142 $this->drupalGet('');
143 $this->assertRaw('_gaq.push(["_gat._anonymizeIp"]);', '[testGoogleAnalyticsTrackingCode]: Anonymize visitors IP address found on frontpage.');
145 // Test whether the BEFORE and AFTER code is added to the tracker.
146 variable_set('googleanalytics_codesnippet_before', '_setDetectFlash(false);');
147 variable_set('googleanalytics_codesnippet_after', '_gaq.push(["t2._setAccount", "UA-123456-3"]);_gaq.push(["t2._trackPageview"]);');
148 $this->drupalGet('');
149 $this->assertRaw('_setDetectFlash(false);', '[testGoogleAnalyticsTrackingCode]: Before codesnippet has been found with "Flash" detection disabled.');
150 $this->assertRaw('t2._setAccount', '[testGoogleAnalyticsTrackingCode]: After codesnippet with "t2" tracker has been found.');
154 class GoogleAnalyticsCustomVariablesTest
extends DrupalWebTestCase
{
156 public static
function getInfo() {
158 'name' => t('Google Analytics Custom Variables tests'),
159 'description' => t('Test custom variables functionality of Google Analytics module.'),
160 'group' => 'Google Analytics',
161 'dependencies' => array('token'),
166 parent
::setUp('googleanalytics', 'token');
168 $permissions = array(
169 'access administration pages',
170 'administer google analytics',
173 // User to set up google_analytics.
174 $this->admin_user
= $this->drupalCreateUser($permissions);
177 function testGoogleAnalyticsCustomVariables() {
178 $ua_code = 'UA-123456-3';
179 variable_set('googleanalytics_account', $ua_code);
181 // Basic test if the feature works.
182 $custom_vars = array(
216 variable_set('googleanalytics_custom_var', $custom_vars);
217 $this->drupalGet('');
219 foreach ($custom_vars['slots'] as
$slot) {
220 $this->assertRaw("_gaq.push(['_setCustomVar', " .
$slot['slot'] .
", \"" .
$slot['name'] .
"\", \"" .
$slot['value'] .
"\", " .
$slot['scope'] .
"]);", '[testGoogleAnalyticsCustomVariables]: _setCustomVar ' .
$slot['slot'] .
' is shown.');
223 // Test whether tokens are replaced in custom variable names.
224 $site_slogan = $this->randomName(16);
225 variable_set('site_slogan', $site_slogan);
227 $custom_vars = array(
231 'name' => 'Name: [site-slogan]',
232 'value' => 'Value: [site-slogan]',
238 'value' => $this->randomName(16),
243 'name' => $this->randomName(16),
261 variable_set('googleanalytics_custom_var', $custom_vars);
262 $this->verbose('<pre>' .
print_r($custom_vars, TRUE
) .
'</pre>');
264 $this->drupalGet('');
265 $this->assertRaw("_gaq.push(['_setCustomVar', 1, \"Name: $site_slogan\", \"Value: $site_slogan\", 3]", '[testGoogleAnalyticsCustomVariables]: Tokens have been replaced in custom variable.');
266 $this->assertNoRaw("_gaq.push(['_setCustomVar', 2,", '[testGoogleAnalyticsCustomVariables]: Value with empty name is not shown.');
267 $this->assertNoRaw("_gaq.push(['_setCustomVar', 3,", '[testGoogleAnalyticsCustomVariables]: Name with empty value is not shown.');
268 $this->assertNoRaw("_gaq.push(['_setCustomVar', 4,", '[testGoogleAnalyticsCustomVariables]: Empty name and value is not shown.');
269 $this->assertNoRaw("_gaq.push(['_setCustomVar', 5,", '[testGoogleAnalyticsCustomVariables]: Empty name and value is not shown.');
273 class GoogleAnalyticsRolesTest
extends DrupalWebTestCase
{
275 public static
function getInfo() {
277 'name' => t('Google Analytics role tests'),
278 'description' => t('Test roles functionality of Google Analytics module.'),
279 'group' => 'Google Analytics',
284 parent
::setUp('googleanalytics');
286 $permissions = array(
287 'access administration pages',
288 'administer google analytics',
291 // User to set up google_analytics.
292 $this->admin_user
= $this->drupalCreateUser($permissions);
295 function testGoogleAnalyticsRolesTracking() {
296 $ua_code = 'UA-123456-4';
297 variable_set('googleanalytics_account', $ua_code);
299 // Test if the default settings are working as expected.
301 // Add to the selected roles only.
302 variable_set('googleanalytics_visibility_roles', 0);
303 // Enable tracking for all users.
304 variable_set('googleanalytics_roles', array());
306 // Check tracking code visibility.
307 $this->drupalGet('');
308 $this->assertRaw($ua_code, '[testGoogleAnalyticsRoleVisibility]: Tracking code is displayed for anonymous users on frontpage with default settings.');
310 $this->drupalLogin($this->admin_user
);
312 $this->drupalGet('');
313 $this->assertRaw($ua_code, '[testGoogleAnalyticsRoleVisibility]: Tracking code is displayed for authenticated users on frontpage with default settings.');
314 $this->drupalGet('admin');
315 $this->assertNoRaw($ua_code, '[testGoogleAnalyticsRoleVisibility]: Tracking code is NOT displayed for authenticated users in admin section with default settings.');
317 // Test if the non-default settings are working as expected.
319 // Enable tracking only for authenticated users.
320 variable_set('googleanalytics_roles', array(DRUPAL_AUTHENTICATED_RID
=> DRUPAL_AUTHENTICATED_RID
));
322 $this->drupalGet('');
323 $this->assertRaw($ua_code, '[testGoogleAnalyticsRoleVisibility]: Tracking code is displayed for authenticated users only on frontpage.');
325 $this->drupalLogout();
326 $this->drupalGet('');
327 $this->assertNoRaw($ua_code, '[testGoogleAnalyticsRoleVisibility]: Tracking code is NOT displayed for anonymous users on frontpage.');
329 // Add to every role except the selected ones.
330 variable_set('googleanalytics_visibility_roles', 1);
331 // Enable tracking for all users.
332 variable_set('googleanalytics_roles', array());
334 // Check tracking code visibility.
335 $this->drupalGet('');
336 $this->assertRaw($ua_code, '[testGoogleAnalyticsRoleVisibility]: Tracking code is added to every role and displayed for anonymous users.');
338 $this->drupalLogin($this->admin_user
);
340 $this->drupalGet('');
341 $this->assertRaw($ua_code, '[testGoogleAnalyticsRoleVisibility]: Tracking code is added to every role and displayed on frontpage for authenticated users.');
342 $this->drupalGet('admin');
343 $this->assertNoRaw($ua_code, '[testGoogleAnalyticsRoleVisibility]: Tracking code is added to every role and NOT displayed in admin section for authenticated users.');
345 // Disable tracking for authenticated users.
346 variable_set('googleanalytics_roles', array(DRUPAL_AUTHENTICATED_RID
=> DRUPAL_AUTHENTICATED_RID
));
348 $this->drupalGet('');
349 $this->assertNoRaw($ua_code, '[testGoogleAnalyticsRoleVisibility]: Tracking code is NOT displayed on frontpage for excluded authenticated users.');
350 $this->drupalGet('admin');
351 $this->assertNoRaw($ua_code, '[testGoogleAnalyticsRoleVisibility]: Tracking code is NOT displayed in admin section for excluded authenticated users.');
353 $this->drupalLogout();
354 $this->drupalGet('');
355 $this->assertRaw($ua_code, '[testGoogleAnalyticsRoleVisibility]: Tracking code is displayed on frontpage for included anonymous users.');