Set Google's date field data_type to string

Created on 16 July 2025, 3 months ago

Problem/Motivation

Google provides a few date related fields, such as yearMonth and date. The data "yearMonth" is a string, preserving the correct formatting of YYYYMM.

Google claims its "date" field formats it as YYYYMMDD, however due to the data_type being "date", Drupal thinks it's a timestamp and tries to format it as a date, resulting in an incorrect value.

Steps to reproduce

Add the "date" field to a view and observe.

Proposed resolution

Set the data_type for the date field to "string".

There may be other ramifications so I won't provide an MR yet while I test it more thoroughly. To do this, I've added a single line to the following function in google_analytics_reports:

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');
  }
  $fields['date']->data_type = 'string';

  return $fields;
}

Remaining tasks

  • ✅ File an issue
  • ➖ Addition/Change/Update/Fix
  • ➖ Testing to ensure no regression
  • ➖ Automated unit testing coverage
  • ➖ Automated functional testing coverage
  • ➖ UX/UI designer responsibilities
  • ➖ Readability
  • ➖ Accessibility
  • ➖ Performance
  • ➖ Security
  • ➖ Documentation
  • ➖ Code review by maintainers
  • ➖ Full testing and approval
  • ➖ Credit contributors
  • ➖ Review with the product owner
  • ➖ Release notes snippet
  • ❌ Release

API changes

  • N/A

Data model changes

  • N/A

Release notes snippet

  • N/A
📌 Task
Status

Active

Version

4.0

Component

Reports module

Created by

🇦🇺Australia imclean Tasmania

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

Comments & Activities

  • Issue created by @imclean
  • 🇦🇺Australia imclean Tasmania
  • 🇦🇺Australia imclean Tasmania
  • 🇺🇦Ukraine gilmord 🇺🇦Ukraine

    Maybe better alter it on the step of storing field data to the fields table?

    while there is no MR/patch this works:

    /**
     * Implements hook_google_analytics_reports_field_import().
     */
    function YOUR_MODULE_google_analytics_reports_field_import_alter(array &$field) {
      if ($field['gaid'] === 'date') {
        $field['data_type'] = 'string';
      }
    }
    
    
  • 🇦🇺Australia imclean Tasmania

    A another approach is to convert the format to a timestamp and leave the data_type as it is. This allows you to select the date display format within the view.

    /**
     * Implements hook_views_post_execute().
     * 
     * @param ViewExecutable $view
     */
    function MY_MODULE_views_post_execute(ViewExecutable $view) {
      if ($view->id() === 'VIEW_ID' ) {
        foreach ($view->result as &$result) {
          $result->date = strtotime($result->date);
        }    
      }
    }
    
Production build 0.71.5 2024