formerly I was using react-ga npm module to insert google analytics in my next js app. and It was simply like this:
import ReactGA from \'react-ga\'
export cons
I found a better way of implementing it. We can use react-gtm-module
library for it.
As Ryan Elainska mentions in their blog, Google Tag Manager in Next.js in the _app.js
file of my NextJS installation:
import App, { Container } from 'next/app'
import React from 'react'
import './app.css'
import TagManager from 'react-gtm'
const tagManagerArgs = {
id: 'GTM-XXXXXXX'
}
class MyApp extends App {
componentDidMount () {
TagManager.initialize(tagManagerArgs)
}
render () {
const { Component, pageProps } = this.props
return (
)
}
}
export default MyApp