react-code-dataset
/
next.js
/docs
/01-app
/03-api-reference
/03-file-conventions
/instrumentation-client.mdx
| --- | |
| title: instrumentation-client.js | |
| description: Learn how to add client-side instrumentation to track and monitor your Next.js application's frontend performance. | |
| --- | |
| The `instrumentation-client.js|ts` file allows you to add monitoring and analytics code that runs before your application's frontend code starts executing. This is useful for setting up performance tracking, error monitoring, or any other client-side observability tools. | |
| To use it, place the file in the **root** of your application or inside a `src` folder. | |
| ## Usage | |
| Unlike [server-side instrumentation](/docs/app/guides/instrumentation), you do not need to export any specific functions. You can write your monitoring code directly in the file: | |
| ```ts filename="instrumentation-client.ts" switcher | |
| // Set up performance monitoring | |
| performance.mark('app-init') | |
| // Initialize analytics | |
| console.log('Analytics initialized') | |
| // Set up error tracking | |
| window.addEventListener('error', (event) => { | |
| // Send to your error tracking service | |
| reportError(event.error) | |
| }) | |
| ``` | |
| ```js filename="instrumentation-client.js" switcher | |
| // Set up performance monitoring | |
| performance.mark('app-init') | |
| // Initialize analytics | |
| console.log('Analytics initialized') | |
| // Set up error tracking | |
| window.addEventListener('error', (event) => { | |
| // Send to your error tracking service | |
| reportError(event.error) | |
| }) | |
| ``` | |
| ## Version History | |
| | Version | Changes | | |
| | ------- | ----------------------------------- | | |
| | `v15.3` | `instrumentation-client` introduced | | |