Question: why use gtag() function instead of using just dataLayer.push()?

Created on 6 February 2025, 4 months ago

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,

💬 Support request
Status

Active

Version

2.0

Component

Code

Created by

🇷🇴Romania dragos-dumi

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

Comments & Activities

  • Issue created by @dragos-dumi
  • Google could have communicated more effectively: 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: The data layer is a JavaScript object that stores information about a webpage and user interactions.
    datalayer.push(): This method adds data to the data layer.

    gtag(): This function is a wrapper that pushes data to the data layer using datalayer.push(). It's designed to be a more robust and easier way to push data to the data layer for tagging and analytics purposes.
    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 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.

Production build 0.71.5 2024