Explore documentation
Install the JavaScript tag
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.
Install the snippet
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:
Replace YOUR_TOKEN with the token shown in the Frontend tab.
The snippet loads asynchronously and won't block page rendering. You never need to update it β the remote script (b.js) is always generated with your latest configuration.
Initialize
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:
Identify users
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 are standard fields. You can add arbitrary custom properties (like plan above) β they're stored alongside the user data.
Call this after sign-in. The association persists for the page session. To clear (e.g., on 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.
Track custom events
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.
Custom configuration
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.- Any other supported option from the Sentry JavaScript SDK.
All other keys are merged with the init configuration.
This can also be managed remotely from the Frontend tab β see Custom configuration.
Verify installation
- Open your application in a browser.
- Go to the Frontend tab and check the Verify data collection section β it shows whether data has been received.
- Alternatively, open Live tail to see events arriving in real time.
Next steps
- Configuration β UI settings: collection toggles, sampling, session replays, sessions, batching, and more
- Events reference β schema of all captured events