SvelteKit error tracking installation
- 1
Install PostHog
RequiredYour goal in this step: Install the PostHog JavaScript web and Node SDKs.Install
posthog-jsusing your package manager:Then, if you haven't created a root layout already, create a new file called
+layout.jsin yoursrc/routesfolder In this file, check the environment is the browser, and initialize PostHog if so. You can get both your API key and instance address in your project settings.routes/+layout.jsInstall
posthog-nodeusing your package manager:Then, initialize the PostHog Node client where you'd like to use it on the server side. For example, in a load function:
routes/+page.server.jsNote: Make sure to always call
posthog.shutdown()after capturing events from the server-side. PostHog queues events into larger batches, and this call forces all batched events to be flushed immediately. - 2
Set up client-side exception capture
RequiredSvelteKit Hooks can be used to capture exceptions in the client and server-side.
Capture exceptions in the
handleErrorcallback in your client-side hooks file:src/hooks.client.js - 3
Set up server-side exception capture
RequiredTo capture exceptions on the server-side, you will also need to implement the
handleErrorcallback:src/hooks.server.ts - 5
PostHog is an all-in-one developer platform for building successful products. We provide product analytics, web analytics, session replay, error tracking,feature flags, experiments, surveys, LLM analytics, data warehouse, CDP, and anAI product assistant to help debug your code, ship features faster, and keep all your usage and customer data in one stack.

