Skip to content

Avoid double GA page views with query on SSG url #12306

Answered by oMatej
valse asked this question in Help
Discussion options

You must be logged in to vote

You can consider doing something like this:

import React from 'react'
import App from 'next/app'
import Router from 'next/router'
import { initGA, logPageView } from '../utils/analytics'

export default class MyApp extends App {
  componentDidMount() {
    const { router } = this.props
    const { asPath, query } = router || {}

    initGA()

    if (!asPath.includes('?') || (asPath.includes('?') && !isEmpty(query))) {
      logPageView()
    }

    Router.events.on('routeChangeComplete', logPageView)
  }

  render() {
    const { Component, pageProps } = this.props
    return <Component {...pageProps} />
  }
}

Replies: 1 comment 3 replies

Comment options

You must be logged in to vote
3 replies
@valse
Comment options

@oMatej
Comment options

@valse
Comment options

Answer selected by valse
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Help
Labels
None yet
2 participants