Analytics
/
Router Utils
Utility library for single page app routing
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.
How to install
Install @analytics/router-utils from npm.
npm install @analytics/router-utilsAPI
Below is the api for @analytics/router-utils. You can import only what you need & the rest will be tree-shaken out of your bundle.
onRouteChange
Listen 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)
})Usage
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()
})