hexo+yilia新增百度統計和Google統計
簡介
百度統計和Google統計主要用於分析網站資料:流量、訪客量等。
需要註冊賬號,然後將程式碼(指令碼)嵌入到網站中,然後在後臺就會生成對應資料,然後進行分析。
- 百度統計網址:https://tongji.baidu.com
- Google統計網址:https://analytics.google.com/analytics/web/
說明
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>
對應的配置檔案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)
我找了好久才找到程式碼,因此記錄下:
生成程式碼:控制檯-->(左下角)管理-->管理-->(中間)媒體資源-->(中間)跟蹤資訊-->跟蹤程式碼
這是針對此媒體資源的全域性網站程式碼 (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>
對應的配置檔案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也立刻就有資料了。
說明
Google雖然好,但是因為牆的原因,所以有時候可能會出現問題。
參考: