Skip to content

Commit

Permalink
home: Add stats and track page views
Browse files Browse the repository at this point in the history
Integrated with Plausible.

This is an optional feature and can be turned off by
setting "statsBase" to any falsy value in "app/
app.config.ts".
  • Loading branch information
ThrRip committed Dec 16, 2024
1 parent 9b260f1 commit f556ce3
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 0 deletions.
1 change: 1 addition & 0 deletions .idea/dictionaries/ThrRip.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions app/app.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ export default defineAppConfig({
backendProjectId: '649758e1eb1fa584a04d',
backendQueryResultsLimit: 1000,

statsBase: 'https://stats.mzg.fan',

biliApiRoomPlayInfoEndpoint: 'https://api.live.bilibili.com/xlive/web-room/v2/index/getRoomPlayInfo',

monitoringDataCollectorUserAgentMatch: /^Zabbix$/
Expand Down
40 changes: 40 additions & 0 deletions packages/home/app/middleware/trackPageview.global.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* Copyright (c) 2024 ThrRip, XMing (洺知-故犯)
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/

export default defineNuxtRouteMiddleware(() => {
if (
!useAppConfig().statsBase ||
import.meta.client ||
import.meta.dev ||
!useRequestHeader('user-agent') ||
// @ts-expect-error
useAppConfig().monitoringDataCollectorUserAgentMatch.test(useRequestHeader('user-agent'))
) return

const fetchHeaders: Record<string, string> = {}
const requestHeaders = useRequestHeaders();
['user-agent', 'x-forwarded-for', 'x-forwarded-proto', 'x-forwarded-host'].forEach(header => {
if (requestHeaders[header]) { fetchHeaders[header] = requestHeaders[header] }
})

useFetch('/api/event', {
baseURL: useAppConfig().statsBase,
method: 'POST',
headers: fetchHeaders,
body: {
// domain
d: new URL(useRequestURL()).hostname,
// name
n: 'pageview',
// url
u: useRequestURL(),
// referrer
r: useRequestHeader('referrer')
}
})
})

0 comments on commit f556ce3

Please sign in to comment.