“gatsby-plugin-google-analytics”不适用于我的 gatsby-strapi 网站

Ank*_*ain 6 google-analytics gatsby

这是我在 gatsby-config.js 中的代码

module.exports = {
  siteMetadata: {
    title: `title`,
    description: ``,
    author: `@Wavii`,
  },

  plugins: [
    {
      resolve: `gatsby-plugin-google-analytics`,
      options: {
        trackingId: "UA-XXXXXX-XX",
        // Defines where to place the tracking script - `true` in the head and `false` in the body
        head: true,
      },
    },
    `gatsby-plugin-react-helmet`,
    {
      resolve: `gatsby-source-filesystem`,
      options: {
        name: `images`,
        path: `${__dirname}/src/images`,
      },
    },
Run Code Online (Sandbox Code Playgroud)

不知道为什么它不起作用,它甚至没有在我的源代码上显示任何谷歌分析。

提前致谢。

小智 6

使用gatsby-plugin-google-gtag和以下配置来支持 google Analytics 4。把它放在插件列表的顶部,如果没有“anonymize_ip: true”我就无法让它工作。

plugins: [
    {
      resolve: `gatsby-plugin-google-gtag`,
      options: {
        trackingIds: [
          "GA-TRACKING_ID", // Google Analytics / GA
        ],
        pluginConfig: {
          head: true,
          anonymize_ip: true,
        },
      },
    },
    // other plugins
  ],
Run Code Online (Sandbox Code Playgroud)


小智 4

我也无法让它在生产中工作。我也尝试过https://www.gatsbyjs.org/packages/gatsby-plugin-gtag/ - 我听说这是“较新”的方式,我可以使用 Helmet 得到一些结果,如下所示:

  <Helmet>
  <script async src="https://www.googletagmanager.com/gtag/js?id=UA-XXX-X"></script>
  <script type="application/ld+json">{`
      window.dataLayer = window.dataLayer || [];
      function gtag(){dataLayer.push(arguments);}
      gtag('config', 'UA-XXX-X');
  `}</script>
  </Helmet>
Run Code Online (Sandbox Code Playgroud)

...但我还没有在生产中成功测试它。我已经安装了所有谷歌分析插件,尽管调试在头盔的情况下有效,但否则我并没有真正得到好的结果。