hexo+yilia添加百度统计和Google统计

简介

百度统计和Google统计主要用于分析网站数据:流量、访客量等。
file

需要注册账号,然后将代码(脚本)嵌入到网站中,然后在后台就会生成对应数据,然后进行分析。

说明

yilia主题已集成,位置如下,我们只需要在主题的配置文件中进行配置即可。

  • H:\Hexo\themes\yilia\layout\_partial\baidu-analytics.ejs
  • H:\Hexo\themes\yilia\layout\_partial\google-analytics.ejs

配置

H:\Hexo\themes\yilia\_config.yml原内容:

# Miscellaneous
baidu_analytics: ''
google_analytics: ''

主要是配置对应的id即可(google_analytics的代码好像更新了,建议新建,使用新的代码)。

百度

网址生成的原代码(id是一串字符串,这里隐藏了)

<script>
var _hmt = _hmt || [];
(function() {
  var hm = document.createElement("script");
  hm.src = "https://hm.baidu.com/hm.js?id";
  var s = document.getElementsByTagName("script")[0]; 
  s.parentNode.insertBefore(hm, s);
})();
</script>

file

对应的配置文件H:\Hexo\themes\yilia\layout\_partial\baidu-analytics.ejs:

<% 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);
})();
</script>
<% } %>

Google全局网站代码 (gtag.js)

我找了好久才找到代码,因此记录下:

生成代码:控制台-->(左下角)管理-->管理-->(中间)媒体资源-->(中间)跟踪信息-->跟踪代码

file

这是针对此媒体资源的全局网站代码 (gtag.js) 跟踪代码。请复制此代码,并将其作为第一个项目粘贴到您要跟踪的每个网页的<HEAD> 标记中。如果您的网页上已经有全局网站代码,则只需将以下代码段中的 config 行添加到现有的全局网站代码。

<!-- Global site tag (gtag.js) - Google Analytics -->
<script async src="https://www.googletagmanager.com/gtag/js?id=ID"></script>
<script>
  window.dataLayer = window.dataLayer || [];
  function gtag(){dataLayer.push(arguments);}
  gtag('js', new Date());
  gtag('config', 'ID');
</script>

file

对应的配置文件H:\Hexo\themes\yilia\layout\_partial\google_analytics.ejs:

<% if (theme.google_analytics){ %>
<!-- Google Analytics -->
<script type="text/javascript">
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','//www.google-analytics.com/analytics.js','ga');

ga('create', '<%= theme.google_analytics %>', 'auto');
ga('send', 'pageview');

</script>
<!-- End Google Analytics -->
<% } %>

代码好像更新了,yilia主题自带的代码应该是旧的,更换为下面的:

<!-- Global site tag (gtag.js) - Google Analytics -->
<% if (theme.google_analytics){ %>
    <script async src="https://www.googletagmanager.com/gtag/js?id=<%= theme.google_analytics %>"></script>
    <script>
      window.dataLayer = window.dataLayer || [];
      function gtag(){dataLayer.push(arguments);}
      gtag('js', new Date());
      gtag('config', '<%= theme.google_analytics %>');
    </script>
<% } %>
<!-- End Google Analytics -->

配置id

H:\Hexo\themes\yilia\_config.yml

# Miscellaneous(杂项):百度统计、Google统计
baidu_analytics: '百度统计id'
google_analytics: 'Google统计id'

成功效果

本地测试,F12打开控制台,没有报相关错误,而且在Sources-->Page有显示下面两个网站,说明配置成功了。过一段之间再去数据分析控制台查看,应该就会有数据。百度介绍说好像是20分钟就有数据了,我试了下,真有了,很快。google也立刻就有数据了。

file

说明

Google虽然好,但是因为墙的原因,所以有时候可能会出现问题。

参考:

posted @ 2020-08-05 21:50  秦时明月之君临天下  阅读(260)  评论(0编辑  收藏  举报
返回顶部
返回主页