mirror of
https://github.com/pacnpal/thrilltrack-explorer.git
synced 2025-12-21 16:51:13 -05:00
Refactor code structure and remove redundant changes
This commit is contained in:
49
src-old/lib/viewTracking.ts
Normal file
49
src-old/lib/viewTracking.ts
Normal file
@@ -0,0 +1,49 @@
|
||||
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 }
|
||||
});
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user