function GoogleAnalyticsBasicTest::testGoogleAnalyticsTrackingCode in Google Analytics 6.4
Same name and namespace in other branches
- 6.3 googleanalytics.test \GoogleAnalyticsBasicTest::testGoogleAnalyticsTrackingCode()
- 7.2 googleanalytics.test \GoogleAnalyticsBasicTest::testGoogleAnalyticsTrackingCode()
- 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);
variable_set('googleanalytics_visibility', 0);
variable_set('googleanalytics_roles', array());
variable_set('googleanalytics_cache', 0);
$this
->drupalGet('');
$this
->assertRaw('//www.google-analytics.com/analytics.js', '[testGoogleAnalyticsTrackingCode]: Latest tracking code used.');
variable_set('googleanalytics_tracker_anonymizeip', 0);
$this
->drupalGet('');
$this
->assertNoRaw('ga("set", "anonymizeIp", true);', '[testGoogleAnalyticsTrackingCode]: Anonymize visitors IP address not found on frontpage.');
variable_set('googleanalytics_tracker_anonymizeip', 1);
$this
->drupalGet('');
$this
->assertRaw('ga("set", "anonymizeIp", true);', '[testGoogleAnalyticsTrackingCode]: Anonymize visitors IP address found on frontpage.');
variable_set('googleanalytics_tracklinkid', 1);
$this
->drupalGet('');
$this
->assertRaw('ga("require", "linkid", "linkid.js");', '[testGoogleAnalyticsTrackingCode]: Tracking code for Enhanced Link Attribution is enabled.');
variable_set('googleanalytics_tracklinkid', 0);
$this
->drupalGet('');
$this
->assertNoRaw('ga("require", "linkid", "linkid.js");', '[testGoogleAnalyticsTrackingCode]: Tracking code for Enhanced Link Attribution is not enabled.');
variable_set('googleanalytics_trackuserid', 1);
$this
->drupalGet('');
$this
->assertRaw(', { "cookieDomain": "auto", "userId": "', '[testGoogleAnalyticsTrackingCode]: Tracking code for User ID is enabled.');
variable_set('googleanalytics_trackuserid', 0);
$this
->drupalGet('');
$this
->assertNoRaw(', { "cookieDomain": "auto", "userId": "', '[testGoogleAnalyticsTrackingCode]: Tracking code for User ID is disabled.');
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.');
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.');
variable_set('googleanalytics_trackdoubleclick', 1);
$this
->drupalGet('');
$this
->assertRaw('ga("require", "displayfeatures");', '[testGoogleAnalyticsTrackingCode]: Tracking code for display features is enabled.');
variable_set('googleanalytics_trackdoubleclick', 0);
$this
->drupalGet('');
$this
->assertNoRaw('ga("require", "displayfeatures");', '[testGoogleAnalyticsTrackingCode]: Tracking code for display features is not enabled.');
$this
->drupalGet('');
$this
->assertRaw('{ "cookieDomain": "auto" }', '[testGoogleAnalyticsTrackingCode]: Single domain tracking is active.');
variable_set('googleanalytics_domain_mode', 1);
$this
->drupalGet('');
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 {
$this
->assertNoRaw('{ "cookieDomain": "' . $cookie_domain . '" }', '[testGoogleAnalyticsTrackingCode]: One domain with multiple subdomains may be active on localhost (test result is not reliable).');
}
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);
variable_set('googleanalytics_debug', 1);
$this
->drupalGet('');
$this
->assertRaw('//www.google-analytics.com/analytics_debug.js', '[testGoogleAnalyticsTrackingCode]: Google debugging script has been enabled.');
$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.');
variable_set('googleanalytics_debug', 0);
$this
->drupalGet('');
$this
->assertRaw('//www.google-analytics.com/analytics.js', '[testGoogleAnalyticsTrackingCode]: Google debugging script has been disabled.');
$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.');
}