mirror of
https://github.com/pacnpal/thrilltrack-explorer.git
synced 2025-12-20 07:51:13 -05:00
50 lines
1.4 KiB
TypeScript
50 lines
1.4 KiB
TypeScript
import { supabase } from '@/lib/supabaseClient';
|
|
import { handleNonCriticalError } from './errorHandler';
|
|
|
|
// Generate anonymous session hash (no PII)
|
|
function getSessionHash(): string {
|
|
// Check if we have a session hash in sessionStorage
|
|
let sessionHash = sessionStorage.getItem('session_hash');
|
|
|
|
if (!sessionHash) {
|
|
// Create a random hash for this session (no user data)
|
|
sessionHash = `session_${Math.random().toString(36).substring(2, 15)}`;
|
|
sessionStorage.setItem('session_hash', sessionHash);
|
|
}
|
|
|
|
return sessionHash;
|
|
}
|
|
|
|
// Debounce tracking to avoid rapid-fire views
|
|
const trackedViews = new Set<string>();
|
|
|
|
export async function trackPageView(
|
|
entityType: 'park' | 'ride' | 'company',
|
|
entityId: string
|
|
) {
|
|
// Create unique key for this view
|
|
const viewKey = `${entityType}:${entityId}`;
|
|
|
|
// Don't track the same entity twice in the same session
|
|
if (trackedViews.has(viewKey)) {
|
|
return;
|
|
}
|
|
|
|
trackedViews.add(viewKey);
|
|
|
|
try {
|
|
// Track view asynchronously (fire and forget)
|
|
await supabase.from('entity_page_views').insert({
|
|
entity_type: entityType,
|
|
entity_id: entityId,
|
|
session_hash: getSessionHash()
|
|
});
|
|
} catch (error: unknown) {
|
|
// Fail silently - don't break the page if tracking fails
|
|
handleNonCriticalError(error, {
|
|
action: 'Track page view',
|
|
metadata: { entityType, entityId }
|
|
});
|
|
}
|
|
}
|