Problem/Motivation
Hello. I have a question about the datalayer variable and the gtag function. Is there a specific reason why, even with gtm without gtag enabled, the datalayer events are pushed via gtag function instead directly via datalayer variable?
The main difference between them is that with gtag() you get Arguments, and if you'd push directly via datalayer, you'd get an object. And then when extracting them in gtm data layer variables in the container, you must use eventModel to get the data of the objects; instead of direct key path to the desired variable.
The problem comes that eventModel.[property] overwrites the tagged variable on each event, regardless if the property exists or not in the new event. Whereas with the direct path to the property, if the next event does not have the same keys, it will not overwrite the variables.
Not saying that is wrong, but I do have a debate with someone saying it is not the correct way to use gtag(), if you use gtm only; and was wondering if the gtag function was chosen with a certain purpose or just because google suggests it for gtag and to have same funtions for any of the options?
Thanks,
Comments
Comment #2
westlinnsamething commentedI believe there's confusion aroung GTAG because users are told that they have to choose between the two to install GA4, leading people to believe the two are mutually exclusive.
But aside from installation, GTAG is the way developers push data from a webpage to the dataLayer, and GTM is used to retrieve values.
GTAG is used because it is a wrapper function for datalayer.push
In essence, both datalayer.push() and gtag() are ways to get data into the data layer, which is then used by GTM and other tools to collect and process information for analytics and TM and GTAG can both be used to install GA4 on webpages, but they tend to be used by different audiences: GTM by analysts, and GTAG by developers.
datalayer events are still pushed via gtag function because its usually developers coding a web page
Once GA4 is installed, both GTM and GTAG can be used to communicate with the dataLayer.
(The dataLayer is a Javascript global variable/object which holds values and serves as the middleman between a webpage and GTM/GA4).
Typically, GTAG is used to send fata to the data layer and typically GTM is used to retrieve values from the dataLayer.
GTM and GTAG were presented as mutually exclusive choices, but the two can work togerher on the csame page.
Google reports
The datalayer and gtag() function are used to send data to tags and analytics tools in Google Tag Manager (GTM).
gtag() is a wrapper for datalayer.push(), meaning that both effectively push data to the data layer.
Using gtag() can be preferable because it helps avoid potential issues with data layer modification or corruption, which could affect analytics data.
Here's a more detailed explanation:
Data Layer: marketing purposes.
Benefits of using gtag():
Simplified tagging:
gtag() provides a streamlined way to send events and other data to various Google products, reducing the need to manage multiple tags.
Future-proofing:
gtag() is designed to be compatible with new features and integrations, making it easier to adapt to changes in Google products.
Reduced code clutter:
Using gtag() can help to avoid having separate scripts for different Google products, simplifying the codebase.
In most cases, using gtag() is the recommended approach for pushing data to the data layer.
Comment #3
japerryThis is by design. Events go through gtag('event', ...), the standard dataLayer.push(arguments) wrapper, so one code path works for both gtag.js and GTM. GTM handles the
eventcommand natively, which is why the values show up undereventModeland are replaced rather than merged. Switching to plain object pushes would break every existing container built oneventModel.*variables.If you want object-style data, read the values with an "Event Model" Data Layer Variable or a Custom JavaScript variable (and turn off "Set default value" where you don't want them cleared), or push your own objects from a custom script. GTM-only containers getting no events since #3512549: gtag.js/gtm.js order in D10.4/D11 vs default consent is tracked in #3579188: Events do not fire in GTM-only setup.