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
This commit is contained in:
Linus Flood
2025-03-27 10:42:24 +00:00
parent 5de2a993a7
commit 0f9c9c2af8
5 changed files with 20 additions and 9 deletions

View File

@@ -1,7 +1,7 @@
import { nanoid } from "nanoid" import { nanoid } from "nanoid"
import { useMemo } from "react" import { useMemo } from "react"
const storageKey = "web_sessionId" export const SESSION_ID_KEY_NAME = "web_sessionId"
export function useSessionId(): string | null { export function useSessionId(): string | null {
const sessionId = useMemo(() => { const sessionId = useMemo(() => {
@@ -10,10 +10,10 @@ export function useSessionId(): string | null {
return null return null
} }
let currentSessionId = sessionStorage.getItem(storageKey) let currentSessionId = sessionStorage.getItem(SESSION_ID_KEY_NAME)
if (!currentSessionId) { if (!currentSessionId) {
currentSessionId = nanoid() currentSessionId = nanoid()
sessionStorage.setItem(storageKey, currentSessionId) sessionStorage.setItem(SESSION_ID_KEY_NAME, currentSessionId)
} }
return currentSessionId return currentSessionId
}, []) }, [])

View File

@@ -6,6 +6,7 @@ import { getPublicNextURL } from "@/server/utils"
import { auth } from "@/auth" import { auth } from "@/auth"
import { findLang } from "@/utils/languages" import { findLang } from "@/utils/languages"
import { isValidSession } from "@/utils/session"
import { getDefaultRequestHeaders } from "./utils" import { getDefaultRequestHeaders } from "./utils"
@@ -16,7 +17,7 @@ export const middleware: NextMiddleware = async (request) => {
const isRedemption = const isRedemption =
request.nextUrl.searchParams.get(SEARCHTYPE) === REDEMPTION request.nextUrl.searchParams.get(SEARCHTYPE) === REDEMPTION
const session = await auth() // Check for user session const session = await auth() // Check for user session
if (isRedemption && (!session || session?.error)) { if (isRedemption && !isValidSession(session)) {
const lang = findLang(request.nextUrl.pathname)! const lang = findLang(request.nextUrl.pathname)!
const nextUrlPublic = getPublicNextURL(request) const nextUrlPublic = getPublicNextURL(request)
const headers = new Headers() const headers = new Headers()

View File

@@ -5,6 +5,7 @@ import * as api from "@/lib/api"
import { getVerifiedUser } from "@/server/routers/user/query" import { getVerifiedUser } from "@/server/routers/user/query"
import { router, safeProtectedServiceProcedure } from "@/server/trpc" import { router, safeProtectedServiceProcedure } from "@/server/trpc"
import { isValidSession } from "@/utils/session"
import { getFriendsMembership } from "@/utils/user" import { getFriendsMembership } from "@/utils/user"
import { import {
@@ -77,7 +78,7 @@ const removePackageFailCounter = meter.createCounter(
async function getMembershipNumber( async function getMembershipNumber(
session: Session | null session: Session | null
): Promise<string | undefined> { ): Promise<string | undefined> {
if (!session) return undefined if (!isValidSession(session)) return undefined
const verifiedUser = await getVerifiedUser({ session }) const verifiedUser = await getVerifiedUser({ session })
if (!verifiedUser || "error" in verifiedUser) { if (!verifiedUser || "error" in verifiedUser) {

View File

@@ -1,6 +1,17 @@
import { SESSION_ID_KEY_NAME } from "@/hooks/useSessionId"
export function trackEvent(data: any) { export function trackEvent(data: any) {
if (typeof window !== "undefined" && window.adobeDataLayer) { 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) window.adobeDataLayer.push(data)
} }
} }

View File

@@ -14,9 +14,7 @@ export function trackPageViewStart() {
} }
export function trackPageView(data: any) { export function trackPageView(data: any) {
if (typeof window !== "undefined" && window.adobeDataLayer) { trackEvent(data)
window.adobeDataLayer.push(data)
}
} }
export function createSDKPageObject( export function createSDKPageObject(