Docker搭建站点统计程序Matomo

开源的网站统计程序Matomo

有什么用

一款非常好用的网站统计程序,Matomo. Matomo的前身是著名的Piwik. 作为一款免费开源的统计程序,它不仅能够对每一条访问提供准确及时的访问记录,还能够自动生成极其专业的统计报表,包括根据地理位置生成的全球访问热点地图,实时访问图景,每一条访问的每一个点击操作详细分析,页面跳转率,来路分析,访客浏览器,访客硬件设备,关键词,等等等等……您还可以使用API,将统计功能整合在客户端、小程序等场景。虽然功能非常丰富,但是Mamoto非常轻量,几乎不需要占用什么资源。

实现方法

cd /www/server/panel/data/compose/matomo

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20

<!-- Matomo -->
<script>
var _paq = window._paq = window._paq || [];
/* tracker methods like "setCustomDimension" should be called before "trackPageView" */
_paq.push(["setDocumentTitle", document.domain + "/" + document.title]);
_paq.push(["setCookieDomain", "*.carlzeng.top"]);
_paq.push(['trackPageView']);
_paq.push(['enableLinkTracking']);
(function() {
var u="//192.168.6.116:8597/";
_paq.push(['setTrackerUrl', u+'matomo.php']);
_paq.push(['setSiteId', '1']);
var d=document, g=d.createElement('script'), s=d.getElementsByTagName('script')[0];
g.async=true; g.src=u+'matomo.js'; s.parentNode.insertBefore(g,s);
})();
</script>
<noscript><p><img src="//192.168.6.116:8597/matomo.php?idsite=1&amp;rec=1" style="border:0;" alt="" /></p></noscript>
<!-- End Matomo Code -->

docker-compose up

  1. Database Server: type in mariadb
  2. Login: type in root
  3. Password: leave blank
  4. Database Name: type in matomodb

Tracking code for Carl Notes

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<!-- Matomo -->
<script>
var _paq = window._paq = window._paq || [];
/* tracker methods like "setCustomDimension" should be called before "trackPageView" */
_paq.push(['trackPageView']);
_paq.push(['enableLinkTracking']);
(function() {
var u="//192.168.192.3/";
_paq.push(['setTrackerUrl', u+'matomo.php']);
_paq.push(['setSiteId', '1']);
var d=document, g=d.createElement('script'), s=d.getElementsByTagName('script')[0];
g.async=true; g.src=u+'matomo.js'; s.parentNode.insertBefore(g,s);
})();
</script>
<!-- End Matomo Code -->

开启防火墙的8597

NPM 新建
https://statcounter.carlzeng.top:4443

https://statcounter.carlzeng.top:4443/?module=Login
Error: The form security failed because of invalid origin. If you previously connected using HTTPS, please ensure you are connecting over a secure (SSL/TLS) connection and try again.

docker-compose down
docker-compose up

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20

<!-- Matomo -->
<script>
var _paq = window._paq = window._paq || [];
/* tracker methods like "setCustomDimension" should be called before "trackPageView" */
_paq.push(["setDocumentTitle", document.domain + "/" + document.title]);
_paq.push(["setCookieDomain", "*.carlzeng.top"]);
_paq.push(['trackPageView']);
_paq.push(['enableLinkTracking']);
(function() {
var u="//statcounter.carlzeng.top:4443/";
_paq.push(['setTrackerUrl', u+'matomo.php']);
_paq.push(['setSiteId', '1']);
var d=document, g=d.createElement('script'), s=d.getElementsByTagName('script')[0];
g.async=true; g.src=u+'matomo.js'; s.parentNode.insertBefore(g,s);
})();
</script>
<noscript><p><img src="//statcounter.carlzeng.top:4443/matomo.php?idsite=1&amp;rec=1" style="border:0;" alt="" /></p></noscript>
<!-- End Matomo Code -->

设置

UI语言

登录Matomo(之前叫Piwik)
点击右上角的齿轮小图标,进入设置界面
点击左菜单:Personal ==> settings.
设置中文(Language表单项,如下图)

访客IP不正确,访客IP全部变成了代理IP

[General]
assume_secure_protocol = 1

参见 http://www.matomo.net.cn/2019/03/setup-matomo-in-proxy-env/

; Uncomment line below if you use a standard proxy
proxy_client_headers[] = HTTP_X_FORWARDED_FOR
proxy_host_headers[] = HTTP_X_FORWARDED_HOST

[已解决]疑惑TODO

mamoto NPM failed to login:statcounter.carlzeng.top:4443

Error: The form security failed because of invalid origin. If you previously connected using HTTPS, please ensure you are connecting over a secure (SSL/TLS) connection and try again.

[done]本地局域网访问假如在config.ini.php中开启下面这条配置,会导致无法登录
;assume_secure_protocol = 1

2023年12月初:解决办法:用在线云浏览器Firefox,远程过去访问本地局域网的NPM面板即可

Mamoto面板反代以后无法正常登录,也请使用在线云浏览器Firefox

2024年1月中旬:彻底解决方案:配置和使用frpc的p2p模式,通过本地局域网运行frpc -c frpc.ini将Matomo的管理地址映射到frp服务上;控制端通过使用frpc -c frpc.ini P2P连接到受控端来远程访问Matomo Web页面

详细解决思路和技术细节请参见:Docker搭建FRP内网穿透服务

如何添加到JS文件

截取部分的代码如下, 直接加入到JS的位置(”use strict”;的后面)

比如app/public/server/server.client.js

1
2
3
4
5
6
7
8
9
10
11
12
13
var _paq = window._paq = window._paq || [];
/* tracker methods like "setCustomDimension" should be called before "trackPageView" */
_paq.push(["setDocumentTitle", document.domain + "/" + document.title]);
_paq.push(["setCookieDomain", "*.carlzeng.top"]);
_paq.push(['trackPageView']);
_paq.push(['enableLinkTracking']);
(function() {
var u="//statcounter.carlzeng.top:4443/";
_paq.push(['setTrackerUrl', u+'matomo.php']);
_paq.push(['setSiteId', '1']);
var d=document, g=d.createElement('script'), s=d.getElementsByTagName('script')[0];
g.async=true; g.src=u+'matomo.js'; s.parentNode.insertBefore(g,s);
})();

高阶定制/配置

20231216 实践证明没有变化。下一步动作

  1. 下载alecthw/mmdb_china_ip_list获得文件Country.mmdb

  2. 上传至/compose/matomo/matomo/misc,覆盖原先的GeoLite2-City.mmdb.

  3. 重启Docker,TODO 静观其变

    1. 效果立竿见影,只有6.1M的Country.mmdb明显无法显示到精确city级别。
  4. 找到一个https://github.com/P3TERX/GeoLite.mmdb/releases/tag/2023.12.16

    1. 下载得到三个文件:
      1. GeoLite2-ASN.mmdb
      2. GeoLite2-Country.mmdb
      3. GeoLite2-City.mmdb
    2. 如果可以像https://ip.skk.moe/query那样显示ISP/Orgnization那该多好

灵感来源

How to Install Matomo on Your Synology NAS

Matomo will always cost you nothing to use, but that doesn’t mean it costs us nothing to make.
Matomo needs your continued support to grow and thrive.

Run Matomo – Google Analytics Alternative – in Docker

可能是世界上最牛逼的网站统计程序——Matomo