You are here

function GoogleAnalyticsCustomDimensionsAndMetricsTest::testGoogleAnalyticsCustomDimensions in Google Analytics 6.4

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

File

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

Class

GoogleAnalyticsCustomDimensionsAndMetricsTest

Code

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

  // Basic test if the feature works.
  $googleanalytics_custom_dimension = array(
    1 => array(
      'index' => 1,
      'value' => 'Bar 1',
    ),
    2 => array(
      'index' => 2,
      'value' => 'Bar 2',
    ),
    3 => array(
      'index' => 3,
      'value' => 'Bar 3',
    ),
    4 => array(
      'index' => 4,
      'value' => 'Bar 4',
    ),
    5 => array(
      'index' => 5,
      'value' => 'Bar 5',
    ),
  );
  variable_set('googleanalytics_custom_dimension', $googleanalytics_custom_dimension);
  $this
    ->drupalGet('');
  foreach ($googleanalytics_custom_dimension as $dimension) {
    $this
      ->assertRaw('ga("set", ' . drupal_to_js('dimension' . $dimension['index']) . ', ' . drupal_to_js($dimension['value']) . ');', '[testGoogleAnalyticsCustomDimensionsAndMetrics]: Dimension #' . $dimension['index'] . ' is shown.');
  }

  // Test whether tokens are replaced in custom dimension values.
  $site_slogan = $this
    ->randomName(16);
  variable_set('site_slogan', $site_slogan);
  $googleanalytics_custom_dimension = array(
    1 => array(
      'index' => 1,
      'value' => 'Value: [site-slogan]',
    ),
    2 => array(
      'index' => 2,
      'value' => $this
        ->randomName(16),
    ),
    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_dimension', $googleanalytics_custom_dimension);
  $this
    ->verbose('<pre>' . print_r($googleanalytics_custom_dimension, TRUE) . '</pre>');
  $this
    ->drupalGet('');
  $this
    ->assertRaw('ga("set", ' . drupal_to_js('dimension1') . ', ' . drupal_to_js("Value: {$site_slogan}") . ');', '[testGoogleAnalyticsCustomDimensionsAndMetrics]: Tokens have been replaced in dimension value.');
  $this
    ->assertRaw('ga("set", ' . drupal_to_js('dimension2') . ', ' . drupal_to_js($googleanalytics_custom_dimension['2']['value']) . ');', '[testGoogleAnalyticsCustomDimensionsAndMetrics]: Random value is shown.');
  $this
    ->assertNoRaw('ga("set", ' . drupal_to_js('dimension3') . ', ' . drupal_to_js('') . ');', '[testGoogleAnalyticsCustomDimensionsAndMetrics]: Empty value is not shown.');
  $this
    ->assertRaw('ga("set", ' . drupal_to_js('dimension4') . ', ' . drupal_to_js('0') . ');', '[testGoogleAnalyticsCustomDimensionsAndMetrics]: Value 0 is shown.');
}