前言
由于只是个小博客,没什么人看。数据都是自己本地贡献不太好。
所以打算本地跑博客的时候关闭谷歌分析等数据分析。

修改命令

1
2
3
4
"scripts": {
"dev": "hexo server --env development",
"build": "hexo generate --env production"
}

这里--env development--env production 就是用来区分开发环境和生产环境。

修改主题代码

  1. 进入到下面文件中themes/butterfly/layout/includes/head/analytics.pug
  2. 在顶部添加下面代码if env.args.env === 'production'
  3. 对除了添加代码进行缩进

修改后的代码如下

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
if env.args.env === 'production'
if theme.baidu_analytics
script.
var _hmt = _hmt || [];
(function() {
var hm = document.createElement("script");
hm.src = "https://hm.baidu.com/hm.js?!{theme.baidu_analytics}";
var s = document.getElementsByTagName("script")[0];
s.parentNode.insertBefore(hm, s);
})();
btf.addGlobalFn('pjaxComplete', () => {
_hmt.push(['_trackPageview',window.location.pathname])
}, 'baidu_analytics')

if theme.google_analytics
script(async src=`https://www.googletagmanager.com/gtag/js?id=${theme.google_analytics}`)
script.
window.dataLayer = window.dataLayer || []
function gtag(){dataLayer.push(arguments)}
gtag('js', new Date())
gtag('config', '!{theme.google_analytics}')
btf.addGlobalFn('pjaxComplete', () => {
gtag('config', '!{theme.google_analytics}', {'page_path': window.location.pathname})
}, 'google_analytics')

if theme.cloudflare_analytics
script(defer data-pjax src='https://static.cloudflareinsights.com/beacon.min.js' data-cf-beacon=`{"token": "${theme.cloudflare_analytics}"}`)

if theme.microsoft_clarity
script.
(function(c,l,a,r,i,t,y){
c[a]=c[a]||function(){(c[a].q=c[a].q||[]).push(arguments)};
t=l.createElement(r);t.async=1;t.src="https://www.clarity.ms/tag/"+i;
y=l.getElementsByTagName(r)[0];y.parentNode.insertBefore(t,y);
})(window, document, "clarity", "script", "!{theme.microsoft_clarity}");

这样就配置完成了