Tracking pageviews in single-page apps (SPA)
Jun 11, 2024
Contents
A single-page application (or SPA) dynamically loads content for new pages using JavaScript instead of loading new pages from the server. Ideally, this enables users to navigate around the app without waiting for new pages to load, providing a seamless user experience.
PostHog's JavaScript Web SDK automatically captures pageview events on page load. The problem with SPAs is that page loads don't happen beyond the initial one. This means user navigation in your SPA isn't tracked.
Luckily, you can opt-in to tracking this behavior by setting defaults: '2025-05-24'
when initializing PostHog to use the most recent defaults. This default uses capture_pageview: 'history_change'
, which captures SPA navigation using the browser history API
This tutorial shows you how to follow the recommended approach for the most popular SPA frameworks like Next.js, Vue, Svelte, and Angular.
Prerequisite: Each of these requires you to have an app created and PostHog installed. To install the PostHog JavaScript Web SDK, run the following command for the package manager of your choice:
Terminal
Tracking pageviews in Next.js (app router)
To add PostHog to your Next.js app use it to capture pageviews, we create a PostHogProvider
component in the app
folder, initialize PostHog with our project API key and host (from your project settings). PostHog will automatically capture pageviews if initialized with updated defaults.
We import this and use it in the app/layout.js
file.
Tracking pageviews in React Router v7
If you are using React Router, start by setting posthog-js
and posthog-js/react
as external packages in your vite.config.ts
file.
Next, create a providers.tsx
file in the app
folder. PostHog will automatically track pageviews if initialized with updated defaults.
Finally, import the PHProvider
component in the app/root.tsx
file.
Tracking pageviews in React Router (v6 or below)
If you are using React Router v6 or below AKA react-router-dom
, start by adding the PostHogProvider
component in the app
folder.
We then import this and use it in the app/layout.js
file.
Tracking pageviews in Vue
After creating a Vue app and setting up the vue-router
, create a new folder in the src/components
named plugins
. In this folder, create a file named posthog.js
. If PostHog is initialized with updated defaults, it automatically tracks pageviews.
After this, you can add the plugin to the main.js
file and use it along with the router to capture pageviews afterEach
route change.
Tracking pageviews in Svelte
If you haven't already, start by creating a +layout.js
file for your Svelte app in your src/routes
folder. PostHog automatically tracks your pageviews once initialized with updated defaults.
Tracking pageviews in Angular
To start tracking pageviews in Angular, begin by initializing PostHog in src/main.ts
. PostHog automatically tracks your pageviews when initialized with updated defaults.
Further reading
- What to do after installing PostHog in 5 steps
- What engineers get wrong about analytics
- Complete guide to event tracking
Subscribe to our newsletter
Product for Engineers
Read by 60,000+ founders and builders
We'll share your email with Substack