UNPKG

2.28 kBMarkdownView Raw
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
16Sentry JavaScript SDKs. Public interfaces might change and break backwards
17compatibility from time to time. We absolutely recommend
18[raven-js](https://github.com/getsentry/raven-js) in production!
19
20## Usage
21
22To use this SDK, call `init(options)` as early as possible after loading the
23page. This will initialize the SDK and hook into the environment. Note that you
24can turn off almost all side effects using the respective options.
25
26```javascript
27import { init } from '@sentry/browser';
28
29init({
30 dsn: '__DSN__',
31 // ...
32});
33```
34
35To 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
37you have called `init()`:
38
39```javascript
40import * as Sentry from '@sentry/browser';
41
42// Set user information, as well as tags and further extras
43Sentry.setExtraContext({ battery: 0.7 });
44Sentry.setTagsContext({ user_mode: 'admin' });
45Sentry.setUserContext({ id: '4711' });
46
47// Add a breadcrumb for future events
48Sentry.addBreadcrumb({
49 message: 'My Breadcrumb',
50 // ...
51});
52
53// Capture exceptions, messages or manual events
54Sentry.captureMessage('Hello, world!');
55Sentry.captureException(new Error('Good bye'));
56Sentry.captureEvent({
57 message: 'Manual',
58 stacktrace: [
59 // ...
60 ],
61});
62```
63
64## Advanced Usage
65
66If you don't want to use a global static instance of Sentry, you can create one
67yourself:
68
69```javascript
70import { BrowserFrontend } from '@sentry/browser';
71
72const client = new BrowserFrontend({
73 dsn: '__DSN__',
74 // ...
75});
76
77client.install();
78// ...
79```