partytown: GTM does not report history change events
Description
I’m trying to use the GTM via Partytown in Next.js. I have successfully loaded the scripts and got GTM to send the initial collect request, but it does not report page_view events after changing routes.
When testing the same setup without Partytown the collect requests get sent properly.
Behaviour when loading GTM normally:
https://user-images.githubusercontent.com/9134970/165815764-c2279ce8-10d9-435b-a73f-da89e2c5126e.mov
Behaviour when loading GTM via Partytown:
https://user-images.githubusercontent.com/9134970/165816399-0d170266-c910-4cce-ad40-4da17f1a0221.mov
Expected behaviour
GTM loaded via Partytown detects route changes and sends appropriate collect requests.
How to reproduce
Link to reproduction repo: https://github.com/MrPumpking/nextjs-partytown-gtag-bug
- The
mainbranch contains pure Next.js setup with GTM. Paste your GTM measurement ID in the.envfile. - The
partytownbranch contains GTM loaded via Partytown.
When running each version, you can check the network tab in devtools for outgoing collect requests.
GTM setup GTM is connected with GA4 using the standard Tag.

GA4 web stream has enabled enhanced measurement with the “Page changes based on browser history events” enabled.


About this issue
- Original URL
- State: closed
- Created 2 years ago
- Reactions: 1
- Comments: 21 (3 by maintainers)
I spent some more time on this. Now I see that the problem is caused by
history.pushState&history.replaceStatenot being called inside worker.I added some more logic to Partytown’s internal LocationUpdate message handler. Now it detects how location has changed (either pushState, replaceState, pop state, etc). When needed it calls pushState or replaceState inside worker.
I already have a POC next.js app with proper history tracking 🎉
I will try to wrap it up and create MR sometime this week. There’s one ugly workaround in my code I would like to fix first.
I found the problem, I had to put the
gtagon the window object (eg:window.gtag = function() {dataLayer.push(arguments)}Then in _document where I’m setting the partown-config:Now partytown properly forwards
gtag()calls to the worker.I have below code but it doesn’t seem to work in Angular App, I do see first request to
https://analytics.google.com/g/collectwhen I open the app in browser, but on subsequent navigation no addional request is shown inNetworktab.Below is the code I am using for GA 4:
FWIW I narrowed this to
popstateandhashchangeevents not being forwared by partytown (those 2 events are used by GTM’s “History Change”.This code shows the problem:
Load and resize events are properly forwared to Partytown, but not others.
I’ve added a
partytown-0.6.0branch to the reproduction repository and GTM reporting works fine now - acollectrequest is being sent after each route change. @slawekkolodziej thanks for your work!Yeah, I just tested version 0.6.0 and it seems to work just fine with the services I need.
Thanks @Vorseek , tried updating the gtm code but it didn’t work:
I am following this example to integrate Party town https://codesandbox.io/s/laughing-bush-u3j0xb?file=/index.html
Try change gtm.js to
I thought about sending the
page_viewmanually as specified in the documentation:But I get the following error:
I’ve also tried calling the
dataLayer.pushdirectly:This time I don’t get any errors but there are still no outgoing requests to GTM: