You are here

google_analytics_reports.module in Google Analytics Reports 8.3

Same filename and directory in other branches
  1. 7.3 google_analytics_reports.module

Front-end interfaces that use the Google Analytics Reports API module.

File

google_analytics_reports.module
View source
<?php

/**
 * @file
 * Front-end interfaces that use the Google Analytics Reports API module.
 */

/**
 * Implements hook_google_analytics_reports_field_import_alter().
 */
function google_analytics_reports_google_analytics_reports_field_import_alter(&$field) {

  // Change data type for Date field.
  if ($field['id'] == 'date') {
    $field['attributes']['dataType'] = 'date';
  }
}

/**
 * Implements hook_google_analytics_reports_api_reported_data_alter().
 */
function google_analytics_reports_google_analytics_reports_api_reported_data_alter(&$name, &$value) {

  // Get all Google Analytics fields.
  $fields = google_analytics_reports_get_fields();

  // Date and time datatypes should not have the digits after the zero.
  if (isset($fields[$name]) && in_array($fields[$name]->data_type, [
    'date',
    'time',
  ])) {
    $value = round($value);
  }
  switch ($name) {
    case 'userType':
      $value = $value == 'New Visitor' ? t('New Visitor') : t('Returning Visitor');
      break;
    case 'date':
      $value = strtotime($value);
      break;
    case 'yearMonth':
      $value = strtotime($value . '01');
      break;
    case 'userGender':
      $value = $value == 'male' ? t('Male') : t('Female');
      break;
  }
}

/**
 * List of Google Analytics dimensions and metrics.
 *
 * @return array
 *   An associative array containing list of Google Analytics column objects.
 *   Each object is associative array containing:
 *   - gid: The primary identifier for a column.
 *   - type: The type of column.
 *   - data_type: The type of data this column represents.
 *   - column_group: The dimensions/metrics group the column belongs to.
 *   - ui_name: The name/label of the column used in user interfaces (UI).
 *   - description: The full description of the column.
 *   - calculation: This shows how the metric is calculated.
 */
function google_analytics_reports_get_fields() {
  $fields =& drupal_static(__FUNCTION__);

  // todo: fetch data from cache.
  if (!isset($fields)) {
    $fields = \Drupal::database()
      ->select('google_analytics_reports_fields', 'g')
      ->fields('g')
      ->execute()
      ->fetchAllAssoc('gaid');
  }
  return $fields;
}

/**
 * Determines if a field is custom or not.
 */
function google_analytics_reports_is_custom($field) {
  return preg_match('/XX/', $field) ? TRUE : FALSE;
}

/**
 * Converts a base custom field name and number into a specific field name.
 */
function google_analytics_reports_custom_to_variable_field($field, $number) {
  return preg_replace('/XX/', $number, $field);
}

/**
 * Converts a specific field name into a base custom field name.
 */
function google_analytics_reports_variable_to_custom_field($field) {
  return preg_replace('/\\d+/', 'XX', $field);
}

Functions