feat: Implement custom view tracking

This commit is contained in:
gpt-engineer-app[bot]
2025-10-10 15:53:56 +00:00
parent c39aba6994
commit 4d2d39fb5a
10 changed files with 288 additions and 58 deletions

47
src/lib/viewTracking.ts Normal file
View File

@@ -0,0 +1,47 @@
import { supabase } from '@/integrations/supabase/client';
// 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()
});
console.log(`✅ Tracked view: ${entityType} ${entityId}`);
} catch (error) {
// Fail silently - don't break the page if tracking fails
console.error('Failed to track page view:', error);
}
}