Skip to content

Single-page apps

In apps with client-side routing (React Router, Vue Router, Next.js, Nuxt and others), the page doesn’t reload on navigation, so a page view has to be recorded on each route change.

Pass autoPageviews: true to init():

cosembra.init('pk_live_REPLACE_ME', {
endpoint: 'https://events.cosembra.com/v1/batch',
autoPageviews: true,
})

The SDK then sends a page_view:

  • once when it starts, and
  • whenever the path or query string changes through history.pushState, history.replaceState, or the back and forward buttons.

A navigation that leaves the path and query string unchanged, such as a change to the #hash alone, doesn’t count as a new page.

If you’d rather control it, for example to wait until the new page’s title is set, leave autoPageviews off and call page() yourself after each navigation:

router.afterEach(() => {
cosembra.page()
})

page(properties) is shorthand for track('page_view', properties). The current URL, path, title and referrer are added automatically.