使用 next-redux-wrapper 将 Google Analytics 添加到 Nextjs 应用程序

Posted

技术标签:

【中文标题】使用 next-redux-wrapper 将 Google Analytics 添加到 Nextjs 应用程序【英文标题】:Add Google Analytics to Nextjs app with next-redux-wrapper 【发布时间】:2022-01-20 03:14:26 【问题描述】:

我用 redux 包装器创建了一个 nextjs 应用程序

class MyApp extends React.Component<AppProps> 
   
    public static getInitialProps = wrapper.getInitialAppProps(store => async ( Component, ctx ) => 

我现在正在尝试将谷歌分析集成到应用程序 (G4)。尽管许多文档解释了如何使用 useEffect 添加,但指南在应用扩展组件而不使用钩子时如何使用它们几乎没有脱节。

我需要使用 componentMount 方法还是存在更好的方法,例如将此实现升级为功能组件?

【问题讨论】:

【参考方案1】:

将应用程序转换为功能组件并使用useEffect

  const MyApp = (AppProps) => 
      const router = useRouter();
      useEffect(() => 

然后

MyApp.getInitialProps = wrapper.getInitialAppProps(store => async ( Component, ctx ) => 

【讨论】:

以上是关于使用 next-redux-wrapper 将 Google Analytics 添加到 Nextjs 应用程序的主要内容,如果未能解决你的问题,请参考以下文章