Skip to content

with-google-analytics double tracks page views #20549

@cly

Description

@cly

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    examplesIssue was opened via the examples template.good first issueEasy to fix issues, good for newcomers

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions