博客搭建 (三) 链接google分析

如何添加 google analytics

如果想使用流量分析工具来统计网站的流量,还需要把它添加进next中。这里我以google analytics为例,看看怎样把它添加到Next中。

工作原理,配置谷歌分析之后,next会自动在blog的html中插入一段js代码(gtags.js),这一小段信息会将用户的数据发送到谷歌进行流量分析。如图:

获取GA ID: G-xxxxx

根据网站上的指引,创建数据流,然后就可以拿到ID了。ID是以G开头的一串字符。

更改Next配置文件

在next的_config.yml中添加以下的配置

1
2
3
4
5
6
7
# Google Analytics
# See: https://analytics.google.com
google_analytics:
tracking_id: <app_id>
# By default, NexT will load an external gtag.js script on your site.
# If you only need the pageview feature, set the following option to true to get a better performance.
only_pageview: false

更改Hexo配置文件的Url

在_config.yml中url的默认设置为example.com,在这里我们需要改成自己的host。否则gtag.js不会启动。

1
url: https://justdong.net

本系列文章:
博客搭建 (一) 开始
博客搭建 (二) 链接谷歌域名
博客搭建 (三) 链接google分析
博客搭建 (四) 显示文章的字数和阅读时长
博客搭建 (五) 接入 Google AdSense (NexT v8.2.1)