Docker搭建站点统计程序Matomo

开源的网站统计程序Matomo

有什么用

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

实现方法

分享可用的docker-compose.yml文件内容如下:

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
21
22
23
24
25
26
27
version: '3.9'
services:
mariadb:
container_name: Matomo-DB
image: mariadb:jammy
command: --max_allowed_packet=1073741824
restart: always
#restart: on-failure:3
volumes:
- ./matomodb:/var/lib/mysql
environment:
- MYSQL_ALLOW_EMPTY_PASSWORD=yes
- MYSQL_DATABASE=matomodb
matomo:
container_name: Matomo
environment:
#- PUID=1026
#- PGID=100
- TZ=Asia/Shanghai
ports:
- 8597:80
depends_on:
- mariadb
volumes:
- ./matomo:/var/www/html:rw
image: matomo
restart: always
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.com"]);
_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.com:4443

https://statcounter.carlzeng.com: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.com"]);
_paq.push(['trackPageView']);
_paq.push(['enableLinkTracking']);
(function() {
var u="//statcounter.carlzeng.com: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.com: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.com: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.com"]);
_paq.push(['trackPageView']);
_paq.push(['enableLinkTracking']);
(function() {
var u="//statcounter.carlzeng.com: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那该多好

数据库查询

工具: [navicat16/17 mac版无限重置试用期脚本](navicat16/17 mac版无限重置试用期脚本)
navicat, 或者 dbeaver

故障: 由于当前docker中的mariadb没有映射出端口+没有设置root密码; 导致无法连接.

解决办法: 修改docker-compose文件, 添加ports: -3308:3306

查询特定页面所有的具体访问

应用场景: 想要查看某个特定页面的所有访问

1
2
3
4
5
select * FROM matomo_log_visit 
where visit_entry_idaction_url in (
select idaction FROM matomo_log_action WHERE name like '%202304131736%'
)
ORDER BY visit_last_action_time DESC

替换’%202304131736%’为具体的URL地址(节选关键字即可)

字段说明: http://www.matomo.net.cn/matomo-piwik-database-schema-pdf/

查询用户的所有历史访问

应用场景: 锁定特定的visitor, 如何查询到他相关的关注的页面? 比如他的浏览器客户端信息等

Matomo数据库中location_ip与idvisitor如何转化成明文

1
SELECT inet_ntoa(conv(hex(location_ip), 16, 10)) as ip, conv(hex(idvisitor), 16, 10) as visitorId FROM matomo_log_visit;

引入Matomo Tag Manager, 测试中…

1
2
3
4
5
6
7
8
9
10
<!-- Matomo Tag Manager -->
<script>
var _mtm = window._mtm = window._mtm || [];
_mtm.push({'mtm.startTime': (new Date().getTime()), 'event': 'mtm.Start'});
(function() {
var d=document, g=d.createElement('script'), s=d.getElementsByTagName('script')[0];
g.async=true; g.src='//statcounter.carlzeng.com:4443/js/container_v1cUcIiP.js'; s.parentNode.insertBefore(g,s);
})();
</script>
<!-- End Matomo Tag Manager -->

灵感来源

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

个性化需求沟通 扫客服加V加群: