Problem/Motivation

When a tag container only has a GTM ID (GTM-XXXXX) and no gtag ID (G-XXXXX), events are never pushed to the dataLayer.

This happens because google_tag_page_attachments() wraps the event collection and the gtag library attachment in a if ($config->getDefaultTagId()) check, and getDefaultTagId() explicitly filters out GTM IDs, so the entire event block is skipped.

However, gtag() is just a wrapper around dataLayer.push(), so events pushed via gtag('event', ...) are picked up by GTM containers as well.

There is a @todo comment in the code: "if no config, only send events to datalayer", but gtag() already does that.

Steps to reproduce

  1. Configure a tag container with a GTM ID only.
  2. Enable a commerce event (e.g. add_to_cart).
  3. Add a product to cart.
  4. Check the dataLayer: no event is pushed.

Proposed resolution

  1. Remove the if ($config->getDefaultTagId()) guard in google_tag.module so that the gtag library and events are always attached.
  2. Move gtag('config', ...), the gtag.js script loading and otherIds configuration in js/gtag.js inside the existing if (config.tagId.length !== 0) check, so these only run when a gtag ID is present. Keep events.forEach(...) outside on the check, so events are always pushed regardless of setup.

Remaining tasks

User interface changes

API changes

Data model changes

Issue fork google_tag-3579188

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

alex.bukach created an issue. See original summary.

alex.bukach’s picture

Status: Active » Needs review
alex.bukach’s picture

Status: Needs review » Needs work
Related issues: +#3512549: gtag.js/gtm.js order in D10.4/D11 vs default consent

japerry made their first commit to this issue’s fork.

japerry’s picture

Status: Needs work » Fixed

Confirmed the regression from #3512549: gtag.js/gtm.js order in D10.4/D11 vs default consent); 2.0.8 isn't affected. MR !137's PHP change looks correct to me, but `gtag('js', new Date())` still runs on every page. On a GTM-only page, GTM treats it as a page load ahead of the default consent, which is what #3512549: gtag.js/gtm.js order in D10.4/D11 vs default consent fixed.

I rebased the MR onto 2.0.x, where page_attachments now lives in GoogleTagPageAttachments, and pushed a follow up commit:

  • gtag.js only runs the 'js' and developer_id commands when there's a gtag.js tag ID. A GTM-only page now gets the default consent (if enabled) and the events, and nothing else. Containers with a G- ID push the same dataLayer as before.
  • Added PageAttachmentsHookTest::testGtmOnlyAttachments. It fails on 2.0.x.

gtag() always pushes to `window.dataLayer`, so a GTM container with a custom data layer name won't see these events. That needs its own issue.

Now that this issue is closed, review the contribution record.

As a contributor, attribute any organization that helped you, or if you volunteered your own time.

Maintainers, credit people who helped resolve this issue.

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.