Tracking pageviews in single-page apps (SPA)
Jun 11, 2024
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.
To fix this, you can implement pageview capture manually using custom events. This tutorial shows you how to do this 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, we start by creating the PostHogProvider component in the app folder. We set capture_pageview: false because we will manually capture pageviews.
To capture pageviews, we create another pageview.js component in the app folder.
Finally, we import both and put them together in the app/layout.js file.
Make sure to dynamically import the PostHogPageView component or the useSearchParams hook will deopt the entire app into client-side rendering.
Tracking pageviews in React Router
If you are using React Router AKA react-router-dom, start by adding the PostHogProvider component in the app folder. Make sure to set capture_pageview: false because we will manually capture pageviews.
To capture pageviews, we create another pageview.js component in the app folder.
Finally, we import both and put them together 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. This is where we initialize PostHog.
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. In it, add the code to initialize PostHog.
After that, create a +layout.svelte file in src/routes. In it, use the afterNavigate interceptor to capture pageviews.
Tracking pageviews in Angular
To start tracking pageviews in Angular, begin by initializing PostHog in src/main.ts.
After setting up your routes and router, you can capture pageviews by subscribing to navigationEnd events in app.component.ts.
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 45,000+ founders and builders.
We'll share your email with Substack
Questions? Ask Max AI.
It's easier than reading through 609 docs articles.