matomo是一款Google-analytics數(shù)據(jù)埋點采集上報的平替方案,可保護您的數(shù)據(jù)和客戶的隱私;正如它官網(wǎng)的slogan: Google Analytics alternative that protects your data and your customers' privacy; 該項目源碼開源免費,支持私有化部署,保證數(shù)據(jù)安全、可靠;支持多種方式集成,不管你的應(yīng)用是傳統(tǒng)的html多頁面應(yīng)用還是現(xiàn)代的SPA單頁面應(yīng)用,不管你的應(yīng)用是CSR渲染還是SSR渲染,均可支持;
SDK統(tǒng)計代碼
<!-- Matomo --> <!-- 聯(lián)系管理員新建項目后自動生成,放入到項目根目錄index.html header標(biāo)簽下,并配置相應(yīng)的追蹤域名地址,即刻生效 --> <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 = "https://test-matobo.jnt-express.com.cn/"; // matomo私有服務(wù)器地址 _paq.push(['setTrackerUrl', u + 'matomo.php']); // 指定 Matomo 服務(wù)器 URL _paq.push(['setSiteId', '9']); // 設(shè)置追蹤的站點唯一編碼(指定網(wǎng)站 ID) 該id將作為唯一標(biāo)識來區(qū)分matomo正在采集數(shù)據(jù)的應(yīng)用 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); // async 屬性指定該腳本SDK將會在加載完畢后執(zhí)行 })(); </script> <!-- End Matomo Code -->
-
常用Api:
-
setCustomUrl(string):
Override the page's reported URL,覆蓋頁面報告的 URL;給matomo上報業(yè)務(wù)系統(tǒng)的地址路徑、路由信息等
-
trackPageView([customTitle]):
Log a page view.?記錄頁面視圖信息并上報頁面標(biāo)題
-
setUserId(userId):
Sets a?User ID?to this user (such as an email address or a username).?設(shè)置該用戶的用戶 ID(例如電子郵件地址或用戶名)
-
resetUserId:
Clears (un-set) the User ID.?清除(取消設(shè)置)用戶 ID
-
trackEvent(category,action,[name],[value]):
Log an event with an event category (Videos, Music, Games...), an event action (Play, Pause, Duration, Add Playlist, Downloaded, Clicked...), and an optional event name and optional numeric value??記錄事件,其中包含事件類別(視頻、音樂、游戲...)、事件操作(播放、暫停、持續(xù)時間、添加播放列表、下載、單擊...)以及可選事件名稱和可選數(shù)值;此api將作為行為埋點,例記錄按鈕點擊次數(shù)上報等場景使用
接入方式
?文章來源:http://www.zghlxwxcb.cn/news/detail-710697.html
matomo支持多種方式接入,不管你的應(yīng)用是傳統(tǒng)的html多頁面應(yīng)用,還是現(xiàn)代的vue、react等單頁面應(yīng)用;
?
傳統(tǒng)的html多頁面應(yīng)用接入:
-
使用
-
引入SDK
<!-- 將統(tǒng)計代碼放入到項目根目錄下的index.html的header標(biāo)簽下,注意,該script是新建項目時自動生成的追蹤代碼--> <!-- 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="https://test-matobo.jnt-express.com.cn/"; _paq.push(['setTrackerUrl', u+'matomo.php']); _paq.push(['setSiteId', '10']); 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 -->
引入SDK即可自動采集部分?jǐn)?shù)據(jù),如有特殊需求需要記錄頁面信息及標(biāo)題,在對應(yīng)的地方調(diào)用window._paq.push方法去傳遞參數(shù)即可;api跟其他方式保持一致;
-
現(xiàn)代的單頁面應(yīng)用接入:
-
Hooks 封裝
/** src/plugins/matomo.ts */ export default function useMatomo() { /** 頁面地址信息上報 */ const setCustomUrl = (url: string) => { ;(window as any)._paq.push(['setCustomUrl', `${url}`]) } /** 頁面標(biāo)題信息上報 */ const trackPageView = (title: string | any) => { ;(window as any)._paq.push(['trackPageView', `${title}`]) } /** 用戶信息userId上報 */ const setUserId = (userId: string | number | boolean) => { ;(window as any)._paq.push(['setUserId', `${userId}`]) ;(window as any)._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 as any)._paq.push(['resetUserId']) ;(window as any)._paq.push(['trackAllContentImpressions', 'new_visit=1']) ;(window as any)._paq.push(['trackPageView']) ;(window as any)._paq.push(['trackAllContentImpressions']) } /** * 行為埋點 * $matomo.trackEvent('行為類別', '事件', 'name', 'value') * behaviorCategory 行為類別 * event 事件 * name 事件名稱 * value 事件值 */ const trackEvent = (behaviorCategory: string, event: string, name: string, value?: string | number) => { ;(window as any)._paq.push(['trackEvent', `${behaviorCategory}`, `${event}`, `${name}`]) } return { setCustomUrl, trackPageView, setUserId, resetUserId, trackEvent } }
<!-- App.vue --> <template> <ConfigProvider :locale="zhCN"> <router-view /> </ConfigProvider> </template> <script lang="ts" setup> import { onMounted, watch } from 'vue' import { ConfigProvider } from 'ant-design-vue' import { useRouter } from 'vue-router' import zhCN from 'ant-design-vue/lib/locale-provider/zh_CN' import useMatomo from '@/plugins/matomo' import { useAuthStore } from '@/store/auth' const router = useRouter() const authStore = useAuthStore() const matomo = useMatomo() /** 在App.vue中記錄每個路由切換的路徑及頁面標(biāo)題信息; sertUserId一般在登錄完成之后調(diào)用,這里在App.vue中調(diào)用是為了解決token存在,spa應(yīng)用直接進目標(biāo)頁面而跳過登錄頁無法觸發(fā)setUserId上報的問題 */ watch( () => router.currentRoute.value, (newValue: any) => { // 這里延遲是因為matomo sdk加載是異步非阻塞加載,所以為了能正確的獲取到window上matomo的實例,我們這里會有略微延遲 setTimeout(() => { /** * 記錄跳轉(zhuǎn)頁面及頁面標(biāo)題 userCode及nickName均為業(yè)務(wù)系統(tǒng)數(shù)據(jù),此處做拼接傳入 **/ const name = authStore.userInfo?.userCode !== undefined ? `${authStore.userInfo?.userCode} | ${authStore.userInfo?.nickName}` : '' matomo.setUserId(name) matomo.setCustomUrl(window.location.href) matomo.trackPageView(router.currentRoute.value.meta.title) }, 500) } )
<!--src/login/index.vue --> <script lang="ts" setup> import { reactive, ref, onMounted } from 'vue' import useMatomo from '@/plugins/matomo' const matomo = useMatomo() const jumpPage = (data: any) => { /** 登錄成功后在跳轉(zhuǎn)之前將業(yè)務(wù)數(shù)據(jù)userCode和nickName上報 */ const { userCode, nickName, menuTree } = data const name = `${userCode} | ${nickName}` || '' matomo.setUserId(name) } </script>
<!--具體的頁面具體的方法中去觸發(fā)trackEvent--> <script lang="ts" setup> import { reactive, ref, onMounted } from 'vue' import useMatomo from '@/plugins/matomo' const matomo = useMatomo() const exportBtn = () => { /** 例如需要記錄導(dǎo)出按鈕的點擊次數(shù),在導(dǎo)出按鈕中trackEvent即可 */ matomo.trackEvent('品牌監(jiān)控報表', '導(dǎo)出', 'export') </script>
vue-matomo npm包方式引入:
-
vue2
/** 官方提供了vue-matomo這個庫,僅適用于vue2,vue3方式接入會有問題 */ npm install --save vue-matomo
/** main.js */ import Vue from 'vue' import VueMatomo from 'vue-matomo' Vue.use(VueMatomo, { // 請求對應(yīng)服務(wù)域名,如果是私有化部署,可以填寫自己內(nèi)網(wǎng)的私有域名,如果是公網(wǎng)部署填寫https://matomo.example.com即可 host: 'https://matomo.example.com', /** 新建項目的script文件中會存在siteId這個變量,這個是跟項目綁定的唯一標(biāo)識,通過該變量索引查找到對應(yīng)項目 */ siteId: 5, // 最終的追蹤js文件名 默認(rèn) 'piwik' trackerFileName: 'matomo', siteId: 9, router: router, // 支持外部鏈接跟蹤 enableLinkTracking: true, // 是否需要在發(fā)送追蹤信息之前請求許可 默認(rèn)false requireConsent: false, // 是否追蹤初始頁面 默認(rèn)true trackInitialView: true, debug: false }); // 掛載到vue的實例中之后,我們可以使用this.$matomo or window._paq.push or window.Piwik.getTracker 等三種方式來訪問均可,此處使用this來訪問
?其實就是在業(yè)務(wù)代碼中做侵入式埋點了,在對應(yīng)的業(yè)務(wù)邏輯中使用??this.$matomo?&&?this.$matomo.trackPageView()?或?window._paq.push(['trackPageView'])?等api來進行注入埋點采集數(shù)據(jù)并上報,兩者功能效果相同,區(qū)別僅僅只是調(diào)用方式不同,掛載到實例中直接以funtion函數(shù)的方式調(diào)用傳參,window全局變量調(diào)用通過?push方法來調(diào)用,push方法接收一個數(shù)組,數(shù)組第一項為key,后續(xù)剩余參數(shù)為name;
- vue3
? vue3由于使用Composition API,且生命周期機制跟vue2不同,同時matomo又是異步加載資源,所以在main.ts文件中即使掛載了對應(yīng)的實例,通過getCurrentInstance()?.appContext.config.globalProperties.$matomo 來訪問matomo對應(yīng)實例,無法保證能準(zhǔn)確的獲取,即使你通過nextTick;因為加載matomo資源不是響應(yīng)式的,若在頁面渲染完成時,matomo資源文件未加載完成,此時獲取到的matomo實例仍然為undefined;至于matomo的實例只在頁面渲染的那一刻就決定你單次加載是否包含matomo實例
?
參考github issues:
? ?github vue-matomo issue
解決方案:
?
通過window._pag.push 來訪問全局變量,因為即使你獲取到的this.$matomo實例是undefined,window._paq.push也可以保證它是可用的;
-
react
? ? 社區(qū)沒有提供react-matomo之類的npm包/工具來給開發(fā)者使用,可以參考Hooks方案,將使用到的Api封裝成Hooks,通過window._paq.push的方式在需要的地方來調(diào)用;
由于是私有化部署,關(guān)于公司logo及業(yè)務(wù)數(shù)據(jù)等相關(guān)較敏感的數(shù)據(jù)均已打碼,只展示具體收集指標(biāo)和效果;
數(shù)據(jù)采集上報最終效果圖:
?
+
?
?
?
?
文章來源地址http://www.zghlxwxcb.cn/news/detail-710697.html
到了這里,關(guān)于基于matomo實現(xiàn)業(yè)務(wù)數(shù)據(jù)埋點采集上報的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!