Skip to content
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

window.gtag is not defined for Next.js-hydration event #40410

Closed
1 task done
Littletonconnor opened this issue Sep 9, 2022 · 3 comments · Fixed by #40645
Closed
1 task done

window.gtag is not defined for Next.js-hydration event #40410

Littletonconnor opened this issue Sep 9, 2022 · 3 comments · Fixed by #40645
Labels
examples Issue/PR related to examples good first issue Easy to fix issues, good for newcomers

Comments

@Littletonconnor
Copy link

Verify canary release

  • I verified that the issue exists in the latest Next.js canary release

Provide environment information

    Operating System:
      Platform: linux
      Arch: x64
      Version: Ubuntu 20.04.0 LTS Fri Sep 09 2022 11:36:36 GMT-0700 (Pacific Daylight Time)
    Binaries:
      Node: 16.14.2
      npm: 7.17.0
      Yarn: 1.22.10
      pnpm: 7.9.5
    Relevant packages:
      next: 12.3.1-canary.0
      eslint-config-next: N/A
      react: 17.0.1
      react-dom: 17.0.1

Which example does this report relate to?

with-google-analytics

What browser are you using? (if relevant)

No response

How are you deploying your application? (if relevant)

No response

Describe the Bug

calling reportWebVitals when having google analytics setup using with-google-analytics results in Next.js-hydration event getting triggered BEFORE gtag is initialized.

This could be problematic in production if someone is just assuming gtag is always defined when reporting on web vitals.

Expected Behavior

When having google analytics setup using the with-google-analytics example, I would expect gtag to be defined for all events before triggering reportWebVitals.

To Reproduce

Here is a link to reproduce: https://stackblitz.com/edit/nextjs-rnaue8?file=pages%2F_app.js,.env.local.example,lib%2Fgtag.js,pages%2Findex.js

@Littletonconnor Littletonconnor added the bug Issue was opened via the bug report template. label Sep 9, 2022
@balazsorban44 balazsorban44 added examples Issue/PR related to examples and removed bug Issue was opened via the bug report template. labels Sep 10, 2022
@grayaustinc
Copy link

Typically you would want to add a Data Layer.
With this, any commands will be processed after gtag is loaded.

@balazsorban44 balazsorban44 added the good first issue Easy to fix issues, good for newcomers label Sep 12, 2022
@dave-hay
Copy link
Contributor

Hey @Littletonconnor I can work on this one.

@kodiakhq kodiakhq bot closed this as completed in #40645 Sep 23, 2022
kodiakhq bot pushed a commit that referenced this issue Sep 23, 2022
…40645)

Next.js-hydration now triggered after gtag is initialized. Fixed by adding _document.js and appropriate script elements.
---
## Documentation / Examples

- [X] Related issues linked using fixes #40410
- [X] Make sure the linting passes by running `pnpm lint`
- [X] The "examples guidelines" are followed from [our contributing doc](https://github.com/vercel/next.js/blob/canary/contributing/examples/adding-examples.md)

Next.js-hydration now triggered after gtag is initialized.
@github-actions
Copy link
Contributor

This closed issue has been automatically locked because it had no new activity for a month. If you are running into a similar issue, please create a new issue with the steps to reproduce. Thank you.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Oct 23, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
examples Issue/PR related to examples good first issue Easy to fix issues, good for newcomers
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants