Skip to content
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

Subscribe to router changes for analytics #508

Open
jahilldev opened this issue Jan 31, 2025 · 1 comment
Open

Subscribe to router changes for analytics #508

jahilldev opened this issue Jan 31, 2025 · 1 comment

Comments

@jahilldev
Copy link

jahilldev commented Jan 31, 2025

I'm looking for a way to centrally define GTM dataLayer pushes based on router state changes, e.g whenever a navigation occurs. We use the dataLayer for tracking all sorts of things for observability.

What's the recommended way of achieving this with wouter?

Something like this (although the onChange property doesn't exist, I know):

<Router
  onChange={
    () => window.dataLayer.push({ event: 'navigation', /*[...]*/ })
  }
>
  {/*[...]*/}
</Router>

Perhaps this can be done via hooks?

Thanks in advance for the help

P.S: So glad we don't need to use the behemoth of react-router for simple React apps anymore! Good work!

@molefrog
Copy link
Owner

molefrog commented Feb 1, 2025

Wouter doesn't support events yet, as an alternative I can suggest listening to changes of the current location:

const [location] = useLocation()

useEffect(() => {
 // some additional logic if you want to avoid sending events on mount or duplicate events
 ... 
}, [location])

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants