Posted on 

Wordpress 手動增加 Google Analytics 追蹤程式碼

打開 wp-includes/default-filters.php

約第 198 行開始隨意找一行加入此段程式碼

1
add_action('wp_head', 'google_analytics');

然後打開 wp-includes/general-template.php 隨意找一行新增以下程式碼

1
2
3
4
5
6
7
8
9
10
11
12
function google_analytics() {  
echo "<script>
(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', '追蹤碼', '網址');
ga('send', 'pageview');

</script>";
}

請把追蹤碼跟網址自行置換即可,或者上 Google Analytics 登入自己的帳號找尋追蹤程式碼來貼也可以囉