Browser monitoring is a beta AppSignal Labs feature. The
@appsignal/browser package is published under the beta tag on npm, and its
configuration and API may still change between beta releases. Share feedback
in our Discord community.Find your Front-end API key
Find the Front-end API key under Front-end error monitoring on your application’s Push & deploy settings. Use the key for the application and environment you want to monitor. This key is meant to be included in browser code. It is different from the Push API keys used by back-end integrations. The install flow puts your key in the generated code. In the examples below, replace<YOUR_FRONTEND_API_KEY> with your own key.
Generate the snippet in AppSignal
The Browser SDK install flow lets you choose Plain JS or React and select which features to include. It starts with Route template selected. The complete examples below include every option:
The install flow generates Browser SDK code. Selecting Distributed tracing does not instrument your API service. To see its spans alongside a browser request error, follow the back-end setup as well.
Both versions also show how to report an error you catch yourself. The React version includes an error boundary for render errors.
Install the package
Install AppSignal for Browser from thebeta tag. Distributed tracing and failed-request reporting require version 1.0.0-beta.7 or later. The install flow currently shows an unversioned command; use one of these commands for the beta release:
Initialize the SDK
Import your AppSignal setup before the application code you want to monitor. Replaceprocess.env.NODE_ENV and process.env.APP_REVISION with the equivalent values from your build system. Set appVersion to your release tag or commit SHA so AppSignal can match errors to sourcemaps.
These examples include all install flow options. Replace api.example.com/** with your API host and path for distributed tracing. Replace the CSS selectors, tags, and router example with values from your application.
- Plain JS
- React
1. Initialize AppSignal in Import it first in your entry point:2. Report route changes from your router. Replace 3. Add tags after sign-in. Use values from the signed-in user or account. Call 4. Report errors you catch yourself. Replace
appsignal.js.appsignal.js
main.js
router.on with your router’s navigation hook. Report the initial route too, then report its template on each navigation.router.js
clearTags() on sign-out to clear stored tags.account.js
save() with your own operation.save.js
appsignal.js file. Replace <YOUR_APP_REVISION> with your release tag or commit SHA:
appsignal.js first in your entry point, as shown above. This smaller setup does not add traceparent headers to your API requests.
For every setup, active: false stops the SDK from collecting or sending data. endpoint defaults to the current page’s origin; set it to https://appsignal-endpoint.net unless you proxy AppSignal requests through your own domain. See all configuration options.
Install without a bundler
If your site loads JavaScript with<script> tags and has no bundler, use the UMD build. It exposes the SDK as AppsignalBrowser on window:
Report the current route
Use this step for a single-page application with routes such as/users/42. Pass the route template, such as /users/:id, when your router navigates. AppSignal uses it to group errors and web vitals by route. The full examples above show where to call setRouteTemplate().
If you do not set a route template, the SDK reports the current page path. That means /users/42 and /users/43 can appear as separate error groups. See React route examples for React Router and Next.js.
Verify the installation
-
Run a build with
activeset totrue. You can temporarily useactive: truein development. -
Open the page and run this code in the browser console:
The timer lets the error reach the page’s global error handler. An error thrown directly in the console is usually handled by the console itself.JavaScript
- Open Browser > Errors in AppSignal and find AppSignal test error.
Content Security Policy
If your site uses a Content Security Policy, allow the configured AppSignal endpoint in itsconnect-src directive. For example, add the endpoint to your existing allowed sources:
Assets hosted on a CDN
Use this step if your site’s JavaScript loads from another origin, such as a CDN. Without cross-origin access, the browser may report onlyScript error. with no stack or line number. The SDK drops those opaque errors because they cannot be traced back to your code.
Serve the script with an Access-Control-Allow-Origin header that permits your page’s origin, and add crossorigin="anonymous" to the script tag:
Uninstall
- Remove the
appsignal.jsimport and other@appsignal/browsercalls from your code. Remove anyErrorBoundaryimported from@appsignal/browser/react. - Remove the package and update your lockfile with
npm uninstall @appsignal/browser,yarn remove @appsignal/browser, orpnpm remove @appsignal/browser. If you installed the UMD build, remove its<script>tag instead. - Deploy the change. The new build stops reporting browser data.
- If this AppSignal application was used only for browser monitoring, you can optionally remove the application.
destroy(). It flushes pending data and stops collection until init() runs again, for example after a page reload.