You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

30 line
715 B

  1. export default defineNuxtPlugin(() => {
  2. const router = useRouter()
  3. const { reportPv } = usePvcv()
  4. let lastFullPath = ''
  5. let lastReportedAt = 0
  6. function track(to, from) {
  7. if (!to || to.fullPath === lastFullPath) return
  8. const now = Date.now()
  9. if (now - lastReportedAt < 300 && to.fullPath === lastFullPath) return
  10. const referrer = from?.fullPath
  11. ? `${window.location.origin}${from.fullPath}`
  12. : document.referrer
  13. lastFullPath = to.fullPath
  14. lastReportedAt = now
  15. reportPv(to, { referrer })
  16. }
  17. router.afterEach((to, from) => {
  18. window.setTimeout(() => track(to, from), 0)
  19. })
  20. window.setTimeout(() => {
  21. track(router.currentRoute.value, null)
  22. }, 0)
  23. })