fix(analytics): pass native arguments to dataLayer and track route changes
Deploy Canina / deploy (push) Successful in 2m20s
Deploy Canina / deploy (push) Successful in 2m20s
This commit is contained in:
1 parent
f59dd6ebec
commit
1651fd00a2
2 files changed
+163
-14
No files matched your search
@@ -1,11 +1,13 @@
|
||||
'use client';
|
||||
|
||||
import { useEffect } from 'react';
|
||||
import { useEffect, useRef } from 'react';
|
||||
import { usePathname } from 'next/navigation';
|
||||
|
||||
declare global {
|
||||
interface Window {
|
||||
dataLayer?: any[];
|
||||
clarity?: any;
|
||||
gtag?: (...args: any[]) => void;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -16,12 +18,46 @@ interface AnalyticsDeferredProps {
|
||||
}
|
||||
|
||||
export default function AnalyticsDeferred({ gtmId, gaId, clarityId }: AnalyticsDeferredProps) {
|
||||
const pathname = usePathname();
|
||||
const loadedRef = useRef(false);
|
||||
const initialPathRef = useRef<string | null>(null);
|
||||
|
||||
useEffect(() => {
|
||||
if (!initialPathRef.current && pathname) {
|
||||
initialPathRef.current = pathname;
|
||||
}
|
||||
}, [pathname]);
|
||||
|
||||
// Track client-side page views on Next.js route transitions
|
||||
useEffect(() => {
|
||||
if (!pathname || !loadedRef.current) return;
|
||||
if (pathname === initialPathRef.current) return;
|
||||
|
||||
// 1. Google Analytics 4 page_view event on client navigation
|
||||
if (gaId && typeof window !== 'undefined' && typeof window.gtag === 'function') {
|
||||
window.gtag('event', 'page_view', {
|
||||
page_path: pathname,
|
||||
page_location: window.location.href,
|
||||
page_title: typeof document !== 'undefined' ? document.title : '',
|
||||
});
|
||||
}
|
||||
|
||||
// 2. Google Tag Manager page_view event on client navigation
|
||||
if (gtmId && typeof window !== 'undefined' && Array.isArray(window.dataLayer)) {
|
||||
window.dataLayer.push({
|
||||
event: 'page_view',
|
||||
page_path: pathname,
|
||||
});
|
||||
}
|
||||
}, [pathname, gaId, gtmId]);
|
||||
|
||||
useEffect(() => {
|
||||
let loaded = false;
|
||||
|
||||
const loadAnalytics = () => {
|
||||
if (loaded) return;
|
||||
loaded = true;
|
||||
loadedRef.current = true;
|
||||
|
||||
// Clean up event listeners
|
||||
window.removeEventListener('scroll', loadAnalytics);
|
||||
@@ -42,10 +78,12 @@ export default function AnalyticsDeferred({ gtmId, gaId, clarityId }: AnalyticsD
|
||||
// 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);
|
||||
function gtag(..._args: any[]) {
|
||||
// Note: Google's gtag.js specifically expects the native `arguments` object.
|
||||
// Pushing an ES6 Array causes gtag.js to silently ignore tracking commands.
|
||||
window.dataLayer?.push(arguments);
|
||||
}
|
||||
(window as any).gtag = gtag;
|
||||
window.gtag = gtag;
|
||||
gtag('js', new Date());
|
||||
gtag('config', gaId, { page_path: window.location.pathname });
|
||||
|
||||
@@ -94,6 +132,7 @@ export default function AnalyticsDeferred({ gtmId, gaId, clarityId }: AnalyticsD
|
||||
|
||||
return () => {
|
||||
loaded = true;
|
||||
loadedRef.current = false;
|
||||
clearTimeout(timer);
|
||||
if (idleId !== undefined) window.cancelIdleCallback(idleId);
|
||||
window.removeEventListener('scroll', loadAnalytics);
|
||||
|
||||
Reference in new issue
Block a user