javascript - 您可以在没有服务器端代码的情况下计算网站的唯一访问者吗?

标签 javascript reactjs gatsby user-tracking

我为我的 friend 创建了一个 Gatsby 博客,这意味着它是无服务器的,并且不使用数据库。这对于博客来说效果很好,每次我进行更改或他添加博客文章时只需要构建 - 但是它对于跟踪唯一用户访问不太有效,因为如果有很多人访问它就必须进行太多构建短时间内。

有没有一种方法可以在不涉及数据库的情况下跟踪唯一用户访问?

最佳答案

就像已经说过的那样,您需要第三方工具(例如 Google Analytics),因为在前端部分完成的任何解决方法都将与客户端/浏览器端相关,那么您将失去跟踪,如果例如,用户更改设备。

您可以使用一些可用插件轻松安装 Analytics(例如 gatsby-plugin-google-gtag )。建议这样做,因为在幕后使用 gtag.js 而不是 analytics.js,这是 Google 由于上次 API 更改而建议的做法。

使用时只需安装它:

npm install gatsby-plugin-google-gtag // or yarn add gatsby-plugin-google-gtag

并添加您的配置:

// In your gatsby-config.js
module.exports = {
  plugins: [
    {
      resolve: `gatsby-plugin-google-gtag`,
      options: {
        // You can add multiple tracking ids and a pageview event will be fired for all of them.
        trackingIds: [
          "GA-TRACKING_ID", // Google Analytics / GA
        ],
        // This object gets passed directly to the gtag config command
        // This config will be shared across all trackingIds
        gtagConfig: {
          optimize_id: "OPT_CONTAINER_ID",
          anonymize_ip: true,
          cookie_expires: 0,
        },
        // This object is used for configuration specific to this plugin
        pluginConfig: {
          // Puts tracking script in the head instead of the body
          head: false,
          // Setting this parameter is also optional
          respectDNT: true,
          // Avoids sending pageview hits from custom paths
          exclude: ["/preview/**", "/do-not-track/me/too/"],
        },
      },
    },
  ],
}

您可以忽略不需要的选项。

关于javascript - 您可以在没有服务器端代码的情况下计算网站的唯一访问者吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68451222/

相关文章:

javascript - 从带线的数据转换日期和时间

node.js - 我如何在 gatsby-node.js 中使用多个 createPage 路由

reactjs - 如何使用 React gatsby-background-image 重复背景图像?

JavaScript (ES6) : any more efficient way than "for loops"?

javascript - 如何将实例变量从 React 组件传递给它的 HOC?

javascript - 抑制 onChange 触发 react

javascript - 准备要传递到 API 查询的字符串(转义特殊字符)

javascript - 使文本在 jQuery Sortable parent 的 textarea 中可选择

javascript - 单选按钮(如果单击)

javascript - 与电子邮件正则表达式作斗争;你能帮我吗?