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

with-google-analytics double tracks page views #20549

Closed
cly opened this issue Dec 28, 2020 · 6 comments
Closed

with-google-analytics double tracks page views #20549

cly opened this issue Dec 28, 2020 · 6 comments
Labels
examples Issue/PR related to examples good first issue Easy to fix issues, good for newcomers

Comments

@cly
Copy link

cly commented Dec 28, 2020

Bug report

Describe the bug

with-google-analytics example double counts page_views. The code

const router = useRouter()
  useEffect(() => {
    const handleRouteChange = (url) => {
      gtag.pageview(url)
    }
    router.events.on('routeChangeComplete', handleRouteChange)
    return () => {
      router.events.off('routeChangeComplete', handleRouteChange)
    }
  }, [router.events])

is not necessary.

To Reproduce

Steps to reproduce the behavior, please provide code snippets or a repository:

  1. Go to with-google-analytics example and run it.
  2. Enable Chrome Extension: GA Debugger
  3. Look at Google Analytics events.
  4. See
    image

Expected behavior

Page views are tracked once.

Screenshots

If applicable, add screenshots to help explain your problem.

System information

  • OS: [e.g. macOS, Windows]
  • Browser (if applies) [e.g. chrome, safari]
  • Version of Next.js: [e.g. 10.0.1]
  • Version of Node.js: [e.g. 12.0.0]
  • Deployment: [e.g. next start, next export, Vercel, other platform]

Additional context

Add any other context about the problem here.

@cly cly added the bug Issue was opened via the bug report template. label Dec 28, 2020
@Timer Timer added good first issue Easy to fix issues, good for newcomers examples Issue/PR related to examples and removed bug Issue was opened via the bug report template. labels Dec 28, 2020
@valse
Copy link
Contributor

valse commented Dec 28, 2020

Hi, take a look to the example using react-ga .

The Chrome extension add a query string that is the cause of the issue: check for it on the App component mount.

@frankjwu
Copy link

frankjwu commented Feb 1, 2021

I'm having this same issue -- @cly did you find a solution for this?

@valse I'm having the issue occur even without the query string.

@cly
Copy link
Author

cly commented Feb 1, 2021

I just removed unnecessary useEffect.

@frankjwu
Copy link

frankjwu commented Feb 1, 2021

Ah yep, that worked -- thanks @cly

@justincohan
Copy link

Some extra info for anyone running into this issue. It looks like with the newer GA- tracking ids the useEffect router handling isn't needed. But for the older UA- tracking id's it is.

@balazsorban44
Copy link
Member

This issue has been automatically locked due to no recent activity. If you are running into a similar issue, please create a new issue with the steps to reproduce. Thank you.

@vercel vercel locked as resolved and limited conversation to collaborators Jan 28, 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

No branches or pull requests

7 participants