Files
web/apps/scandic-web/stores/tracking.ts
Joakim Jäderberg 87d97db324 Merged in fix/pageview-tracking (pull request #1687)
Fix/pageview tracking

* Restructure pageview tracking

* Update trpc version and turn off batching

* Remove unused state and remove logs

* cleanup

* remove unused code and console.logs


Approved-by: Linus Flood
2025-04-01 06:43:54 +00:00

58 lines
1.5 KiB
TypeScript

"use client"
import { create } from "zustand"
interface TrackingStoreState {
initialStartTime: number
setInitialPageLoadTime: (time: number) => void
getPageLoadTime: () => number
currentPath: string | null
previousPath: string | null
currentLang: string | null
previousLang: string | null
updateRouteInfo: (path: string, lang: string) => void
hasPathOrLangChanged: () => boolean
}
const useTrackingStore = create<TrackingStoreState>((set, get) => ({
initialStartTime: Date.now(),
setInitialPageLoadTime: (time) => set({ initialStartTime: time }),
getPageLoadTime: () => {
const { initialStartTime } = get()
return (Date.now() - initialStartTime) / 1000
},
currentPath: null,
previousPath: null,
currentLang: null,
previousLang: null,
updateRouteInfo: (path, lang) =>
set((state) => {
if (!path || !lang) return state
if (!state.currentPath || !state.currentLang) {
return {
currentPath: path,
currentLang: lang,
previousPath: null,
previousLang: null,
}
}
return {
previousPath: state.currentPath,
previousLang: state.currentLang,
currentPath: path,
currentLang: lang,
}
}),
hasPathOrLangChanged: () => {
const { currentPath, previousPath, currentLang, previousLang } = get()
if (!previousPath || !previousLang) return false
return currentPath !== previousPath || currentLang !== previousLang
},
}))
export default useTrackingStore