public function GoogleAnalyticsCustomDimensionsAndMetricsTest::testGoogleAnalyticsCustomDimensions in Google Analytics 8.2
Same name and namespace in other branches
- 8.3 tests/src/Functional/GoogleAnalyticsCustomDimensionsAndMetricsTest.php \Drupal\Tests\google_analytics\Functional\GoogleAnalyticsCustomDimensionsAndMetricsTest::testGoogleAnalyticsCustomDimensions()
- 4.x tests/src/Functional/GoogleAnalyticsCustomDimensionsAndMetricsTest.php \Drupal\Tests\google_analytics\Functional\GoogleAnalyticsCustomDimensionsAndMetricsTest::testGoogleAnalyticsCustomDimensions()
Tests if custom dimensions are properly added to the page.
File
- tests/
src/ Functional/ GoogleAnalyticsCustomDimensionsAndMetricsTest.php, line 56
Class
- GoogleAnalyticsCustomDimensionsAndMetricsTest
- Test custom dimensions and metrics functionality of Google Analytics module.
Namespace
Drupal\Tests\google_analytics\FunctionalCode
public function testGoogleAnalyticsCustomDimensions() {
$ua_code = 'UA-123456-3';
$this
->config('google_analytics.settings')
->set('account', $ua_code)
->save();
$node = $this
->drupalCreateNode([
'type' => 'article',
]);
// Basic test if the feature works.
$google_analytics_custom_dimension = [
1 => [
'index' => 1,
'value' => 'Bar 1',
],
2 => [
'index' => 2,
'value' => 'Bar 2',
],
3 => [
'index' => 3,
'value' => 'Bar 3',
],
4 => [
'index' => 4,
'value' => 'Bar 4',
],
5 => [
'index' => 5,
'value' => 'Bar 5',
],
];
$this
->config('google_analytics.settings')
->set('custom.dimension', $google_analytics_custom_dimension)
->save();
$this
->drupalGet('');
foreach ($google_analytics_custom_dimension as $dimension) {
$this
->assertSession()
->responseContains('ga("set", ' . Json::encode('dimension' . $dimension['index']) . ', ' . Json::encode($dimension['value']) . ');');
}
// Test whether tokens are replaced in custom dimension values.
$site_slogan = $this
->randomMachineName(16);
$this
->config('system.site')
->set('slogan', $site_slogan)
->save();
$google_analytics_custom_dimension = [
1 => [
'index' => 1,
'value' => 'Value: [site:slogan]',
],
2 => [
'index' => 2,
'value' => $this
->randomMachineName(16),
],
3 => [
'index' => 3,
'value' => '',
],
// #2300701: Custom dimensions and custom metrics not outputed on zero
// value.
4 => [
'index' => 4,
'value' => '0',
],
5 => [
'index' => 5,
'value' => '[node:type]',
],
// Test google_analytics_tokens().
6 => [
'index' => 6,
'value' => '[current-user:role-names]',
],
7 => [
'index' => 7,
'value' => '[current-user:role-ids]',
],
];
$this
->config('google_analytics.settings')
->set('custom.dimension', $google_analytics_custom_dimension)
->save();
$this
->verbose('<pre>' . print_r($google_analytics_custom_dimension, TRUE) . '</pre>');
// Test on frontpage.
$this
->drupalGet('');
$this
->assertSession()
->responseContains('ga("set", ' . Json::encode('dimension1') . ', ' . Json::encode("Value: {$site_slogan}") . ');');
$this
->assertSession()
->responseContains('ga("set", ' . Json::encode('dimension2') . ', ' . Json::encode($google_analytics_custom_dimension['2']['value']) . ');');
$this
->assertSession()
->responseNotContains('ga("set", ' . Json::encode('dimension3') . ', ' . Json::encode('') . ');');
$this
->assertSession()
->responseContains('ga("set", ' . Json::encode('dimension4') . ', ' . Json::encode('0') . ');');
$this
->assertSession()
->responseNotContains('ga("set", ' . Json::encode('dimension5') . ', ' . Json::encode('article') . ');');
$this
->assertSession()
->responseContains('ga("set", ' . Json::encode('dimension6') . ', ' . Json::encode(implode(',', \Drupal::currentUser()
->getRoles())) . ');');
$this
->assertSession()
->responseContains('ga("set", ' . Json::encode('dimension7') . ', ' . Json::encode(implode(',', array_keys(\Drupal::currentUser()
->getRoles()))) . ');');
// Test on a node.
$this
->drupalGet('node/' . $node
->id());
$this
->assertText($node
->getTitle());
$this
->assertSession()
->responseContains('ga("set", ' . Json::encode('dimension5') . ', ' . Json::encode('article') . ');');
}