Skip to content

Commit 2237e9d

Browse files
authored
fix(analytics): keep initial pageviews enabled (#31)
1 parent cc63bca commit 2237e9d

1 file changed

Lines changed: 8 additions & 1 deletion

File tree

‎src/app/components/GoogleAnalytics.tsx‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,19 @@ declare global {
1616
export default function GoogleAnalytics() {
1717
const pathname = usePathname();
1818
const lastTrackedPath = useRef<string | null>(null);
19+
const hasTrackedInitialPage = useRef(false);
1920
const pendingPageViews = useRef<Array<{ pathname: string; pageTitle: string }>>([]);
2021
const [gtagReady, setGtagReady] = useState(false);
2122

2223
useEffect(() => {
2324
if (!pathname) return;
2425

26+
if (!hasTrackedInitialPage.current) {
27+
hasTrackedInitialPage.current = true;
28+
lastTrackedPath.current = pathname;
29+
return;
30+
}
31+
2532
if (lastTrackedPath.current !== pathname) {
2633
lastTrackedPath.current = pathname;
2734
pendingPageViews.current.push({ pathname, pageTitle: document.title });
@@ -50,7 +57,7 @@ export default function GoogleAnalytics() {
5057
{`window.dataLayer = window.dataLayer || [];
5158
window.gtag = window.gtag || function(...args) { window.dataLayer.push(args); };
5259
window.gtag('js', new Date());
53-
window.gtag('config', '${measurementId}', { send_page_view: false });`}
60+
window.gtag('config', '${measurementId}');`}
5461
</Script>
5562
</>
5663
);

0 commit comments

Comments
 (0)