|
| 1 | +--- |
| 2 | +title: Track Svelte Components |
| 3 | +--- |
| 4 | + |
| 5 | +Sentry's Svelte SDK offers a feature to monitor the performance of your Svelte components: component tracking. Enabling this feature provides you with spans in your transactions that show the initialization and update cycles of your Svelte components. This allows you to get a drilled-down view into how your components are behaving so you can do things like identify slow initializations or frequent updates, which might have an impact on your app's performance. |
| 6 | + |
| 7 | +## Usage |
| 8 | + |
| 9 | +To get started, add the Sentry `componentTrackingPreprocessor` to your `svelte.config.js` file: |
| 10 | + |
| 11 | +```javascript {tabTitle: svelte.config.js} |
| 12 | +import * as Sentry from "@sentry/svelte"; |
| 13 | + |
| 14 | +const config = { |
| 15 | + preprocess: [ |
| 16 | + Sentry.componentTrackingPreprocessor({ |
| 17 | + // Add the components you want to be tracked to this array. |
| 18 | + // Specificy `true` to track all components or `false` to disable |
| 19 | + // tracking entirely |
| 20 | + // (defaults to `true`) |
| 21 | + trackComponents: ["Navbar", "PrimaryButton", "LoginForm"], |
| 22 | + // To disable component initialization spans, set this to `false`. |
| 23 | + // (defaults to `true`) |
| 24 | + trackInit: true, |
| 25 | + // To disable component update spans, set this to `false` |
| 26 | + // (defaults to `true`) |
| 27 | + trackUpdates: false, |
| 28 | + }), |
| 29 | + // ... |
| 30 | + ], |
| 31 | + // ... |
| 32 | +}; |
| 33 | + |
| 34 | +export default config; |
| 35 | +``` |
| 36 | + |
| 37 | +This preprocessor is called at application build time. It inserts a function call to a function in the Sentry SDK into the `<script>` tag of your components before your app is compiled and bundled. The called function takes care of recording the spans by using the Svelte component's lifecycle hooks. |
| 38 | + |
| 39 | +## Alternative Usage |
| 40 | + |
| 41 | +If you don't want to use our preprocessor to automatically insert the function call at build time, you can call the tracking function manually in every component you would like to see tracked: |
| 42 | + |
| 43 | +```javascript {tabTitle: MyComponent.svelte} |
| 44 | +<script> |
| 45 | + import * as Sentry from "@sentry/svelte"; |
| 46 | + Sentry.trackComponent({ |
| 47 | + // To disable component initialization spans, set this to `false`. |
| 48 | + // (defaults to `true`) |
| 49 | + trackInit: true, |
| 50 | + // To disable component update spans, set this to `false` |
| 51 | + // (defaults to `true`) |
| 52 | + trackUpdates: false, |
| 53 | + // Specify a custom name to be shown in the span description |
| 54 | + // (defaults to the automatically detected component name) |
| 55 | + componentName: string |
| 56 | + }) |
| 57 | + // rest of your code |
| 58 | +</script> |
| 59 | +``` |
0 commit comments