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
- Configure a tag container with a GTM ID only.
- Enable a commerce event (e.g. add_to_cart).
- Add a product to cart.
- Check the dataLayer: no event is pushed.
Proposed resolution
- Remove the
if ($config->getDefaultTagId())guard in google_tag.module so that the gtag library and events are always attached. - 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. Keepevents.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
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
Comment #3
alex.bukach commentedComment #4
alex.bukach commentedComment #5
alex.bukach commentedComment #7
japerryConfirmed 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() 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.