Google Tag script not appearing with negated response codes

Created on 26 October 2023, about 1 year ago
Updated 8 April 2024, 8 months ago

Problem/Motivation

Google tag scripts are not appearing on pages.

Installed version 2.0.2
Drupal 9.5.11
PHP 8.1.22

Our GTM-xxx tag is set in the module. We have one container which is set as follows:

  • No custom metrics
  • Search event activated
  • Response code 403, 404 excluded
  • Default set of request paths excluded
  • Active for Anonymous user role only.
  • Active for the correct content types, including Article and Homepage.
  • No active vocabularies
  • All webforms included

Steps to reproduce

Install Google Tag and add a GTM container id.
Activate for Homepage content type.

Visit home page and inspect source.

The google tag js script tag is not present.

Proposed resolution

Script tag should be present on the included content-type pages.

This was definately working before in the 1.x versions No changes have been made execpt updating the module to the 2.x branch.

πŸ› Bug report
Status

Needs work

Version

2.0

Component

Code

Created by

πŸ‡¨πŸ‡¦Canada jodavidson

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

Comments & Activities

  • Issue created by @jodavidson
  • πŸ‡¦πŸ‡ͺUnited Arab Emirates ThirstySix

    Yes, it's not working.

  • πŸ‡ΊπŸ‡ΈUnited States g.paluch

    Confirming, after switching from 1.x to 2.0.2 I have the same issue, google tag js script tag is not present.

  • πŸ‡ΊπŸ‡ΈUnited States g.paluch

    Uninstalling and installing back module fixed the issue for me.

  • πŸ‡ΊπŸ‡ΈUnited States ddavisboxleitner

    On our project the issue appeared to be a container configuration file which was created using the 1.x version and when imported into the 2.x version caused the problem. After importing the 'stale' config the GTM snippet no longer appeared on the page.

    The problem file was named like `google_tag.container.your_container_name.yml` and deleting the file, then uninstalling/re-installing google_tag module and importing config again resolved the issue.

    We recommend checking your configs for any containers created in the old version of the module and try removing those config files, then re-import.

  • πŸ‡¨πŸ‡¦Canada jodavidson

    We recommend checking your configs for any containers created in the old version of the module and try removing those config files, then re-import.

    Daniel, that seems reasonable. I've reverted to get things going again (marketing gets a little huffy when their tracking isn't working.)

    I'll give it a go on a dev site and report back. If this is the fix the doumentation should be updated to note the process.

  • πŸ‡¨πŸ‡¦Canada nbeaucage Montreal

    This happened to us just now, and we found out that our container configuration was not properly setting the user_role condition.

    Previously we were matching the anonymous user role only (for the condition), but since upgrading to the 2.x version of the module, the negate sub-property was set to true instead of our initial/expected false value.

    So instead of having GTM fire off for anonymous users, it was firing for all authenticated users.

    Setting the conditions.user_role.negate YAML property to false, and then re-importing our container configuration file fixed the issue for us (with a subsequent cache rebuild).

    We feel that the upgrade process from 1.x to 2.x wrongly translated the YAML property role_toggle: 'include listed' to conditions.user_role.negate: true instead of negate: false. Might be wrong about this though, just throwing out some ideas.

  • We are also running into the script not appearing on our page.

    Debugging the issue, it appears that some conditions in TagContainerResolver::passesConditions were throwing MissingValueContextException exceptions. Since this sets $missing_value to FALSE, the function returns FALSE and the tag isn't included.

    Taking inspiration from Issue 3100588 β†’ , we have patched the code to ignore when $missing_value is FALSE and the tag is now included.

    It appears to be working for us, but we are not sure of the broader implications.

  • πŸ‡ΊπŸ‡ΈUnited States jjeffrie

    I recently transitioned from Google Analytics to Google Tag and have also encountered this bug. I decided to add the

    and tags to my theme template file (html.html.twig). I can configure the tag behavior from the Google Tag Manager portal.
  • I am also having this issue when trying to both add a new gtm container to a new site and while editing any existing containers as well. william.thomas.cox #8 πŸ› Google Tag script not appearing on page Active I tried your solution but this did not work for me. Is there any way you could post your patch? I am curious to see if there is anything you did differently.

  • πŸ‡ΊπŸ‡ΈUnited States firewaller

    @william.thomas.cox can you post your patch that fixed this?

  • πŸ‡ΊπŸ‡ΈUnited States firewaller

    I think what's happening is if any of the conditions fail then the tag won't show up (i.e. you can't use both a positive node and a term condition). This logic likely needs to be revisited: https://git.drupalcode.org/project/google_tag/-/blob/2.0.x/src/TagContai...

  • Status changed to Needs review 8 months ago
  • Open in Jenkins β†’ Open on Drupal.org β†’
    Core: 10.2.x + Environment: PHP 8.1 & MySQL 5.7
    last update 8 months ago
    52 pass, 1 fail
  • πŸ‡ΊπŸ‡ΈUnited States mikeryan Murphysboro, IL, USA

    The key point from the issue description is "Response code 403, 404 excluded" - we have the exact same situation. The problem is simply that the ResponseCode condition plugin is not applying negation. Patch attached.

Production build 0.71.5 2024