utmp-php should support googleanalytics defined custom dimensions and metrics by default

Created on 16 November 2021, over 3 years ago
Updated 10 March 2025, 30 days ago

The base Google Analytics module has an admin interface for defining custom dimentions and metrics to send with pageviews. (See screenshot: https://i.imgur.com/A22hvuX.png), but these dimensions and metrics are not respected/sent by default by the utmp-php push method.

ga_push_method_utmp_php_request() should be patched to send any defined dimensions and metrics by default:
(See googleanalytics_page_alter() in googleanalytics.module)

  // PATCH: Add custom dimensions and metrics as defined in Google Analytics base module admin interface.
  // See googleanalytics_page_alter().
  $custom_var = '';
  foreach (array('dimension', 'metric') as $googleanalytics_custom_type) {
    $googleanalytics_custom_vars = variable_get('googleanalytics_custom_' . $googleanalytics_custom_type, array());
    // Are there dimensions or metrics configured?
    if (!empty($googleanalytics_custom_vars)) {
      // Add all the configured variables to the content.
      foreach ($googleanalytics_custom_vars as $googleanalytics_custom_var) {
        // Replace tokens in values.
        $types = array();
        $node = menu_get_object();
        if (is_object($node)) {
          $types += array('node' => $node);
        }
        $googleanalytics_custom_var['value'] = token_replace($googleanalytics_custom_var['value'], $types, array('clear' => TRUE));

        // Suppress empty values.
        if (!drupal_strlen(trim($googleanalytics_custom_var['value']))) {
          continue;
        }

        // Per documentation the max length of a dimension is 150 bytes.
        // A metric has no length limitation. It's not documented if this
        // limit means 150 bytes after url encoding or before.
        // See https://developers.google.com/analytics/devguides/collection/analyticsjs/field-reference#customs
        if ($googleanalytics_custom_type == 'dimension' && drupal_strlen($googleanalytics_custom_var['value']) > 150) {
          $googleanalytics_custom_var['value'] = substr($googleanalytics_custom_var['value'], 0, 150);
        }

        // Cast metric values to data type numeric.
        if ($googleanalytics_custom_type == 'metric') {
          settype($googleanalytics_custom_var['value'], 'float');
        };

        // Add values to request,
        if ($googleanalytics_custom_type == 'dimension') {
          $custom_index = 'cd' . $googleanalytics_custom_var['index'];
        }
        elseif ($googleanalytics_custom_type == 'metric') {
          $custom_index = 'cm' . $googleanalytics_custom_var['index'];
        }
        // Respect any overrides already sent with $data.
        if (!isset($data[$custom_index])) {
          $data[$custom_index] = $googleanalytics_custom_var['value'];
        }
      }
    }
  }
  // END PATCH.
✨ Feature request
Status

Closed: outdated

Version

1.0

Component

Code

Created by

πŸ‡ΊπŸ‡ΈUnited States JordanMagnuson

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

Production build 0.71.5 2024