feat(analytics): instrument privacy-minimized catalog navigation

This commit is contained in:
Canina Automation committed 2026-10-28 07:59:35 +00:00
1 parent c62d9836e9
commit 13dc74807a
8 files changed
+145 -32

No files matched your search

@@ -0,0 +1,21 @@
import {describe,it,expect,vi} from 'vitest';
import {catalogRoute, analyticsAllowed, trackCatalogClick} from '../catalogAnalytics';
describe('catalog analytics privacy and intent',()=>{
it('uses bounded public route templates without identifiers or private URLs',()=>{
expect(catalogRoute('/shop/joint')).toBe('/shop/[slug]');
expect(catalogRoute('/blog/article')).toBe('/blog/[slug]');
expect(catalogRoute('/dashboard/orders/secret')).toBe('/other');
});
it('respects browser privacy signals',()=>{expect(analyticsAllowed({doNotTrack:'1'},{})).toBe(false);expect(analyticsAllowed({}, {globalPrivacyControl:true})).toBe(false);});
it('records article to product intent without href, title, query or contact data',()=>{
const emit=vi.fn(); const a=document.createElement('a');a.href='/shop/joint?email=private#phone';a.textContent='private';
trackCatalogClick(new MouseEvent('click'),a,'/blog/article',emit);
expect(emit).toHaveBeenCalledExactlyOnceWith('internal_navigation',{source_route:'/blog/[slug]',target_route:'/shop/[slug]'});
});
it('records actual contact anchors but never contact value or external navigation',()=>{
const emit=vi.fn();const a=document.createElement('a');a.href='tel:private';trackCatalogClick(new MouseEvent('click'),a,'/shop/joint',emit);
expect(emit).toHaveBeenCalledExactlyOnceWith('contact_intent',{source_route:'/shop/[slug]',contact_type:'phone'});
emit.mockClear();a.href='https://external.test';trackCatalogClick(new MouseEvent('click'),a,'/blog/a',emit);expect(emit).not.toHaveBeenCalled();
});
it('ignores cancelled, modified, and non-primary clicks',()=>{const emit=vi.fn();const a=document.createElement('a');a.href='/shop/a';for(const event of [new MouseEvent('click',{ctrlKey:true}),new MouseEvent('click',{button:1})]) trackCatalogClick(event,a,'/blog/a',emit);const event=new MouseEvent('click',{cancelable:true});event.preventDefault();trackCatalogClick(event,a,'/blog/a',emit);expect(emit).not.toHaveBeenCalled();});
});
@@ -0,0 +1,27 @@
// 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 });
}