function GoogleAnalyticsCustomDimensionsAndMetricsTest::testGoogleAnalyticsCustomMetrics in Google Analytics 6.4
Same name and namespace in other branches
- 7.2 googleanalytics.test \GoogleAnalyticsCustomDimensionsAndMetricsTest::testGoogleAnalyticsCustomMetrics()
File
- ./
googleanalytics.test, line 351 - Test file for Google Analytics module.
Class
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_to_js('metric' . $metric['index']) . ', ' . drupal_to_js((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' => '[site-date-raw]',
),
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_to_js('metric1') . ', ', '[testGoogleAnalyticsCustomDimensionsAndMetrics]: Tokens have been replaced in metric value.');
$this
->assertRaw('ga("set", ' . drupal_to_js('metric2') . ', ' . drupal_to_js($googleanalytics_custom_metric['2']['value']) . ');', '[testGoogleAnalyticsCustomDimensionsAndMetrics]: Random value is shown.');
$this
->assertNoRaw('ga("set", ' . drupal_to_js('metric3') . ', ' . drupal_to_js('') . ');', '[testGoogleAnalyticsCustomDimensionsAndMetrics]: Empty value is not shown.');
$this
->assertRaw('ga("set", ' . drupal_to_js('metric4') . ', ' . drupal_to_js(0) . ');', '[testGoogleAnalyticsCustomDimensionsAndMetrics]: Value 0 is shown.');
}