-
Notifications
You must be signed in to change notification settings - Fork 68
fix(gtm): onBeforeGtmStart gtag function using arguments object #494
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weβll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
- Fix dataLayer initialization by moving it inside gtag function - Add onBeforeGtmStart callback option for custom initialization - Ensure dataLayer exists before each gtag call to prevent errors - Add documentation example showing consent mode configuration Fixes potential race condition where dataLayer might not be initialized when onBeforeGtmStart callback is executed.
@endorfin is attempting to deploy a commit to the Nuxt Team on Vercel. A member of the Team first needs to authorize it. |
commit: |
Thanks for the PR! Can you please check the failing |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
- Fix gtag function in onBeforeGtmStart hook to use native arguments object - GTM DataLayer expects Arguments object, not Array for proper event processing - Enables consent settings to be correctly processed before GTM initialization - Add ESLint disable comment for prefer-rest-params rule
GTM onBeforeGtmStart Fix - Technical AnalysisInitial Hypothesis vs. RealityMy initial assumption was that the issue was related to the DataLayer not being properly initialized when the The Real Root CauseThe problem was related to JavaScript's type system and Google Tag Manager's internal event processing: Working External Implementation:function gtag() {
dataLayer.push(arguments);
} Console Output: Broken Internal Implementation:function gtag(...args) {
dataLayer.push(args);
} Console Output: Why This Difference MattersGoogle Tag Manager's DataLayer specifically expects each gtag call to be pushed as an
When using rest parameters ( Key TakeawayThis was not a timing, initialization, or DataLayer availability issue. It was a pure type compatibility problem between JavaScript's modern rest parameters and Google Tag Manager's legacy API expectations. The bug was difficult to diagnose because the data appeared identical in console logs, but the underlying object types were different enough to break GTM's internal consent processing. SolutionThe fix ensures that GTM receives exactly the data type it expects by using the native |
The example code was missing the gtag parameter in the onBeforeGtmStart callback function, which would cause a ReferenceError when trying to use gtag() for
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks !
I'm not sure why but there was a reason I didn't move GTM to arguments
for gtag. I think it didn't work at that time (but did for GA which required arguments
instead of args.
Looks like an unmentionned breaking change on GTM
π Linked issue
Followup on #392
β Type of change
π Description
arguments
object instead of rest parameters for GTM compatibility