I wonder why gtag.js doesn't work when push to dataLayer array of variables instead of 'arguments'? Here is some code example:
window.dataLayer = window.dataLayer || []
function gtag(first: any, second: any) {
window.dataLayer.push([first, second])
}
gtag('js', new Date())
gtag('config', trackingCode)
This only works when I replace '[first, second]' with 'arguments'
We don't know what you're trying to achieve by overwriting the gtag
function so we can't answer the specific question as to what "works"
and what doesn't.
What can be said: arguments
is an Object and GTM expects an object from dataLayer.push
, hence why this follows the intended design (whereas [first, second]
is an Array and thus does not).
Note that the push
call in itself "works":
However you won't be able to read that data via GTM which as explained above expects .push
to pass Objects.
If you want to use the dataLayer in a compeltely free way, you can use the GTM dataLayer helper:
https://github.com/google/data-layer-helper
This does support the array syntax via Meta commands:
dataLayer.push(['abc.push', 4, 5, 6]);
However GTM by default doesn't support reading this data (once again GTM expects an Object so it can extract values based on the object properties), so to read that data via GTM you would need to use the dataLayer helper within GTM tags and variables.