1 | <p align="center">
|
2 | <a href="https://sentry.io" target="_blank" align="center">
|
3 | <img src="https://sentry-brand.storage.googleapis.com/sentry-logo-black.png" width="280">
|
4 | </a>
|
5 | <br />
|
6 | </p>
|
7 |
|
8 | # Official Sentry SDK for Browsers (Preview)
|
9 |
|
10 | [![npm version](https://img.shields.io/npm/v/@sentry/browser.svg)](https://www.npmjs.com/package/@sentry/browser)
|
11 | [![npm dm](https://img.shields.io/npm/dm/@sentry/browser.svg)](https://www.npmjs.com/package/@sentry/browser)
|
12 | [![npm dt](https://img.shields.io/npm/dt/@sentry/browser.svg)](https://www.npmjs.com/package/@sentry/browser)
|
13 |
|
14 | **WARNING:** This SDK is part of an early access preview for the
|
15 | [next generation](https://github.com/getsentry/raven-js/tree/next#readme) of
|
16 | Sentry JavaScript SDKs. Public interfaces might change and break backwards
|
17 | compatibility from time to time. We absolutely recommend
|
18 | [raven-js](https://github.com/getsentry/raven-js) in production!
|
19 |
|
20 | ## Usage
|
21 |
|
22 | To use this SDK, call `init(options)` as early as possible after loading the
|
23 | page. This will initialize the SDK and hook into the environment. Note that you
|
24 | can turn off almost all side effects using the respective options.
|
25 |
|
26 | ```javascript
|
27 | import { init } from '@sentry/browser';
|
28 |
|
29 | init({
|
30 | dsn: '__DSN__',
|
31 | // ...
|
32 | });
|
33 | ```
|
34 |
|
35 | To set context information or send manual events, use the exported functions of
|
36 | `@sentry/browser`. Note that these functions will not perform any action before
|
37 | you have called `init()`:
|
38 |
|
39 | ```javascript
|
40 | import * as Sentry from '@sentry/browser';
|
41 |
|
42 | // Set user information, as well as tags and further extras
|
43 | Sentry.setExtraContext({ battery: 0.7 });
|
44 | Sentry.setTagsContext({ user_mode: 'admin' });
|
45 | Sentry.setUserContext({ id: '4711' });
|
46 |
|
47 | // Add a breadcrumb for future events
|
48 | Sentry.addBreadcrumb({
|
49 | message: 'My Breadcrumb',
|
50 | // ...
|
51 | });
|
52 |
|
53 | // Capture exceptions, messages or manual events
|
54 | Sentry.captureMessage('Hello, world!');
|
55 | Sentry.captureException(new Error('Good bye'));
|
56 | Sentry.captureEvent({
|
57 | message: 'Manual',
|
58 | stacktrace: [
|
59 | // ...
|
60 | ],
|
61 | });
|
62 | ```
|
63 |
|
64 | ## Advanced Usage
|
65 |
|
66 | If you don't want to use a global static instance of Sentry, you can create one
|
67 | yourself:
|
68 |
|
69 | ```javascript
|
70 | import { BrowserFrontend } from '@sentry/browser';
|
71 |
|
72 | const client = new BrowserFrontend({
|
73 | dsn: '__DSN__',
|
74 | // ...
|
75 | });
|
76 |
|
77 | client.install();
|
78 | // ...
|
79 | ```
|