原單手表網(wǎng)站域名解析在線查詢
Matomo
文章目錄
- Matomo
- 前言
- 一、環(huán)境準備
- 1. 整體安裝流程
- 2.安裝PHP 7.3.30
- 3.nginx配置
- 4.安裝matomo
- 4.1 訪問安裝頁面 http://192.168.10.45:8088/index.php
- 4.2 連接數(shù)據(jù)庫
- 4.3 設置管理員賬號
- 4.4 生成js跟蹤代碼
- 4.5 安裝完成
- 4.6 警告修改
- 4.7 刷新頁面,就可以看到登陸頁面
- 二、Matomo使用
- 1. JS代碼跟蹤
- 1.1 獲取生成js
- 1.2 網(wǎng)站粘貼js代碼
- 1.3 訪問后查看報表
- 1.4 js方法封裝
- 1.5 測試代碼
- 1.6 報表
- 2. IMG跟蹤
- 使用img標簽追蹤
- img標簽追蹤鏈接
- 3. HTTP API 跟蹤
- ==注意:必填參數(shù)==
- api測試
- 4.HTTP API 獲取報告
- 4.1 創(chuàng)建身份驗證token
- 身份驗證
- 4.2 拼接查詢參數(shù)
- 4.3 發(fā)起請求

Matomo
: 一款開源的網(wǎng)站數(shù)據(jù)分析統(tǒng)計平臺,以前稱為 `Piwik,可以讓您輕松地從訪問者那里獲得您想要的信息。例如查看您的網(wǎng)站訪問者來自何處、正在查看哪些頁面、單擊了哪些鏈接以及其他各種有用的信息。可以用于跟蹤、分析您的網(wǎng)站的流量,同時充分保障數(shù)據(jù)安全性、隱私性,
提供了豐富的報告和實時數(shù)據(jù)分析功能,并提供了一套完整的隱私保護措施,確保用戶數(shù)據(jù)安全。
官網(wǎng)傳送門:matomo
前言
? 本文是為了在linux
系統(tǒng)上安裝matomo
,并使用它完成頁面跟蹤
與統(tǒng)計數(shù)據(jù)獲取
。
一、環(huán)境準備
1. 整體安裝流程
- PHP 7.2.5 或者以上的版本
- 安裝php擴展:pdo 和pdo_mysql
- MySQL 5.5或者以上的版本
- nginx配置
- 下載安裝matomo 4.15.1
2.安裝PHP 7.3.30
-
1.安裝epel
#yum list installed | grep php #查看已安裝的PHP, #查到后rpm -e 卸載 #yum repolist all | grep php yum install epel-release -y
-
2.安裝REMI源:
rpm -ivh https://mirrors.tuna.tsinghua.edu.cn/remi/enterprise/remi-release-7.rpm
-
3.查看可以安裝的PHP版本:
yum repolist all | grep php
-
4.安裝管理工具
yum install yum-utils
-
5.設置默認安裝的版本
yum-config-manager --enable remi-php73
-
6.安裝php
yum install php
-
7.查看php安裝版本
php -v
-
8.查看哪些可以安裝的PHP擴展
yum search php74-php
-
9.安裝php拓展
yum install php74-php-bcmath php74-php-gd php74-php-pdo php74-php-mbstring php74-php-cli php74-php-fpm php74-php-mysqlnd php74-php-simplexml php74-php-develyum install php php-curl php-gd php-cli mysql-server php-mysql php-xml php-mbstring php-fpm
-
10.運行php-fpm
修改
/etc/php-fpm.d/www.conf
文件啟動fpm程序
systemctl start php-fpm.service
#啟動 php-fpm systemctl start php-fpm.service #停止 php-fpm systemctl stop php-fpm.service #重啟 php-fpm systemctl reload php-fpm.service
-
11.查看php擴展
php -m
3.nginx配置
matomo的安裝包是通過php的網(wǎng)頁安裝,但php直接訪問會被當成普通文件直接下載,不會被編譯,
所以需要在nginx中配置
php-fpm
解釋器
-
nginx.conf文件配置
user nginx nginx; #user nobody; worker_processes 1; #error_log logs/error.log; #error_log logs/error.log notice; #error_log logs/error.log info;#pid logs/nginx.pid;events {worker_connections 1024; }http {include mime.types;default_type application/octet-stream;#log_format main '$remote_addr - $remote_user [$time_local] "$request" '# '$status $body_bytes_sent "$http_referer" '# '"$http_user_agent" "$http_x_forwarded_for"';#access_log logs/access.log main;sendfile on;#tcp_nopush on;#keepalive_timeout 0;keepalive_timeout 65;#gzip on;server {listen 8088;server_name 192.168.10.45;charset utf-8;root /matomo/matomo;#location / {# try_files $uri $uri/ /index.php?$query_string;#}location / {index index.php index.html index.htm;}location ~ \.php$ {fastcgi_pass 127.0.0.1:9000;fastcgi_index index.php;fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;include fastcgi_params;}}}
? 重啟nginx nginx -s reload
4.安裝matomo
# 解壓matomo壓縮包
unzip matomo-latest.zip -d ./#給解壓的安裝包賦權(quán)限
chown -R nginx:nginx /matomo
chown -R 777 /matomo/*
注:如果出現(xiàn) nginx No input file specified
錯誤,檢查nginx錯誤日志如有 FastCGI sent in stderr: "Primary script unknown" while reading response header from upstream
,嘗試關閉selinux解決:``setenforce 0`。
安全增強型Linux(SELinux)是一個Linux內(nèi)核的功能,它提供支持訪問控制的安全政策保護機制。記錄操作如何關閉SELinux,并且避免系統(tǒng)無法啟動的問題。
執(zhí)行命令:
setenforce 0臨時關閉SELinux
-
4.1 訪問安裝頁面 http://192.168.10.45:8088/index.php
-
4.2 連接數(shù)據(jù)庫
-
4.3 設置管理員賬號
-
4.4 生成js跟蹤代碼
<!-- 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.10.45/";_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 -->
-
4.5 安裝完成
-
4.6 警告修改
修改
config.ini.php
找到
trusted_hosts[] = "192.168.10.45"
,增加端口,改為trusted_hosts[] = "192.168.10.45:8088"
另外還需要再增加一行
enable_trusted_host_check=0
,用于跳過受信任檢測,否則反代之后登錄可能會失敗 -
4.7 刷新頁面,就可以看到登陸頁面
二、Matomo使用
1. JS代碼跟蹤
-
1.1 獲取生成js
-
1.2 網(wǎng)站粘貼js代碼
-
1.3 訪問后查看報表
-
1.4 js方法封裝
官方api參數(shù):https://developer.matomo.org/guides/tracking-javascript-guide
// https://developer.matomo.org/guides/tracking-javascript-guide/** 頁面地址信息上報 */ const setCustomUrl = (url) => {window._paq.push(['setCustomUrl', `${url}`]) }/** 頁面標題信息上報 */ const trackPageView = (title) => {window._paq.push(['trackPageView', `${title}`]) }/** 用戶信息userId上報 */ const setUserId = (userId) => {window._paq.push(['setUserId', `${userId}`])window._paq.push(['trackPageView']) }/** 重置userId,這里多次調(diào)用trackAllContentImpressions是為了在退出登錄的時候重置調(diào)userId,并在下一次登錄時重新生成一條最新的記錄 */ const resetUserId = () => {// UserID passed to Matomo (see https://developer.matomo.org/guides/tracking-javascript-guide#user-id)window._paq.push(['resetUserId'])window._paq.push(['trackAllContentImpressions', 'new_visit=1'])window._paq.push(['trackPageView'])window._paq.push(['trackAllContentImpressions']) }/*** 點擊外網(wǎng)地址* @param url*/ const setDomains = (url) => {_paq(['setDomains', [url]]);_paq.push(['trackPageView']); }/*** 行為埋點* $matomo.trackEvent('行為類別', '事件', 'name', 'value')* behaviorCategory 行為類別* event 事件* name 事件名稱* value 事件值*/ const trackEvent = (behaviorCategory, event, name, value) => {window._paq.push(['trackEvent', `${behaviorCategory}`, `${event}`, `${name}`, `${value}`]) }const matomoFun = {setCustomUrl, trackPageView, setUserId, resetUserId, trackEvent }export default matomoFun;
-
1.5 測試代碼
matomoF(item) {console.log("matomo--", item);matomoFun.setUserId("xxx123123xxx");matomoFun.trackEvent('風險申報', '點擊matomo按鈕', "測試事件",item.id); },
-
1.6 報表
2. IMG跟蹤
使用img標簽追蹤
當訪客禁用 JavaScript 或者無法使用 JavaScript 時,img標簽追蹤功能(Image Tracking)允許您使用img標簽中的鏈接來進行訪客統(tǒng)計。 在下方產(chǎn)生鏈接,并將生成的 HTML 粘貼到網(wǎng)頁中。如果要把它用作 JavaScript 的追蹤功能失敗時的備用方式,您可以把它放在
<noscript></noscript>
標簽中。查看 追蹤 API 說明文檔閱讀可用于img標簽追蹤鏈接的全部參數(shù)列表。
img標簽追蹤鏈接
<!-- Matomo Image Tracker--> <img referrerpolicy="no-referrer-when-downgrade" src="http://192.168.10.45:8088/matomo.php?idsite=1&rec=1" style="border:0" alt="" /> <!-- End Matomo -->
3. HTTP API 跟蹤
要跟蹤頁面瀏覽量、事件、訪問量,必須向跟蹤 HTTP API 端點發(fā)送 HTTP 請求(GET 或 POST),例如,http://192.168.10.45:8088/matomo.php 設置了正確的查詢參數(shù)。
官方參數(shù)地址:https://developer.matomo.org/api-reference/tracking-api
在線API URL生成器:https://http-builder.openmost.io/
注意:必填參數(shù)
idsite
(required) — The ID of the website we’re tracking a visit/action for.
idsite
(必填) — 我們跟蹤其訪問/操作的網(wǎng)站的 ID。rec
(required) — Required for tracking, must be set to one, eg,&rec=1
.
rec
(required) — 跟蹤時需要,必須設置為 1,例如&rec=1
.
4.HTTP API 獲取報告
Matomo中的所有數(shù)據(jù),都可通過簡單的API接口獲取。這個插件是Web服務的切入點,那你可以通過調(diào)用獲取你想要的網(wǎng)站分析的數(shù)據(jù),比如XML,JSON,PHP,CSV等。
Matomo API 介紹 和 Matomo API 參考資料。
-
4.1 創(chuàng)建身份驗證token
身份驗證
如果要在腳本,crontab等中請求數(shù)據(jù),則需要將URL參數(shù)
token_auth
添加到需要身份驗證的API調(diào)用URL中。注意:超級管理員和編輯權(quán)限生成的令牌是不能在查詢中使用的,所以要換其他只讀賬號生成
令牌: 3c0a2e399a490607f029ae35a727b357
-
4.2 拼接查詢參數(shù)
參數(shù)規(guī)范:https://developer.matomo.org/api-reference/reporting-api
標準API參數(shù)
-
idSite:您網(wǎng)站的整數(shù) ID,例如。idSite=1,還可以指定以逗號分隔的 idSites 列表,例如。idSite=1,4,5,6,如果要獲取所有網(wǎng)站的數(shù)據(jù),請設置 idSite=all(僅某些 API 方法支持此功能)
-
period: 您請求統(tǒng)計信息的時間段,可以是以下任意一項:日、周、月、年或范圍。所有報告都根據(jù)網(wǎng)站的時區(qū)返回日期
- day:day 返回給定日期的數(shù)據(jù)。
- week:week 返回包含指定“date”的那一周的數(shù)據(jù)
- month:返回包含指定“date”的月份的數(shù)據(jù)
- year:year 返回包含指定“date”的年份的數(shù)據(jù)
- range:range 返回指定“date”范圍的數(shù)據(jù)。
例如,要請求 1 月 1 日至 2 月 15 日范圍的報告,可以編寫 &period=range&date=2011-01-01,2011-02-15
-
date:日期
- 標準格式 = YYYY-MM-DD
- 關鍵字= today, yesterday, lastWeek, lastMonth or lastYear. These are relative the website timezone. For example, for a website with UTC+12 timezone
- 日期范圍 =
lastX
previousX
YYYY-MM-DD,YYYY-MM-DD
-
segment:定義要將報表篩選到的自定義區(qū)段。
-
format:定義輸出的格式,XML、JSON、CSV、TSV、HTM、RSS
-
filter_limit:定義要返回的行數(shù)。
- 默認情況下,僅返回前 100 行。
- 設置為 -1 可返回所有行
-
module:API api類型
-
method:獲取數(shù)據(jù)類型,如VisitorInterest.getNumberOfVisitsPerVisitDuration
-
-
4.3 發(fā)起請求
例子為:每次訪問持續(xù)時間的訪問次數(shù)
GET http://192.168.10.45:8088/index.php?module=API&method=VisitorInterest.getNumberOfVisitsPerVisitDuration&idSite=1&period=day&date=2023-11-02&token_auth=ca369ba839f4c9c36df034fa1366de3c&format=json
返回參數(shù)注釋
通過以上步驟,我們就可以使用linux實例來搭建自己的Matomo平臺,并進行網(wǎng)站數(shù)據(jù)分析了。
Matomo不僅可以幫助我們獲取網(wǎng)站的基本數(shù)據(jù),還可以進行高級分析,如用戶行為、轉(zhuǎn)化率、目標跟蹤、電子商務等。此外,Matomo還支持多種擴展和集成,如地理位置、社交媒體、搜索引擎優(yōu)化等。通過Matomo,我們可以更深入地了解我們的網(wǎng)站和用戶,從而提升網(wǎng)站的價值和效果。
尾聲
感謝小伙伴們的支持吖,祝大家萬圣節(jié)快樂哦!