-
Notifications
You must be signed in to change notification settings - Fork 27.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Google Tag Manager event "History change" is no longer fired in Next.js 14.0.3 #58924
Comments
I thought this was the default behavior when I first using Nextjs with 14.0.3. I added a template.tsx to trigger GTM track event manually :/ |
I just checked and this was breaking Google Tag Manager / Analytics for me. Rolling back to 14.0.2 fixes for now. |
@cduff @vKongv @manuelseisl Are you having trouble keeping track of all the pages? I use import { GoogleTagManager } from "@next/third-parties/google"; |
@Bart3kL Do you have it set up to trigger on History Change? This is required due to hydration. |
@cduff Do you have it done somewhere and can you show it? |
I did it in 'use client';
import { useEffect } from 'react';
import { sendGTMEvent } from '@next/third-parties/google';
export default function Template({ children }: { children: React.ReactNode }) {
const isProd = process.env.NODE_ENV === 'production';
useEffect(() => {
isProd && process.env.NEXT_PUBLIC_GTM_ID && sendGTMEvent({ event: 'page_view' });
}, [isProd]);
return <div>{children}</div>;
} |
The GTM history change event should 'just work', but recently Next patched |
@Bart3kL I just have GTM set up in the root layout like: https://nextjs.org/docs/app/building-your-application/optimizing/third-party-libraries#google-tag-manager And the tag is configured like this: |
This issue should be resolved by 8ff1368, I think. It'll be in the next non-canary release which I am guessing will be some time in the next week or two. |
Fixed in 14.0.4 for me. |
Fixed on 14.0.4 but that version (and 14.0.5-canary) has a bug where all imports break. And because of the complexity of the setup there's no way to add a minimal repro, so can't open an issue:
Gonna be stuck on 14.0.3 for a while or revert to 14.0.2. |
@vKongv Hello, I tried yours and use different variable for the useEffect, but seems it won't re-render between pages using Component, is there other thing that I need to set in order to re-render the Template.tsx on /app/template.tsx? I only able to fire it using usePathName(), as the Template.tsx seems not rerendered or recalled eg like in https://medium.com/@codingbeautytutorials/nextjs-detect-route-change-bb8e6c316d1
I do look into the documentation that the state always not same each time we change to other page, but in this case seems it isn't, or I'm looking or interpret the docs and your answer wrongly? Thank you |
I'm on BTW, by following a similar
|
Haven't retested with the latest version, but had to move the GoogleTagManager into head at some point for History Change trigger to work.
|
…A4 (#1996) ## Summary Fixes #1889 ### Time to review: __2 mins__ ## Changes proposed > Added a client useEffect that will fire GA4 page change whenever a page is loaded ## Context for reviewers > vercel/next.js#58924 ## Additional information > Screenshots, GIF demos, code examples or output to help show the changes working as expected.
|
|
Link to the code that reproduces this issue
https://github.com/manuelseisl/next-js-demo
To Reproduce
Current vs. Expected behavior
The Google Tag Manager event "History change" is no longer fired in Next.js 14.0.3.
Verify canary release
Provide environment information
Operating System: Platform: darwin Arch: arm64 Version: Darwin Kernel Version 23.1.0: Mon Oct 9 21:28:45 PDT 2023; root:xnu-10002.41.9~6/RELEASE_ARM64_T6020 Binaries: Node: 20.10.0 npm: 10.2.3 Yarn: 1.22.19 pnpm: 8.8.0 Relevant Packages: next: 14.0.3 eslint-config-next: 14.0.3 react: 18.2.0 react-dom: 18.2.0 typescript: 5.3.2 Next.js Config: output: N/A
Which area(s) are affected? (Select all that apply)
App Router, Routing (next/router, next/navigation, next/link)
Additional context
I tested my reproduction against different versions and canary releases, and the first one that introduced the bug was “14.0.3”, when switching back to “14.0.2” it works.
The text was updated successfully, but these errors were encountered: