Add validation to Google Tag ID input to remove initial/trailing spaces when applying Google Analytics recipe

Created on 8 May 2025, 3 days ago

Problem/Motivation

When entering a Google Tag ID in the Google Analytics recommended recipe (Google Tag module configuration), whitespace is currently allowed.

If a user accidentally includes a leading or trailing space (e.g. when pasting a GTM Container ID like GTM-XXXXXX), this is passed through to the rendered

tag. (When a user highlights the GTM ID in Google Tag Manager, an initial space is included when copying the value.) The space results in an invalid script URL and breaks tag loading, leading to confusing and difficult-to-diagnose errors. For example:
<script async src="https://www.googletagmanager.com/gtm.js?id= GTM-XXXXXXX" type="text/javascript"></script>
Notice the space after id= in the src string.

Steps to reproduce

0. Install Drupal CMS. 1. Turn off JS Aggregation so that we can see the individual script tags for troubleshooting. (Configuration > Development > Performance > Aggregation > Toggle off JS Aggregation) 2. Go to Google Tag Manager and create a new container for a website. - Select "Web" as the target platform. - After creation, note the Container ID (formatted like GTM-XXXXXXX). 3. Go to Google Analytics and create a new GA4 property for the same site. This step is optional for reproducing the bug, but is typically done in real-world usage when configuring Google Analytics via GTM. 4. In Drupal CMS, install the Google Analytics recommended add-on (recipe). (Extend > Recommended > Google Analytics > Install or Reinstall) This will install the Google Tag module and prompt for a Google Tag ID at admin/config/services/google-tag. 5. In Google Tag Manager, copy the container ID on the Workspace tab > Overview page. Paste the GTM Container ID into the Google Tag ID field. For me, an initial space was included when I pasted. To reproduce the bug, ensure there is a space before the GTM ID. 6. Visit the front page of the site and log out. Return to the front page. 7. When the Klaro privacy consent window pops up, click Customize. Toggle on all categories so that the Google Analytics cookies will be visible. (Only functional cookies are enabled by default.) 8. Inspect source and look for the following tag:

9. Notice the space after the id in the src string.

Proposed resolution

When configuring Google Tag module, the Google Analytics recipe should automatically trim whitespace or validate the field to avoid accidental invalid input for the Google Tag ID config.

User interface changes

- Add validation to Google Tag ID field.

✨ Feature request
Status

Active

Component

Track: Analytics

Created by

πŸ‡ΊπŸ‡ΈUnited States Amber Himes Matz Portland, OR USA

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

Comments & Activities

  • Issue created by @Amber Himes Matz
  • πŸ‡ΊπŸ‡ΈUnited States Amber Himes Matz Portland, OR USA
  • πŸ‡ΊπŸ‡ΈUnited States Amber Himes Matz Portland, OR USA
  • πŸ‡ΊπŸ‡ΈUnited States phenaproxima Massachusetts

    Validation can be extremely tricky for recipe inputs, because the default value has to fulfill the constraints too. Unfortunately, something like Google Tag really doesn't have a valid default value. Additionally, recipes don't support input transformation. We could validate that the tag ID doesn't contain whitespace (an empty string would still pass that constraint), but we probably can't actively strip it out.

  • πŸ‡ΊπŸ‡ΈUnited States Amber Himes Matz Portland, OR USA

    My perspective is from the documentation writer's point of view. To have to say, "Make sure you don't have any leading or trailing spaces when you enter your GTM ID" in documentation for setting up analytics is weird to me. That just smacks of a programmer's problem, not a user's problem.

    Since the Google Tag module is responsible for constructing the string for the script tag's src attribute, maybe this issue would be better filed in their queue instead. And instead of suggestion validation, suggesting trimming leading/trailing whitespace from the tagId variable.

    web/modules/contrib/google_tag/js/gtm.js, lines 23-30:

    config.tagIds.forEach(function (tagId) {
        const script = document.createElement('script');
        script.async = true;
        const dLink = dl != 'dataLayer' ? `&l=${dl}` : '';
        script.src = `https://www.googletagmanager.com/gtm.js?id=${tagId}${gtm_environment}${dLink}`;
        script.type = 'text/javascript';
        document.getElementsByTagName('head')[0].appendChild(script);
      });

    @phenaproxima Ok with me to close as won't fix and I'll file it in the Google Tag module's queue instead.

  • πŸ‡ΊπŸ‡ΈUnited States phenaproxima Massachusetts

    Sure, that sounds good. We could also just repurpose this issue and move it to Google Tag's queue, so they have the context for why we're doing this.

  • πŸ‡ΊπŸ‡ΈUnited States Amber Himes Matz Portland, OR USA

    Updated title and issue summary.

    I'm not sure how to move this to another project's queue. Could use some help with that.

  • πŸ‡ΊπŸ‡ΈUnited States Amber Himes Matz Portland, OR USA
  • πŸ‡ΊπŸ‡ΈUnited States phenaproxima Massachusetts

    Fixed. :) The "Project" field in the issue metadata is where you control that.

  • πŸ‡ΊπŸ‡ΈUnited States Amber Himes Matz Portland, OR USA

    Thanks! I just saw that a moment too late.

  • πŸ‡ͺπŸ‡ΈSpain penyaskito Seville πŸ’ƒ, Spain πŸ‡ͺπŸ‡Έ, UTC+2 πŸ‡ͺπŸ‡Ί

    Maybe this could happen on preSave instead of the rendering?

    Actually, the recipe validates with:

          Regex: '/^(?:GT|UA|G|AW|DC|GTM)-[0-9a-zA-Z]{4,}(?:-[0-9]{1,})?$/'
    

    Wouldn't that be enough?

  • πŸ‡ͺπŸ‡ΈSpain penyaskito Seville πŸ’ƒ, Spain πŸ‡ͺπŸ‡Έ, UTC+2 πŸ‡ͺπŸ‡Ί

    Even if we still want this fixed in Google Tag, this would alleviate the issue on the Drupal CMS recipe with HTML5 validation:

        form:
          '#type': 'textfield'
          '#title': 'Google Tag ID'
    +      '#pattern': '/^(?:GT|UA|G|AW|DC|GTM)-[0-9a-zA-Z]{4,}(?:-[0-9]{1,})?$/'
          '#description': "The Google Tag ID for tracking. This ID is unique to each site you want to track separately, and must be in the form of GT-xxxxxx, G-xxxxxxxx, AW-xxxxxxxxx, GTM-xxxxxxxx or DC-xxxxxxxx."
    
    
Production build 0.71.5 2024