This page covers everything you need to add to your application code. All other settings are managed remotely from the Better Stack UI, see Configuration.
Navigate to your application's Frontend tab and copy the <script> tag. Place it before the closing </head> tag on every page you want to instrument:
The snippet loads asynchronously and won't block page rendering. You never need to update it, as the remote script (b.js) is always generated with your latest configuration.
The JavaScript tag uses Sentry's browser SDK internally. If your site also loads its own Sentry SDK, both run on the same shared global Sentry instance and may interfere with each other, producing corrupted or unreliable data on both sides. Remove previous Sentry SDK instances before installing the JavaScript tag.
The betterstack('init', opts) call starts data collection. You can pass the following options:
| Option | Type | Default | Description |
|---|---|---|---|
environment |
string |
undefined |
Environment name (e.g., 'production', 'staging'). Passed to error tracking for filtering. |
release |
string |
undefined |
App version or release identifier. Useful for tracking regressions across deployments. |
autoPageview |
boolean |
true |
Automatically track page views. Set to false to track navigation manually. |
debug |
boolean |
false |
Log internal activity to the browser console. Useful during development. |
Example:
Call betterstack('user', ...) to associate the current session with a known user. Once set, all subsequent errors, events, and session replays are linked to that user.
id, email, and username uniquely identify the user. group_id and group_name associate the user with a group in order to track behavior at the company, team, or project level.
You can add arbitrary custom properties, like plan in the example above, they're stored alongside the user data.
Call this after sign-in. The association persists for the page session. To clear it at sign-out:
If Auto-capture identified user events is enabled in the Frontend tab, clicks, form submissions, and other interactions are automatically captured for identified users.
Use betterstack('track', eventName, data) to send custom events with an arbitrary payload:
eventName: a string identifying the event type.data: an object with any key-value pairs you want to record.Events appear in Live tail and can be queried in dashboards. The full event schema is documented in the Events reference.
Custom events respect sampling. If the current session was sampled out of web event tracking, track calls are silently dropped. Events are also gated by the Auto-capture anonymous user events and Auto-capture identified user events toggles depending on whether a user has been identified.
When trace collection is enabled, the tag captures a pageload trace for the initial browser request. If your backend already started a trace for this request, the tag can continue it, so the frontend pageload becomes part of the same end-to-end trace instead of a new, disconnected one.
The tag reads a W3C traceparent that your server emits with the HTML response and adopts it as the parent of the pageload trace. No configuration is needed: continuation happens automatically whenever a valid traceparent is present. Your server only has to expose it in one of two ways.
Render a traceparent meta tag into the document <head>:
Most tracing SDKs can produce this value for the current server span.
If your server can set a response header but not render markup, expose the traceparent as a Server-Timing entry named traceparent on the document response:
The tag reads this from the browser's Performance API. The header must be on the main document (navigation) response and served from the same origin as the page.
When both are present, the meta tag takes precedence. If neither is present, the tag generates a new trace ID for the pageload span.
Use betterstack('config', ...) to set configuration options before init is called:
config must be called before init. Calling it after has no effect: a warning is logged if debug is enabled.
The sentry key passes options directly to the underlying error tracking SDK, deep-merged with defaults. Common use cases:
ignoreErrors: array of error message patterns to suppress.denyUrls / allowUrls: filter errors by script URL.All other keys are merged with the init configuration.
This can also be managed remotely from the Frontend tab: see Custom configuration.
We use cookies to authenticate users, improve the product user experience, and for personalized ads. Learn more.