GoogleAnalyticsCustomUrls.php in Google Analytics 4.x
File
tests/src/Functional/GoogleAnalyticsCustomUrls.php
View source
<?php
namespace Drupal\Tests\google_analytics\Functional;
use Drupal\Component\Serialization\Json;
use Drupal\Tests\BrowserTestBase;
class GoogleAnalyticsCustomUrls extends BrowserTestBase {
public static $modules = [
'google_analytics',
];
protected $defaultTheme = 'stark';
protected $adminUser;
protected function setUp() {
parent::setUp();
$permissions = [
'access administration pages',
'administer google analytics',
'administer modules',
'administer site configuration',
];
$this->adminUser = $this
->drupalCreateUser($permissions);
}
public function testGoogleAnalyticsCustomUrls() {
$base_path = base_path();
$ua_code = 'UA-123456-1';
$this
->config('google_analytics.settings')
->set('account', $ua_code)
->set('privacy.anonymizeip', 0)
->set('track.displayfeatures', 1)
->save();
$this
->drupalGet('user/password', [
'query' => [
'name' => 'foo',
],
]);
$this
->assertRaw('gtag("config", ' . Json::encode($ua_code) . ', {"groups":"default","page_path":"' . $base_path . 'user/password"});');
$this
->drupalGet('user/password', [
'query' => [
'name' => 'foo@example.com',
],
]);
$this
->assertRaw('gtag("config", ' . Json::encode($ua_code) . ', {"groups":"default","page_path":"' . $base_path . 'user/password"});');
$this
->drupalGet('user/password');
$this
->assertNoRaw('"page_path":"' . $base_path . 'user/password"});');
$this
->drupalGet('admin');
$this
->assertResponse(403);
$this
->assertRaw($base_path . '403.html');
$this
->drupalGet($this
->randomMachineName(64));
$this
->assertResponse(404);
$this
->assertRaw($base_path . '404.html');
}
}