You are here

function GoogleAnalyticsCustomDimensionsAndMetricsTest::testGoogleAnalyticsCustomMetrics in Google Analytics 7.2

Same name and namespace in other branches
  1. 6.4 googleanalytics.test \GoogleAnalyticsCustomDimensionsAndMetricsTest::testGoogleAnalyticsCustomMetrics()

File

./googleanalytics.test, line 389
Test file for Google Analytics module.

Class

GoogleAnalyticsCustomDimensionsAndMetricsTest

Code

function testGoogleAnalyticsCustomMetrics() {
  $ua_code = 'UA-123456-3';
  variable_set('googleanalytics_account', $ua_code);

  // Basic test if the feature works.
  $googleanalytics_custom_metric = array(
    1 => array(
      'index' => 1,
      'value' => '6',
    ),
    2 => array(
      'index' => 2,
      'value' => '8000',
    ),
    3 => array(
      'index' => 3,
      'value' => '7.8654',
    ),
    4 => array(
      'index' => 4,
      'value' => '1123.4',
    ),
    5 => array(
      'index' => 5,
      'value' => '5,67',
    ),
  );
  variable_set('googleanalytics_custom_metric', $googleanalytics_custom_metric);
  $this
    ->drupalGet('');
  foreach ($googleanalytics_custom_metric as $metric) {
    $this
      ->assertRaw('ga("set", ' . drupal_json_encode('metric' . $metric['index']) . ', ' . drupal_json_encode((double) $metric['value']) . ');', '[testGoogleAnalyticsCustomDimensionsAndMetrics]: Metric #' . $metric['index'] . ' is shown.');
  }

  // Test whether tokens are replaced in custom metric values.
  $googleanalytics_custom_metric = array(
    1 => array(
      'index' => 1,
      'value' => '[current-user:roles:count]',
    ),
    2 => array(
      'index' => 2,
      'value' => mt_rand(),
    ),
    3 => array(
      'index' => 3,
      'value' => '',
    ),
    // #2300701: Custom dimensions and custom metrics not outputed on zero value.
    4 => array(
      'index' => 4,
      'value' => '0',
    ),
  );
  variable_set('googleanalytics_custom_metric', $googleanalytics_custom_metric);
  $this
    ->verbose('<pre>' . print_r($googleanalytics_custom_metric, TRUE) . '</pre>');
  $this
    ->drupalGet('');
  $this
    ->assertRaw('ga("set", ' . drupal_json_encode('metric1') . ', ', '[testGoogleAnalyticsCustomDimensionsAndMetrics]: Tokens have been replaced in metric value.');
  $this
    ->assertRaw('ga("set", ' . drupal_json_encode('metric2') . ', ' . drupal_json_encode($googleanalytics_custom_metric['2']['value']) . ');', '[testGoogleAnalyticsCustomDimensionsAndMetrics]: Random value is shown.');
  $this
    ->assertNoRaw('ga("set", ' . drupal_json_encode('metric3') . ', ' . drupal_json_encode('') . ');', '[testGoogleAnalyticsCustomDimensionsAndMetrics]: Empty value is not shown.');
  $this
    ->assertRaw('ga("set", ' . drupal_json_encode('metric4') . ', ' . drupal_json_encode(0) . ');', '[testGoogleAnalyticsCustomDimensionsAndMetrics]: Value 0 is shown.');
}