目前市面上有很多网站统计分析平台,如:百度统计、CNZZ(友盟)、51LA、Google Analytics,其中 CNZZ(友盟)已经停止免费统计服务,谷歌统计在国内的体验差,51LA 相比百度统计功能及体验方便又不如
总结下来就剩百度统计,但是数据放在第三方可能有些人又不放心,所以还是自己搭建一个数据统计分析平台吧😛
所以最后选择了 matomo,matomo 的生态很好,插件市场有很多插件可供使用,最大化满足您的数据统计分析需求!
Matomo 的前身是 Piwik,是一套基于 PHP+MySQL 技术构建的开源网站访问统计系统。
准备材料:
服务器 域名
请先安装宝塔面板,当然也可以使用其他服务器运维面板或者 LNMP 一键包等等,没有限制。
出于速度考虑,优先推荐 Web 环境:Linux+Nginx+PHP+MariaDB 10.4
下载Matomo安装包
Matomo下载地址:https://matomo.org/download/
单击 DOWNLOAD MATOMO 按钮即可下载,之后上传到网站根目录,把 matomo 目录内的全部文件复制粘贴到网站根目录,删除 matomo 目录及 matomo 安装压缩包。
访问域名开始安装。
单击 NEXT 按钮进入下一步操作。
接下来是检查系统环境,需要开启 php 的 shell_exec 的函数。
进入 php 管理页面>禁用函数>找到 shell_exec,点击删除即可,之后重启 php。
根据 matomo 文档的建议,请为您的网站配置文件添加以下规则
location ^~ /.git/ {
deny all;
}
location ^~ /core/ {
deny all;
}
location ^~ /config/ {
deny all;
}
location ^~ /lang/ {
deny all;
}
location ^~ /tmp/ {
deny all;
}
## only allow accessing the following php files
location ~ ^/(index|matomo|piwik|js/index|plugins/HeatmapSessionRecording/configs)\.php$ {
try_files $fastcgi_script_name =404; # protects against CVE-2019-11043. If this line is already included in your snippets/fastcgi-php.conf you can comment it here.
fastcgi_param HTTP_PROXY ""; # prohibit httpoxy: https://httpoxy.org/
#fastcgi_pass 127.0.0.1:9000; # uncomment if you are using PHP via TCP sockets (e.g. Docker container)
}
## deny access to all other .php files
location ~* ^.+\.php$ {
deny all;
return 403;
}
## serve all other files normally
location / {
try_files $uri $uri/ =404;
}
## disable all access to the following directories
location ~ ^/(config|tmp|core|lang) {
deny all;
return 403; # replace with 404 to not show these directories exist
}
location ~ /\.ht {
deny all;
return 403;
}
location ~ js/container_.*_preview\.js$ {
expires off;
add_header Cache-Control 'private, no-cache, no-store';
}
location ~ \.(gif|ico|jpg|png|svg|js|css|htm|html|mp3|mp4|wav|ogg|avi|ttf|eot|woff|woff2)$ {
allow all;
## Cache images,CSS,JS and webfonts for an hour
## Increasing the duration may improve the load-time, but may cause old files to show after an Matomo upgrade
expires 1h;
add_header Pragma public;
add_header Cache-Control "public";
}
location ~ ^/(libs|vendor|plugins|misc|node_modules) {
deny all;
return 403;
}
## properly display textfiles in root directory
location ~/(.*\.md|LEGALNOTICE|LICENSE) {
default_type text/plain;
}
添加到宝塔网站配置的伪静态即可,点击 NEXT 进入下一步
填写数据库信息,其他选项默认即可,next。
填写管理员账号密码,next。
填写网站名称、网站 URL、时区、是否启用电子商务网站
网址处需要填写完整带协议头的 URL,结尾不需要带/
时区选择上海,一般国内使用的都是上海时区(Asia/Shanghai),至于为何?历史遗留问题。。。
复制下方统计代码粘贴到您的网站底部即可实现网站统计服务,下一步。
出于数据分析考虑,建议取消勾选 匿名访问者 IP 地址的最后一个字节,以遵守您当地的隐私法/指南 选项,当然某些国家法律法规要求需要开启,具体根据法律法规选择。下一步。
安装成功!之后会重定向到后台,登录账号。
如何设置中文?
进入网站后台>Personal>setting>language,选择简体中文即可。
如何新添加网站?
进入后台,点击所有网站>添加一个新网站
选择网站。内联网网站是指内网网站,即网站解析地址为局域网 IP。
剩下的根据自己的需求填写即可,可以添加多个域名,子域。保存即可。
点击下方显示跟踪代码获取统计代码。
复制统计网站粘贴到网站底部即可。
同时 matomo 也支持浏览器不支持 JavaScript 的时候也可以实现统计服务,但需要使用 noscript 标签,以及 matomo 提供的 img 标签统计代码,例如:浏览器被禁用 JavaScript 功能,或用户使用的浏览器不支持 matomo 的脚本。
<noscript>标签是指在浏览器无法加载 JavaScript 显示的内容。
统计代码实例如下:
<!DOCTYPE html>
<html lang="zh">
<head>
<meta charset="UTF-8">
<title>matomo 网站统计 test</title></head>
<body> <!-- 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 = "//we.3g88.com/";
_paq.push(['setTrackerUrl', u + 'matomo.php']);
_paq.push(['setSiteId', '2']);
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 --> <!-- Matomo Image Tracker-->
<noscript><img referrerpolicy="no-referrer-when-downgrade" src="https://we.3g88.com/matomo.php?idsite=2&rec=1"
style="border:0" alt=""/></noscript> <!-- End Matomo --> </body>
</html>
经过测试可以看到 matomo 的统计方法,先加载 matomo.js 文件,通过 matomo.php 文件发送 post 请求,其中包含当前网页及用户的信息参数,还包含一些其他的字段数据。
测试发现浏览器禁用 JavaScript 也可以正常实现发送统计请求。
可以在 matomo 后台查看网站的实时访问日志,包含访问时的时间、访问时长,访问页面次数、首次访问时间、来自网站、设备信息(访问设备、浏览器、系统版本、地区 IP)、设置的转化目标等等信息。
matomo 自带的 IP 库 IP 归属不准确,后期会更新 matomo 三方 IP 库的使用方法。
后期也会更新一些 matomo 的使用教程,如定时处理访问数据(可大幅度减轻服务器压力),欢迎关注!
还没有人赞赏,快来当第一个赞赏的人吧!

- 1¥
- 5¥
- 10¥
- 20¥
- 50¥
本文为原创文章,版权归阿峰博客所有,欢迎分享本文,转载请保留出处!
内容反馈