You are here

function GoogleAnalyticsCustomDimensionsAndMetricsTest::testGoogleAnalyticsCustomDimensions in Google Analytics 7.2

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

File

./googleanalytics.test, line 322
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_json_encode('dimension' . $dimension['index']) . ', ' . drupal_json_encode($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_json_encode('dimension1') . ', ' . drupal_json_encode("Value: {$site_slogan}") . ');', '[testGoogleAnalyticsCustomDimensionsAndMetrics]: Tokens have been replaced in dimension value.');
  $this
    ->assertRaw('ga("set", ' . drupal_json_encode('dimension2') . ', ' . drupal_json_encode($googleanalytics_custom_dimension['2']['value']) . ');', '[testGoogleAnalyticsCustomDimensionsAndMetrics]: Random value is shown.');
  $this
    ->assertNoRaw('ga("set", ' . drupal_json_encode('dimension3') . ', ' . drupal_json_encode('') . ');', '[testGoogleAnalyticsCustomDimensionsAndMetrics]: Empty value is not shown.');
  $this
    ->assertRaw('ga("set", ' . drupal_json_encode('dimension4') . ', ' . drupal_json_encode('0') . ');', '[testGoogleAnalyticsCustomDimensionsAndMetrics]: Value 0 is shown.');
}