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

后端 未结 6 977
北恋
北恋 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 16:40

    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:

    
    
                                     
                  
提交回复
热议问题