diff --git a/.idea/dictionaries/ThrRip.xml b/.idea/dictionaries/ThrRip.xml index b26272c..e0ba72d 100644 --- a/.idea/dictionaries/ThrRip.xml +++ b/.idea/dictionaries/ThrRip.xml @@ -9,6 +9,7 @@ getline mktemp nuxtjs + pageview singleline typecheck webmanifest diff --git a/app/app.config.ts b/app/app.config.ts index 098c945..cfb76c8 100644 --- a/app/app.config.ts +++ b/app/app.config.ts @@ -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$/ diff --git a/packages/home/app/middleware/trackPageview.global.ts b/packages/home/app/middleware/trackPageview.global.ts new file mode 100644 index 0000000..9802f78 --- /dev/null +++ b/packages/home/app/middleware/trackPageview.global.ts @@ -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 = {} + 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') + } + }) +})