|
| 1 | +--- |
| 2 | +name: Ionic |
| 3 | +doc_link: https://docs.sentry.io/platforms/javascript/guides/capacitor/ |
| 4 | +support_level: production |
| 5 | +type: framework |
| 6 | +--- |
| 7 | + |
| 8 | +To use Sentry in your Ionic app, install the Sentry Capacitor SDK alongside the sibling Sentry SDK related to the Web framework you're using with Ionic. |
| 9 | +The supported siblings are: Angular `@sentry/angular`, React `@sentry/react` and Vue `@sentry/vue`. |
| 10 | + |
| 11 | +Heres an example of installing Sentry Capacitor along with Sentry Angular: |
| 12 | +``` |
| 13 | +npm install --save @sentry/capacitor @sentry/angular |
| 14 | +``` |
| 15 | +or |
| 16 | +``` |
| 17 | +yarn add @sentry/capacitor @sentry/angular |
| 18 | +``` |
| 19 | +The same installation process applies to the other siblings, all you need to do is to replace `@sentry/angular` by the desired sibling. |
| 20 | + |
| 21 | +## Android Installation |
| 22 | + |
| 23 | +Then, add the `SentryCapacitor` plugin class inside the `onCreate` method of your `MainActivity` file. |
| 24 | + |
| 25 | +Java: |
| 26 | + |
| 27 | +```java |
| 28 | +import android.os.Bundle; |
| 29 | +import com.getcapacitor.BridgeActivity; |
| 30 | +import com.getcapacitor.Plugin; |
| 31 | +import io.sentry.capacitor.SentryCapacitor; |
| 32 | +import java.util.ArrayList; |
| 33 | + |
| 34 | +public class MainActivity extends BridgeActivity { |
| 35 | + @Override |
| 36 | + public void onCreate(Bundle savedInstanceState) { |
| 37 | + super.onCreate(savedInstanceState); |
| 38 | + // Initializes the Bridge |
| 39 | + this.init(savedInstanceState, new ArrayList<Class<? extends Plugin>>() {{ |
| 40 | + add(SentryCapacitor.class); |
| 41 | + }}); |
| 42 | + } |
| 43 | +} |
| 44 | +``` |
| 45 | + |
| 46 | +Kotlin: |
| 47 | + |
| 48 | +```kotlin |
| 49 | +import android.os.Bundle |
| 50 | +import com.getcapacitor.BridgeActivity |
| 51 | +import com.getcapacitor.Plugin |
| 52 | +import io.sentry.capacitor.SentryCapacitor |
| 53 | + |
| 54 | +class MainActivity : BridgeActivity() { |
| 55 | + fun onCreate(savedInstanceState: Bundle?) { |
| 56 | + super.onCreate(savedInstanceState) |
| 57 | + // Initializes the Bridge |
| 58 | + this.init( |
| 59 | + savedInstanceState, |
| 60 | + listOf<Class<out Plugin>>(SentryCapacitor::class.java) |
| 61 | + ) |
| 62 | + } |
| 63 | +} |
| 64 | +``` |
| 65 | + |
| 66 | +## Initializing the SDK |
| 67 | + |
| 68 | +You must initialize the Sentry SDK as early as you can: |
| 69 | + |
| 70 | +```javascript |
| 71 | +import * as Sentry from "@sentry/capacitor"; |
| 72 | +// The example is using Angular, Import "@sentry/vue" or "@sentry/react" when using a Sibling different than Angular. |
| 73 | +import * as SentrySibling from "@sentry/angular"; |
| 74 | +// For automatic instrumentation (highly recommended) |
| 75 | +import { BrowserTracing } from "@sentry/tracing"; |
| 76 | + |
| 77 | +Sentry.init( |
| 78 | + { |
| 79 | + dsn: "___PUBLIC_DSN___", |
| 80 | + // To set your release and dist versions |
| 81 | + release: "my-project-name@" + process.env.npm_package_version, |
| 82 | + dist: "1", |
| 83 | + // Set tracesSampleRate to 1.0 to capture 100% of transactions for performance monitoring. |
| 84 | + // We recommend adjusting this value in production. |
| 85 | + tracesSampleRate: 1.0, |
| 86 | + integrations: [ |
| 87 | + new BrowserTracing({ |
| 88 | + tracingOrigins: ["localhost", "https://yourserver.io/api"], |
| 89 | + }), |
| 90 | + ] |
| 91 | + }, |
| 92 | + // Forward the init method to the sibling Framework. |
| 93 | + SentrySibling.init |
| 94 | +); |
| 95 | +``` |
| 96 | + |
| 97 | +Additionally for Angular, you will also need to alter NgModule (same code doesn't apply to other siblings) |
| 98 | + |
| 99 | +```javascript |
| 100 | +@NgModule({ |
| 101 | + providers: [ |
| 102 | + { |
| 103 | + provide: ErrorHandler, |
| 104 | + // Attach the Sentry ErrorHandler |
| 105 | + useValue: SentrySibling.createErrorHandler(), |
| 106 | + }, |
| 107 | + ], |
| 108 | +}) |
| 109 | +``` |
| 110 | + |
| 111 | +## Verify |
| 112 | + |
| 113 | +This snippet includes an intentional error, so you can test that everything is working as soon as you set it up: |
| 114 | + |
| 115 | +```javascript |
| 116 | +import * as Sentry from "@sentry/capacitor"; |
| 117 | + |
| 118 | +Sentry.captureException("Test Captured Exception"); |
| 119 | +``` |
| 120 | + |
| 121 | +You can also throw an error anywhere in your application: |
| 122 | + |
| 123 | +```javascript |
| 124 | +// Must be thrown after Sentry.init is called to be captured. |
| 125 | +throw new Error(`Test Thrown Error`); |
| 126 | +``` |
| 127 | + |
| 128 | +Or trigger a native crash: |
| 129 | + |
| 130 | +```javascript |
| 131 | +import * as Sentry from "@sentry/capacitor"; |
| 132 | + |
| 133 | +Sentry.nativeCrash(); |
| 134 | +``` |
0 commit comments