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

@@ -54,14 +54,19 @@ it('tracks client-side route changes when pathname changes',()=>{
mockPathname = '/blog/test-article';
rerender(<AnalyticsDeferred gaId="G-test" gtmId="GTM-test"/>);
const gtmPageView = window.dataLayer?.find(entry => entry.event === 'page_view' && entry.page_path === '/blog/test-article');
expect(gtmPageView).toBeDefined();
const gtmPageView = window.dataLayer?.find(entry => entry.event === 'page_view');
expect(gtmPageView).toBeUndefined(); // direct GA is the single first-party owner
mockPathname = '/';
rerender(<AnalyticsDeferred gaId="G-test" gtmId="GTM-test"/>);
const views = window.dataLayer?.filter(entry => entry[0] === 'event' && entry[1] === 'page_view');
expect(views?.map(entry => entry[2].page_path)).toEqual(['/','/blog/[slug]','/']);
expect(views?.every(entry => !('page_title' in entry[2]) && !entry[2].page_location.includes('?'))).toBe(true);
const gaPageView = window.dataLayer?.find(entry => {
return Object.prototype.toString.call(entry) === '[object Arguments]' && entry[0] === 'event' && entry[1] === 'page_view';
});
expect(gaPageView).toBeDefined();
expect(gaPageView[2].page_path).toBe('/blog/test-article');
expect(gaPageView[2].page_path).toBe('/');
});
it.each(['scroll','touchstart','mousemove','keydown'])('loads on %s',event=>{
@@ -115,6 +120,32 @@ it('cancels idle work on unmount',()=>{
expect(cancelIdleCallback).toHaveBeenCalledWith(9);
});
it('honors opt-out before loading and after initialization',()=>{
window.globalPrivacyControl=true;
const v=render(<AnalyticsDeferred gaId="G-test"/>);
fireEvent.scroll(window);
expect(document.scripts).toHaveLength(0);
delete window.globalPrivacyControl;
fireEvent.keyDown(window);
const count=window.dataLayer?.length;
window.globalPrivacyControl=true;
mockPathname='/shop/joint';v.rerender(<AnalyticsDeferred gaId="G-test"/>);
expect(window.dataLayer).toHaveLength(count!);
delete window.globalPrivacyControl;
});
it('tracks catalog views and actual enabled contact anchors without commerce events',()=>{
mockPathname='/shop/joint';
const v=render(<AnalyticsDeferred gaId="G-test"/>);
fireEvent.scroll(window);
const a=document.createElement('a');a.href='tel:private';document.body.append(a);
fireEvent.click(a);a.remove();
expect(window.dataLayer?.filter(e=>e[0]==='event').map(e=>e[1])).toEqual(['page_view','catalog_product_view','contact_intent']);
expect(window.dataLayer?.some(e=>e[0]==='config' && e[2].send_page_view===false)).toBe(true);
mockPathname='/shop/joint';v.rerender(<AnalyticsDeferred gaId="G-test"/>);
expect(window.dataLayer?.filter(e=>e[1]==='catalog_product_view')).toHaveLength(1);
});
it('unmount removes event and timeout callbacks',()=>{
const v=render(<AnalyticsDeferred gtmId="GTM-test"/>);
v.unmount();