@analytics/router-utils — Route change utilities for single page apps. One of the small, focused utility packages that power the analytics library, usable standalone.
Full documentation, synced from the package README:
A tiny (577 bytes) routing utility for listening to route changes in single page applications.
Exposes onRouteChange function.
This library will work with analytics or as a standalone import in your code.
Install @analytics/router-utils from npm.
npm install @analytics/router-utilsBelow is the api for @analytics/router-utils. You can import only what you need & the rest will be tree-shaken out of your bundle.
onRouteChangeListen for push state route changes in single page apps. Pass a callback that receives the new route path whenever the route changes.
import onRouteChange from '@analytics/router-utils'
onRouteChange((newRoutePath) => {
console.log('new route path', newRoutePath)
})Wire onRouteChange up to analytics to trigger a page view on every route change.
import onRouteChange from '@analytics/router-utils'
import analytics from './analytics'
onRouteChange((newRoutePath) => {
console.log('new route path', newRoutePath)
// trigger page view
analytics.page()
})