Google Tag Manager History Change is not fired after page change
Answered
Blue swimming crab posted this in #help-forum
Blue swimming crabOP
Google Tag Manager trigger „History change“ will not be triggered after page change. Is Next.js app router using the history api?
Answered by B33fb0n3
next changed something in 14.0.3 in the history push stuff. Are you using the 14.0.3?
43 Replies
Blue swimming crabOP
No. We inject the code directly via javascript, because of out consent tool. Everything, for example loading the Google Tag Manager is working, but when I set a trigger for History change. This doesn‘t work. Maybe Next.js is not executing one of this points:
When I use the third party link that I send to you, the call history call will be executed. The consent is by default -1. You can send an consent update event, whenever the user accepts or declines it
Blue swimming crabOP
Ok. Now I use the third-party package: https://github.com/manuelseisl/next-js-demo, but it doesn't work. No history change when navigating between pages:
it seems like it didn't even load... did you configured it correctly? 🤔
Blue swimming crabOP
Yes, the google tag manager loads correctly ðŸ˜
but there are not load and init hits 🤔https://ibb.co/bQ4LCv3
Blue swimming crabOP
Yeah because I deleted the events with the trash icon left top ☺ï¸
oh ok
Blue swimming crabOP
Do you have an example or an idea?
I retried it now exactly like you done and I see a history change event xD
So there might be something on your end, that dont work
Blue swimming crabOP
Can you send me a screenshot?
Can you also please share your code?
This would be very helpful ☺ï¸
I just switched the site and you can see, that page view are triggert two times: one for the inital page and the second for the page switch
Maybe you also mean the historyChange-v2 I guess... 🤔
Blue swimming crabOP
Yes, of course. The event “gtm.historyChange-v2†is not fired in my example.
ðŸ˜
Maybe you can provide me the code.
It’s literally the component from the nextjs docs. Nothing more. For me it haven’t worked directly, because my vpn and adblocker was enabled. Check if that blocks the tag…
Blue swimming crabOP
Are you using the link component from next.js for navigating?
And which version of next.js you are using?
@Blue swimming crab
I am using the Link component from next for navigating, yes.
And I am using the nextjs version
I am using the Link component from next for navigating, yes.
And I am using the nextjs version
"next": "14.0.3",Blue swimming crabOP
No
Are you using the next.js app router or pages router?
It works when I'm using:
<button onClick={() => history.pushState({ pageID: "asdf2" }, "asdf2", "/de/asdf2")}>
Test2
</button>
<button onClick={() => history.pushState({ pageID: "asdf2" }, "asdf2", "/de/asdf2")}>
Test2
</button>
@Blue swimming crab Are you using the next.js app router or pages router?
I am using the app router
Blue swimming crabOP
But the event is not firing when I'm using the next.js router push or link component:
<button onClick={() => router.push("/de/asdf1")}>Test1</button>
<button onClick={() => router.push("/de/asdf1")}>Test1</button>
next changed something in 14.0.3 in the history push stuff. Are you using the 14.0.3?
Answer
Blue swimming crabOP
Yes, I'm using Next.js 14.0.3
Would you try 14.0.2?
Blue swimming crabOP
I switched back to 14.0.0 and it works
Thank you. What changes did they made?
Maybe I should write an issue
btw have you tried canary
oh canary is also a possible fix, yeah
Blue swimming crabOP
With v14.0.4-canary.17 it doesn't work
Blue swimming crabOP
Maybe you can push the issue 😉
https://github.com/vercel/next.js/issues/58924
https://github.com/vercel/next.js/issues/58924