How to set up Google Analytics through Google Tag Manager for Next-Js? : <p>formerly I was using react-ga npm module to insert google analytics in my next js app. and It was simply like this:</p>

<pre><code>import ReactGA from 'react-ga'

export const initGA = () =&gt; {
  ReactGA.initialize('UA-*******-*', {
    titleCase: false
  })
}

export const logPageView = () =&gt; {
  if (window.location.href.split('?')[1]) {
    ReactGA.set({page: window.location.pathname + '?' + window.location.href.split('?')[1]})
    ReactGA.pageview(window.location.pathname + '?' + window.location.href.split('?')[1])
  } else {
    ReactGA.set({page: window.location.pathname})
    ReactGA.pageview(window.location.pathname)
  }
}
</code></pre>

<p>and then I was calling logPageView function in my header(that was inserted to every page of my app) like this:</p>

<pre><code>  componentDidMount () {
    if (!window.GA_INITIALIZED) {
      initGA()
      window.GA_INITIALIZED = true
    }
    logPageView()
  }
  componentWillReceiveProps () {
    if (!window.GA_INITIALIZED) {
      initGA()
      window.GA_INITIALIZED = true
    }
    logPageView()
  }
</code></pre>

<p>now I want to use Google Tag Manager to handle Analytics page view . How could I do this?</p>
