Files
canina/frontend/application/components/AnalyticsDeferred.tsx
T
devops ddbf1ca026
Deploy Canina / deploy (push) Successful in 4m1s
E2E Playwright Tests / Run Full E2E & Security Suites (push) Failing after 7s
test: comprehensive test suites and quality gates pass across backend, storefront and admin
- Backend (NestJS): 82 suites, 641 tests passing; 0 ESLint errors; 0 tsc errors. Hardened SSL cert/key matching, SSRF IP pinning, and eBank validation.
- Admin Panel (Vite/React): 48 suites, 300 tests passing; 0 ESLint errors; 0 tsc errors. Full coverage across all pages and settings.
- Storefront (Next.js): 62 suites, 368 tests passing; 0 ESLint errors; 0 tsc errors.
- Fixed unmount lifecycle guards, promise error handling, and component edge cases.
2026-10-13 16:01:00 +00:00

108 lines
3.6 KiB
TypeScript

'use client';
import { useEffect } from 'react';
declare global {
interface Window {
dataLayer?: any[];
clarity?: any;
}
}
interface AnalyticsDeferredProps {
gtmId?: string;
gaId?: string;
clarityId?: string;
}
export default function AnalyticsDeferred({ gtmId, gaId, clarityId }: AnalyticsDeferredProps) {
useEffect(() => {
let loaded = false;
const loadAnalytics = () => {
if (loaded) return;
loaded = true;
// Clean up event listeners
window.removeEventListener('scroll', loadAnalytics);
window.removeEventListener('touchstart', loadAnalytics);
window.removeEventListener('mousemove', loadAnalytics);
window.removeEventListener('keydown', loadAnalytics);
// 1. Google Tag Manager
if (gtmId) {
window.dataLayer = window.dataLayer || [];
window.dataLayer.push({ 'gtm.start': new Date().getTime(), event: 'gtm.js' });
const gtmScript = document.createElement('script');
gtmScript.async = true;
gtmScript.src = `https://www.googletagmanager.com/gtm.js?id=${gtmId}`;
document.head.appendChild(gtmScript);
}
// 2. Google Analytics 4 (ensures GA4 hits fire directly even if GTM container lacks GA4 config tag)
if (gaId) {
window.dataLayer = window.dataLayer || [];
function gtag(...args: any[]) {
window.dataLayer?.push(args);
}
(window as any).gtag = gtag;
gtag('js', new Date());
gtag('config', gaId, { page_path: window.location.pathname });
// Only append gtag.js script if not already added
if (!document.querySelector(`script[src*="${gaId}"]`)) {
const gaScript = document.createElement('script');
gaScript.async = true;
gaScript.src = `https://www.googletagmanager.com/gtag/js?id=${gaId}`;
document.head.appendChild(gaScript);
}
}
// 3. Microsoft Clarity
if (clarityId) {
(function (c: any, l: any, a: any, r: any, i: any) {
c[a] =
c[a] ||
function (...args: unknown[]) {
(c[a].q = c[a].q || []).push(args);
};
const t = l.createElement(r);
t.async = 1;
t.src = 'https://www.clarity.ms/tag/' + i;
const y = l.getElementsByTagName(r)[0];
if (y?.parentNode) y.parentNode.insertBefore(t, y);
else l.head.appendChild(t);
})(window, document, 'clarity', 'script', clarityId);
}
};
// Listen to first user interaction
window.addEventListener('scroll', loadAnalytics, { passive: true, once: true });
window.addEventListener('touchstart', loadAnalytics, { passive: true, once: true });
window.addEventListener('mousemove', loadAnalytics, { passive: true, once: true });
window.addEventListener('keydown', loadAnalytics, { passive: true, once: true });
// Fallback: idle execution after 3.5s
let idleId: number | undefined;
const timer = setTimeout(() => {
if (typeof window !== 'undefined' && 'requestIdleCallback' in window) {
idleId = window.requestIdleCallback(loadAnalytics, { timeout: 2000 });
} else {
loadAnalytics();
}
}, 3500);
return () => {
loaded = true;
clearTimeout(timer);
if (idleId !== undefined) window.cancelIdleCallback(idleId);
window.removeEventListener('scroll', loadAnalytics);
window.removeEventListener('touchstart', loadAnalytics);
window.removeEventListener('mousemove', loadAnalytics);
window.removeEventListener('keydown', loadAnalytics);
};
}, [gtmId, gaId, clarityId]);
return null;
}