public function GoogleAnalyticsBasicTest::testGoogleAnalyticsTrackingCode in Google Analytics 8.2
Same name and namespace in other branches
- 8.3 tests/src/Functional/GoogleAnalyticsBasicTest.php \Drupal\Tests\google_analytics\Functional\GoogleAnalyticsBasicTest::testGoogleAnalyticsTrackingCode()
- 4.x tests/src/Functional/GoogleAnalyticsBasicTest.php \Drupal\Tests\google_analytics\Functional\GoogleAnalyticsBasicTest::testGoogleAnalyticsTrackingCode()
Tests if tracking code is properly added to the page.
File
- tests/
src/ Functional/ GoogleAnalyticsBasicTest.php, line 186
Class
- GoogleAnalyticsBasicTest
- Test basic functionality of Google Analytics module.
Namespace
Drupal\Tests\google_analytics\FunctionalCode
public function testGoogleAnalyticsTrackingCode() {
$ua_code = 'UA-123456-2';
$this
->config('google_analytics.settings')
->set('account', $ua_code)
->save();
// Show tracking code on every page except the listed pages.
$this
->config('google_analytics.settings')
->set('visibility.request_path_mode', 0)
->save();
// Enable tracking code for all user roles.
$this
->config('google_analytics.settings')
->set('visibility.user_role_roles', [])
->save();
/* Sample JS code as added to page:
<script type="text/javascript" src="/sites/all/modules/google_analytics/google_analytics.js?w"></script>
<script>
(function(i,s,o,g,r,a,m){
i["GoogleAnalyticsObject"]=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,"script","https://www.google-analytics.com/analytics.js","ga");
ga('create', 'UA-123456-7');
ga('send', 'pageview');
</script>
<!-- End Google Analytics -->
*/
// Test whether tracking code uses latest JS.
$this
->config('google_analytics.settings')
->set('cache', 0)
->save();
$this
->drupalGet('');
$this
->assertSession()
->responseContains('https://www.google-analytics.com/analytics.js');
// Test whether anonymize visitors IP address feature has been enabled.
$this
->config('google_analytics.settings')
->set('privacy.anonymizeip', 0)
->save();
$this
->drupalGet('');
$this
->assertSession()
->responseNotContains('ga("set", "anonymizeIp", true);');
// Enable anonymizing of IP addresses.
$this
->config('google_analytics.settings')
->set('privacy.anonymizeip', 1)
->save();
$this
->drupalGet('');
$this
->assertSession()
->responseContains('ga("set", "anonymizeIp", true);');
// Test if track Enhanced Link Attribution is enabled.
$this
->config('google_analytics.settings')
->set('track.linkid', 1)
->save();
$this
->drupalGet('');
$this
->assertSession()
->responseContains('ga("require", "linkid", "linkid.js");');
// Test if track Enhanced Link Attribution is disabled.
$this
->config('google_analytics.settings')
->set('track.linkid', 0)
->save();
$this
->drupalGet('');
$this
->assertSession()
->responseNotContains('ga("require", "linkid", "linkid.js");');
// Test if tracking of url fragments is enabled.
$this
->config('google_analytics.settings')
->set('track.urlfragments', 1)
->save();
$this
->drupalGet('');
$this
->assertSession()
->responseContains('ga("set", "page", location.pathname + location.search + location.hash);');
// Test if tracking of url fragments is disabled.
$this
->config('google_analytics.settings')
->set('track.urlfragments', 0)
->save();
$this
->drupalGet('');
$this
->assertSession()
->responseNotContains('ga("set", "page", location.pathname + location.search + location.hash);');
// Test if tracking of User ID is enabled.
$this
->config('google_analytics.settings')
->set('track.userid', 1)
->save();
$this
->drupalGet('');
$this
->assertSession()
->responseContains(', {"cookieDomain":"auto","userId":"');
// Test if tracking of User ID is disabled.
$this
->config('google_analytics.settings')
->set('track.userid', 0)
->save();
$this
->drupalGet('');
$this
->assertSession()
->responseNotContains(', {"cookieDomain":"auto","userId":"');
// Test if track display features is enabled.
$this
->config('google_analytics.settings')
->set('track.displayfeatures', 1)
->save();
$this
->drupalGet('');
$this
->assertSession()
->responseContains('ga("require", "displayfeatures");');
// Test if track display features is disabled.
$this
->config('google_analytics.settings')
->set('track.displayfeatures', 0)
->save();
$this
->drupalGet('');
$this
->assertSession()
->responseNotContains('ga("require", "displayfeatures");');
// Test whether single domain tracking is active.
$this
->drupalGet('');
$this
->assertSession()
->responseContains('{"cookieDomain":"auto"}');
// Enable "One domain with multiple subdomains".
$this
->config('google_analytics.settings')
->set('domain_mode', 1)
->save();
$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
->assertSession()
->responseContains('{"cookieDomain":"' . $cookie_domain . '"}');
}
else {
// Special cases, Localhost and IP addresses don't show 'cookieDomain'.
$this
->assertSession()
->responseNotContains('{"cookieDomain":"' . $cookie_domain . '"}');
}
// Enable "Multiple top-level domains" tracking.
$this
->config('google_analytics.settings')
->set('domain_mode', 2)
->set('cross_domains', "www.example.com\nwww.example.net")
->save();
$this
->drupalGet('');
$this
->assertSession()
->responseContains('ga("create", "' . $ua_code . '", {"cookieDomain":"auto","allowLinker":true');
$this
->assertSession()
->responseContains('ga("require", "linker");');
$this
->assertSession()
->responseContains('ga("linker:autoLink", ["www.example.com","www.example.net"]);');
$this
->assertSession()
->responseContains('"trackDomainMode":2,');
$this
->assertSession()
->responseContains('"trackCrossDomains":["www.example.com","www.example.net"]');
$this
->config('google_analytics.settings')
->set('domain_mode', 0)
->save();
// Test whether debugging script has been enabled.
$this
->config('google_analytics.settings')
->set('debug', 1)
->save();
$this
->drupalGet('');
$this
->assertSession()
->responseContains('https://www.google-analytics.com/analytics_debug.js');
// Check if text and link is shown on 'Status Reports' page.
// Requires 'administer site configuration' permission.
$this
->drupalGet('admin/reports/status');
$this
->assertSession()
->responseContains(t('Google Analytics module has debugging enabled. Please disable debugging setting in production sites from the <a href=":url">Google Analytics settings page</a>.', [
':url' => Url::fromRoute('google_analytics.admin_settings_form')
->toString(),
]));
// Test whether debugging script has been disabled.
$this
->config('google_analytics.settings')
->set('debug', 0)
->save();
$this
->drupalGet('');
$this
->assertSession()
->responseContains('https://www.google-analytics.com/analytics.js');
// Test whether the CREATE and BEFORE and AFTER code is added to the
// tracking code.
$codesnippet_create = [
'cookieDomain' => 'foo.example.com',
'cookieName' => 'myNewName',
'cookieExpires' => 20000,
'allowAnchor' => TRUE,
'sampleRate' => 4.3,
];
$this
->config('google_analytics.settings')
->set('codesnippet.create', $codesnippet_create)
->set('codesnippet.before', 'ga("set", "forceSSL", true);')
->set('codesnippet.after', 'ga("create", "UA-123456-3", {"name": "newTracker"});if(1 == 1 && 2 < 3 && 2 > 1){console.log("Google Analytics: Custom condition works.");}ga("newTracker.send", "pageview");')
->save();
$this
->drupalGet('');
$this
->assertSession()
->responseContains('ga("create", "' . $ua_code . '", {"cookieDomain":"foo.example.com","cookieName":"myNewName","cookieExpires":20000,"allowAnchor":true,"sampleRate":4.3});');
$this
->assertSession()
->responseContains('ga("set", "forceSSL", true);');
$this
->assertSession()
->responseContains('ga("create", "UA-123456-3", {"name": "newTracker"});');
$this
->assertSession()
->responseContains('if(1 == 1 && 2 < 3 && 2 > 1){console.log("Google Analytics: Custom condition works.");}');
}