google_analytics_reports-8.x-3.0-rc3/google_analytics_reports.module
google_analytics_reports.module
<?php
/**
* @file
* Front-end interfaces that use the Google Analytics Reports API module.
*/
use Drupal\Core\Routing\RouteMatchInterface;
/**
* Implements hook_help().
*/
function google_analytics_reports_help($route_name, RouteMatchInterface $route_match) {
switch ($route_name) {
case 'help.page.google_analytics_reports':
$output = '';
$output .= '<h3>' . t('About') . '</h3>';
$output .= '<p>' . t('Display statistics from Google Analytics using Views.') . '</p>';
$output .= '<p>' . t('Visit the <a href=":project_link">Project page</a> on Drupal.org for more information.', [
':project_link' => 'https://www.drupal.org/project/google_analytics_reports',
]);
return $output;
}
}
/**
* 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['gaid'] === 'date') {
$field['data_type'] = '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'], TRUE)
) {
$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;
}
