public function GoogleAnalyticsCustomDimensionsAndMetricsTest::testGoogleAnalyticsCustomMetrics 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::testGoogleAnalyticsCustomMetrics()
- 4.x tests/src/Functional/GoogleAnalyticsCustomDimensionsAndMetricsTest.php \Drupal\Tests\google_analytics\Functional\GoogleAnalyticsCustomDimensionsAndMetricsTest::testGoogleAnalyticsCustomMetrics()
Tests if custom metrics are properly added to the page.
File
- tests/
src/ Functional/ GoogleAnalyticsCustomDimensionsAndMetricsTest.php, line 152
Class
- GoogleAnalyticsCustomDimensionsAndMetricsTest
- Test custom dimensions and metrics functionality of Google Analytics module.
Namespace
Drupal\Tests\google_analytics\FunctionalCode
public function testGoogleAnalyticsCustomMetrics() {
$ua_code = 'UA-123456-3';
$this
->config('google_analytics.settings')
->set('account', $ua_code)
->save();
// Basic test if the feature works.
$google_analytics_custom_metric = [
1 => [
'index' => 1,
'value' => '6',
],
2 => [
'index' => 2,
'value' => '8000',
],
3 => [
'index' => 3,
'value' => '7.8654',
],
4 => [
'index' => 4,
'value' => '1123.4',
],
5 => [
'index' => 5,
'value' => '5,67',
],
];
$this
->config('google_analytics.settings')
->set('custom.metric', $google_analytics_custom_metric)
->save();
$this
->drupalGet('');
foreach ($google_analytics_custom_metric as $metric) {
$this
->assertSession()
->responseContains('ga("set", ' . Json::encode('metric' . $metric['index']) . ', ' . Json::encode((double) $metric['value']) . ');');
}
// Test whether tokens are replaced in custom metric values.
$google_analytics_custom_metric = [
1 => [
'index' => 1,
'value' => '[current-user:roles:count]',
],
2 => [
'index' => 2,
'value' => mt_rand(),
],
3 => [
'index' => 3,
'value' => '',
],
// #2300701: Custom dimensions and custom metrics not outputed on zero
// value.
4 => [
'index' => 4,
'value' => '0',
],
];
$this
->config('google_analytics.settings')
->set('custom.metric', $google_analytics_custom_metric)
->save();
$this
->verbose('<pre>' . print_r($google_analytics_custom_metric, TRUE) . '</pre>');
$this
->drupalGet('');
$this
->assertSession()
->responseContains('ga("set", ' . Json::encode('metric1') . ', ');
$this
->assertSession()
->responseContains('ga("set", ' . Json::encode('metric2') . ', ' . Json::encode($google_analytics_custom_metric['2']['value']) . ');');
$this
->assertSession()
->responseNotContains('ga("set", ' . Json::encode('metric3') . ', ' . Json::encode('') . ');');
$this
->assertSession()
->responseContains('ga("set", ' . Json::encode('metric4') . ', ' . Json::encode(0) . ');');
}