How to set up Google Analytics through Google Tag Manager for Next-Js?

后端 未结 6 1069
北恋
北恋 2021-02-13 16:24

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         


        
6条回答
  •  天命终不由人
    2021-02-13 17:02

    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
    

提交回复
热议问题