|
- export default defineNuxtPlugin(() => {
- const router = useRouter()
- const { reportPv } = usePvcv()
-
- let lastFullPath = ''
- let lastReportedAt = 0
-
- function track(to, from) {
- if (!to || to.fullPath === lastFullPath) return
- const now = Date.now()
- if (now - lastReportedAt < 300 && to.fullPath === lastFullPath) return
-
- const referrer = from?.fullPath
- ? `${window.location.origin}${from.fullPath}`
- : document.referrer
-
- lastFullPath = to.fullPath
- lastReportedAt = now
- reportPv(to, { referrer })
- }
-
- router.afterEach((to, from) => {
- window.setTimeout(() => track(to, from), 0)
- })
-
- window.setTimeout(() => {
- track(router.currentRoute.value, null)
- }, 0)
- })
|