肿瘤康复网,内容丰富有趣,生活中的好帮手!
肿瘤康复网 > hexo+yilia添加百度统计和Google统计

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

时间:2021-04-12 21:36:19

相关推荐

文章目录

简介说明配置百度Google全局网站代码 (gtag.js)配置id成功效果说明

简介

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

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

百度统计网址:Google统计网址:/analytics/web/

说明

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

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

配置

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

# Miscellaneousbaidu_analytics: ''google_analytics: ''

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

百度

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

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

对应的配置文件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 = "/hm.js?<%= theme.baidu_analytics %>";var s = document.getElementsByTagName("script")[0]; s.parentNode.insertBefore(hm, s);})();</script><% } %>

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

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

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

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

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

对应的配置文件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.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="/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也立刻就有数据了。

说明

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

参考:

设置 Google Analytics(分析)代码Hexo-blog-yilia主题迭代 – 增加RSS功能+百度统计/google统计

文章首发于:hexo+yilia添加百度统计和Google统计

如果觉得《hexo+yilia添加百度统计和Google统计》对你有帮助,请点赞、收藏,并留下你的观点哦!

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。