博客添加访问计数统计

leancloud访问统计

因为用的是fluid主题,直接配置很方便

前提

已经注册了leancloud,使用的是fluid主题

步骤

  1. leancloud创建class

leancloud创建一个应用例如blog visit count,点击新建class之后,命名为Count,权限选择无限制

add class on leancloud

  1. 修改fluid配置文件

进入leancloud应用,选择左下角的 设置 > 应用 Key,可以看到APP ID,APP Key。

_config.fluid.yml在LeanCloud 计数统计处填上以下信息

1
2
3
4
5
6
7
8
9
10
11
web_analytics:  # 网页访问统计
enable: true
...
...

leancloud:
app_id: ""
app_key: ""
server_url: //博客主页URL
path: window.location.pathname
ignore_local: true

修改权限防篡改

参考以下
https://leaferx.online/2018/02/11/lc-security/
https://github.com/fluid-dev/hexo-theme-fluid/issues/763

waline修改数据库

但是由于用的是leancloud国际版,对国内访问不统计,有一些fetch网络报错。所以更换到waline访问统计。并且后端改用postgresql统计速度更快。这里我使用的是supabase,有免费的额度也不用绑卡。

supabase创建项目

新建一个项目,进入首页,左侧栏找到SQL Editor,把这里的waline.pgsql复制下来,填入sql编辑框,点右下角run运行,就可以创建好初始的表格了。到左侧栏找到Table Editor可以看到创建的几个表格。

tables in supabase

在项目的home页点击connect,复制这一段保存,用来填写环境变量
for environment variables

1
user=[PG_USER] password=[PG_USER] host=[PG_USER] port=5432 dbname=postgres

填写vercel环境变量

我把之前的leancloud环境变量删掉了,添加了这几个:

1
2
3
4
5
PG_DB: postgres
PG_USER:
PG_USER:
PG_USER: xxxxx.supabase.com
PG_PORT: 5432

填写完在Deployment重新部署就可以了。如果评论后提示一些错误,检查一下是不是环境变量名称或者数值填错了。如果PG_HOST填错了可能会报错xxx127.0.0.1xxxx,因为默认的是这个值。

以上就完成了评论在后端的数据联动。

访问统计 #TODO

参考链接

https://blog.liluhui.cn/2019/03/17/Leancloud%E5%BF%AB%E9%80%9F%E6%90%9E%E5%AE%9A%E7%BD%91%E7%AB%99%E8%AE%BF%E9%97%AE%E9%87%8F%E7%BB%9F%E8%AE%A1/index.html
https://blog.garryde.com/archives/48665.html
https://finisky.github.io/hexormleancloud/
https://waline.js.org/guide/features/pageview.html#%E5%8D%95%E7%8B%AC%E4%BD%BF%E7%94%A8
https://finisky.github.io/hexormleancloud/


博客添加访问计数统计
https://hexwhat.top/2024/04/09/hexo-blog-count/
作者
Leah
发布于
2024年4月9日
更新于
2024年5月1日
许可协议