Currently the tokens for custom dimensions are limited to node tokens only, I'd like to use term an field collection tokens. Therefore it would be great to have the possibility to alter the data before token replace.

Comments

osopolar created an issue. See original summary.

osopolar’s picture

Status: Active » Needs review
StatusFileSize
new1.23 KB

This patch adds the hook_googleanalytics_custom_type_token_data_types_alter(). See below for an example how to implement this hook. To be able to use other tokens the settings form needs to be altered too, as below.

/**
 * Implements hook_googleanalytics_custom_type_token_data_types_alter().
 */
function custom_module_googleanalytics_custom_type_token_data_types_alter(&$types, $googleanalytics_custom_type) {

  // Check if menu object is taxonomy term.
  if (empty($types)) {
    $term = menu_get_object('taxonomy_term', 2);
    if (is_object($term)) {
      $types += array('term' => $term);
    }
  }

  // Check if menu object is fieldcollection item.
  if (empty($types)) {
    $field_collection_item = menu_get_object('field_collection_item', 2);
    if (is_object($field_collection_item)) {
      $types += array('field_collection_item' => $field_collection_item);
    }
  }
}

/**
 * Implements hook_googleanalytics_admin_settings_form_alter().
 */
function custom_module_form_googleanalytics_admin_settings_form_alter(&$form, &$form_state) {
  // Alter settings form to enable all token types for custom dimensions.
  for ($i = 1; $i <= 20; $i++) {
    $form['googleanalytics_custom_dimension']['indexes'][$i]['value']['#token_types'] = array('all');
  }
  if (module_exists('token')) {
    $form['googleanalytics_custom_dimension']['googleanalytics_token_tree']['#token_types'] = 'all';
  }
}
hass’s picture

Status: Needs review » Closed (duplicate)
Parent issue: » #231451: Add hook to alter data before sending it to browser