From 0f9c9c2af8f8311a81d903eed8811509f3d74441 Mon Sep 17 00:00:00 2001 From: Linus Flood Date: Thu, 27 Mar 2025 10:42:24 +0000 Subject: [PATCH] Merged in feat/tracking-sessionId (pull request #1653) Feat(tracking): add sessionId to all events. Fixed some invalid session bugs * Feat(tracking): add sessionId to all events. Fixed some invalid session bugs Approved-by: Anton Gunnarsson --- apps/scandic-web/hooks/useSessionId.ts | 6 +++--- apps/scandic-web/middlewares/bookingFlow.ts | 3 ++- apps/scandic-web/server/routers/booking/mutation.ts | 3 ++- apps/scandic-web/utils/tracking/base.ts | 13 ++++++++++++- apps/scandic-web/utils/tracking/pageview.ts | 4 +--- 5 files changed, 20 insertions(+), 9 deletions(-) diff --git a/apps/scandic-web/hooks/useSessionId.ts b/apps/scandic-web/hooks/useSessionId.ts index ca38d152f..264d9478f 100644 --- a/apps/scandic-web/hooks/useSessionId.ts +++ b/apps/scandic-web/hooks/useSessionId.ts @@ -1,7 +1,7 @@ import { nanoid } from "nanoid" import { useMemo } from "react" -const storageKey = "web_sessionId" +export const SESSION_ID_KEY_NAME = "web_sessionId" export function useSessionId(): string | null { const sessionId = useMemo(() => { @@ -10,10 +10,10 @@ export function useSessionId(): string | null { return null } - let currentSessionId = sessionStorage.getItem(storageKey) + let currentSessionId = sessionStorage.getItem(SESSION_ID_KEY_NAME) if (!currentSessionId) { currentSessionId = nanoid() - sessionStorage.setItem(storageKey, currentSessionId) + sessionStorage.setItem(SESSION_ID_KEY_NAME, currentSessionId) } return currentSessionId }, []) diff --git a/apps/scandic-web/middlewares/bookingFlow.ts b/apps/scandic-web/middlewares/bookingFlow.ts index f080b8782..5f0e10788 100644 --- a/apps/scandic-web/middlewares/bookingFlow.ts +++ b/apps/scandic-web/middlewares/bookingFlow.ts @@ -6,6 +6,7 @@ import { getPublicNextURL } from "@/server/utils" import { auth } from "@/auth" import { findLang } from "@/utils/languages" +import { isValidSession } from "@/utils/session" import { getDefaultRequestHeaders } from "./utils" @@ -16,7 +17,7 @@ export const middleware: NextMiddleware = async (request) => { const isRedemption = request.nextUrl.searchParams.get(SEARCHTYPE) === REDEMPTION const session = await auth() // Check for user session - if (isRedemption && (!session || session?.error)) { + if (isRedemption && !isValidSession(session)) { const lang = findLang(request.nextUrl.pathname)! const nextUrlPublic = getPublicNextURL(request) const headers = new Headers() diff --git a/apps/scandic-web/server/routers/booking/mutation.ts b/apps/scandic-web/server/routers/booking/mutation.ts index d265bbeaf..5a092f3f7 100644 --- a/apps/scandic-web/server/routers/booking/mutation.ts +++ b/apps/scandic-web/server/routers/booking/mutation.ts @@ -5,6 +5,7 @@ import * as api from "@/lib/api" import { getVerifiedUser } from "@/server/routers/user/query" import { router, safeProtectedServiceProcedure } from "@/server/trpc" +import { isValidSession } from "@/utils/session" import { getFriendsMembership } from "@/utils/user" import { @@ -77,7 +78,7 @@ const removePackageFailCounter = meter.createCounter( async function getMembershipNumber( session: Session | null ): Promise { - if (!session) return undefined + if (!isValidSession(session)) return undefined const verifiedUser = await getVerifiedUser({ session }) if (!verifiedUser || "error" in verifiedUser) { diff --git a/apps/scandic-web/utils/tracking/base.ts b/apps/scandic-web/utils/tracking/base.ts index 608c35703..dc024d050 100644 --- a/apps/scandic-web/utils/tracking/base.ts +++ b/apps/scandic-web/utils/tracking/base.ts @@ -1,6 +1,17 @@ +import { SESSION_ID_KEY_NAME } from "@/hooks/useSessionId" + export function trackEvent(data: any) { if (typeof window !== "undefined" && window.adobeDataLayer) { - data = { ...data, siteVersion: "new-web" } + let sessionId = "" + + try { + sessionId = sessionStorage.getItem(SESSION_ID_KEY_NAME) ?? "" + } catch (e) { + console.error("Error getting sessionId from sessionStorage", e) + } + + data = { ...data, siteVersion: "new-web", sessionId } + window.adobeDataLayer.push(data) } } diff --git a/apps/scandic-web/utils/tracking/pageview.ts b/apps/scandic-web/utils/tracking/pageview.ts index e0b9cdf55..2b623386f 100644 --- a/apps/scandic-web/utils/tracking/pageview.ts +++ b/apps/scandic-web/utils/tracking/pageview.ts @@ -14,9 +14,7 @@ export function trackPageViewStart() { } export function trackPageView(data: any) { - if (typeof window !== "undefined" && window.adobeDataLayer) { - window.adobeDataLayer.push(data) - } + trackEvent(data) } export function createSDKPageObject(