你能在没有服务器端代码的情况下统计网站的唯一访问者吗



我为我的朋友创建了一个Gatsby博客,这意味着它是无服务器的,不使用数据库。这对于一个博客来说非常有效,因为每当我做出更改或他添加博客文章时,它只需要构建——然而,对于跟踪独特的用户访问来说,它就不太好了,因为如果在短时间内有很多人访问,它就必须进行太多的构建。

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

如前所述,您需要第三方工具(如Google Analytics(,因为在前端部分完成的任何解决方案都将与客户端/浏览器端相关,例如,如果用户更改设备,您将失去跟踪功能。

您可以使用一些可用的插件(如gatsby-plugin-google-gtag(轻松安装Analytics。这是推荐的,因为under the hood使用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/"],
},
},
},
],
}

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

相关内容

最新更新