- 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.
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.