Files
canina/frontend/application/lib/catalogAnalytics.ts
T

28 lines
1.8 KiB
TypeScript

// Only bounded route classes leave the browser; never arbitrary paths or contact values.
export function catalogRoute(path: string): string {
if (/^\/(shop|blog|wiki)\/[^/]+\/?$/.test(path)) return `/${path.split('/')[1]}/[slug]`;
return ['/', '/shop', '/blog', '/wiki', '/catalog', '/about', '/contact', '/videos'].includes(path) ? path : '/other';
}
export function analyticsAllowed(nav: { doNotTrack?: string | null; globalPrivacyControl?: boolean }, browser: { globalPrivacyControl?: boolean }): boolean {
return nav.doNotTrack !== '1' && !nav.globalPrivacyControl && !browser.globalPrivacyControl;
}
export type CatalogEmitter = (event: string, params: Record<string, string>) => void;
export function trackCatalogClick(event: MouseEvent, anchor: HTMLAnchorElement, source: string, emit: CatalogEmitter): void {
if (event.defaultPrevented || event.button !== 0 || event.ctrlKey || event.metaKey || event.shiftKey || event.altKey || anchor.hasAttribute('download')) return;
const url = new URL(anchor.href, window.location.origin);
const source_route = catalogRoute(source);
if (url.protocol === 'tel:' || url.protocol === 'mailto:') {
emit('contact_intent', { source_route, contact_type: url.protocol === 'tel:' ? 'phone' : 'email' });
return;
}
if (url.origin !== window.location.origin || !['http:', 'https:'].includes(url.protocol)) return;
const target_route = catalogRoute(url.pathname);
if (target_route === '/other' || url.pathname === source) return;
if (!['/blog/[slug]', '/wiki/[slug]', '/shop/[slug]'].includes(source_route) && target_route !== '/contact') return;
emit(target_route === '/contact' ? 'contact_intent' : 'internal_navigation', target_route === '/contact'
? { source_route, contact_type: 'contact_page' } : { source_route, target_route });
}