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
Since you are using nextJS, there is no need to add pure javascript from Google Analytics, all you need to have is the GA tracking id.
Then you create util function:
export const event = ({ action, category, label, value }) => {
window.gtag('event', action, {
event_category: category,
event_label: label,
value: value
})
}
and add this in your _document.js:
After that, you just need to import the util function in your page/ components and call:
gtag.event({
action: 'submit_form',
category: 'Contact',
label: this.state.message
})
Reference: example of NextJS with GA