SvelteKit error tracking installation
- 1
Install PostHog
RequiredYour goal in this step: Install the PostHog JavaScript web and Node SDKs.Install
posthog-js
using your package manager:Then, if you haven't created a root layout already, create a new file called
+layout.js
in yoursrc/routes
folder 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-node
using 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
handleError
callback 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
handleError
callback:src/hooks.server.ts - 5