You are here

public function GoogleAnalyticsCustomDimensionsAndMetricsTest::testGoogleAnalyticsCustomMetrics in Google Analytics 4.x

Same name and namespace in other branches
  1. 8.3 tests/src/Functional/GoogleAnalyticsCustomDimensionsAndMetricsTest.php \Drupal\Tests\google_analytics\Functional\GoogleAnalyticsCustomDimensionsAndMetricsTest::testGoogleAnalyticsCustomMetrics()
  2. 8.2 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 189

Class

GoogleAnalyticsCustomDimensionsAndMetricsTest
Test custom dimensions and metrics functionality of Google Analytics module.

Namespace

Drupal\Tests\google_analytics\Functional

Code

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,
      'name' => 'foo1',
      'value' => '6',
    ],
    2 => [
      'index' => 2,
      'name' => 'foo2',
      'value' => '8000',
    ],
    3 => [
      'index' => 3,
      'name' => 'foo3',
      'value' => '7.8654',
    ],
    4 => [
      'index' => 4,
      'name' => 'foo4',
      'value' => '1123.4',
    ],
    5 => [
      'index' => 5,
      'name' => 'foo5',
      'value' => '5,67',
    ],
  ];
  $this
    ->config('google_analytics.settings')
    ->set('custom.metric', $google_analytics_custom_metric)
    ->save();
  $this
    ->drupalGet('');
  $custom_map = [];
  $custom_vars = [];
  foreach ($google_analytics_custom_metric as $metric) {
    $custom_map['custom_map']['metric' . $metric['index']] = $metric['name'];
    $custom_vars[$metric['name']] = (double) $metric['value'];
  }
  $this
    ->assertRaw('gtag("config", ' . Json::encode($ua_code) . ', ' . Json::encode($custom_map) . ');');
  $this
    ->assertRaw('gtag("event", "custom", ' . Json::encode($custom_vars) . ');');

  // Test whether tokens are replaced in custom metric values.
  $google_analytics_custom_metric = [
    1 => [
      'index' => 1,
      'name' => 'bar1',
      'value' => '[current-user:roles:count]',
    ],
    2 => [
      'index' => 2,
      'name' => 'bar2',
      'value' => mt_rand(),
    ],
    3 => [
      'index' => 3,
      'name' => 'bar3',
      'value' => '',
    ],
    // #2300701: Custom dimensions and custom metrics not outputed on zero
    // value.
    4 => [
      'index' => 4,
      'name' => 'bar4',
      '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
    ->assertRaw(Json::encode('metric1') . ':' . Json::encode($google_analytics_custom_metric['1']['name']));
  $this
    ->assertRaw(Json::encode($google_analytics_custom_metric['1']['name']) . ':');
  $this
    ->assertRaw(Json::encode('metric2') . ':' . Json::encode($google_analytics_custom_metric['2']['name']));
  $this
    ->assertRaw(Json::encode($google_analytics_custom_metric['2']['name']) . ':' . Json::encode($google_analytics_custom_metric['2']['value']));
  $this
    ->assertNoRaw(Json::encode('metric3') . ':' . Json::encode($google_analytics_custom_metric['3']['name']));
  $this
    ->assertNoRaw(Json::encode($google_analytics_custom_metric['3']['name']) . ':' . Json::encode(''));
  $this
    ->assertRaw(Json::encode('metric4') . ':' . Json::encode($google_analytics_custom_metric['4']['name']));
  $this
    ->assertRaw(Json::encode($google_analytics_custom_metric['4']['name']) . ':' . Json::encode(0));
}