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雖然好,但是因爲牆的原因,所以有時候可能會出現問題。

參考:


文章首發於:hexo+yilia添加百度統計和Google統計

發佈了132 篇原創文章 · 獲贊 149 · 訪問量 19萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章