From adf609576267dcbb237b0e898a877d55d8e26fcb Mon Sep 17 00:00:00 2001 From: Linus Flood Date: Tue, 3 Dec 2024 14:20:13 +0100 Subject: [PATCH] Check if client for session id hook --- hooks/useSessionId.ts | 7 ++++++- types/components/tracking.ts | 2 +- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/hooks/useSessionId.ts b/hooks/useSessionId.ts index 3a14a5027..ebf32b812 100644 --- a/hooks/useSessionId.ts +++ b/hooks/useSessionId.ts @@ -3,8 +3,13 @@ import { v4 as uuidv4 } from "uuid" const storageKey = "sessionId" -export function useSessionId(): string { +export function useSessionId(): string | null { const sessionId = useMemo(() => { + if (typeof window === "undefined") { + // Return null if running on the server + return null + } + let currentSessionId = sessionStorage.getItem(storageKey) if (!currentSessionId) { currentSessionId = uuidv4() diff --git a/types/components/tracking.ts b/types/components/tracking.ts index a4c9e30d5..1eb629a99 100644 --- a/types/components/tracking.ts +++ b/types/components/tracking.ts @@ -23,7 +23,7 @@ export type TrackingSDKPageData = { domain?: string siteSections: string pageLoadTime?: number // Page load time in seconds - sessionId?: string + sessionId?: string | null } export enum LoginTypeEnum {