capacitor.tsx 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. import {Fragment} from 'react';
  2. import {Layout, LayoutProps} from 'sentry/components/onboarding/gettingStartedDoc/layout';
  3. import {ModuleProps} from 'sentry/components/onboarding/gettingStartedDoc/sdkDocumentation';
  4. import {StepType} from 'sentry/components/onboarding/gettingStartedDoc/step';
  5. import {t, tct} from 'sentry/locale';
  6. // Configuration Start
  7. export const steps = ({
  8. dsn,
  9. }: {
  10. dsn?: string;
  11. } = {}): LayoutProps['steps'] => [
  12. {
  13. type: StepType.INSTALL,
  14. description: t(
  15. 'Install the Sentry Capacitor SDK alongside the sibling Sentry Angular SDK:'
  16. ),
  17. configurations: [
  18. {
  19. language: 'bash',
  20. code: `
  21. # npm
  22. npm install --save @sentry/capacitor @sentry/angular-ivy
  23. # yarn
  24. yarn add @sentry/capacitor @sentry/angular @sentry/tracing --exact
  25. `,
  26. },
  27. {
  28. language: 'bash',
  29. description: t(
  30. "Or install the standalone Sentry Capacitor SDK if you don't use Ionic/Angular:"
  31. ),
  32. code: `
  33. # npm
  34. npm install --save @sentry/capacitor @sentry/tracing
  35. # yarn
  36. yarn add @sentry/capacitor
  37. `,
  38. },
  39. {
  40. description: (
  41. <Fragment>
  42. <h5>{t('Capacitor 2 - Android')}</h5>
  43. {t('This step is not needed if you are using Capacitor 3')}
  44. <p>
  45. {tct(
  46. 'Then, add the [sentryCapacitorCode:SentryCapacitor] plugin class inside the [onCreateCode:onCreate] method of your [mainActivityCode:MainActivity] file.',
  47. {
  48. sentryCapacitorCode: <code />,
  49. onCreateCode: <code />,
  50. mainActivityCode: <code />,
  51. }
  52. )}
  53. </p>
  54. </Fragment>
  55. ),
  56. configurations: [
  57. {
  58. description: <strong>Java</strong>,
  59. language: 'java',
  60. code: `
  61. import android.os.Bundle;
  62. import com.getcapacitor.BridgeActivity;
  63. import com.getcapacitor.Plugin;
  64. import io.sentry.capacitor.SentryCapacitor;
  65. import java.util.ArrayList;
  66. public class MainActivity extends BridgeActivity {
  67. @Override
  68. public void onCreate(Bundle savedInstanceState) {
  69. super.onCreate(savedInstanceState);
  70. // Initializes the Bridge
  71. this.init(savedInstanceState, new ArrayList<Class<? extends Plugin>>() {{
  72. add(SentryCapacitor.class);
  73. }});
  74. }
  75. }
  76. `,
  77. },
  78. {
  79. description: <strong>Kotlin</strong>,
  80. language: 'kotlin',
  81. code: `
  82. import android.os.Bundle
  83. import com.getcapacitor.BridgeActivity
  84. import com.getcapacitor.Plugin
  85. import io.sentry.capacitor.SentryCapacitor
  86. class MainActivity : BridgeActivity() {
  87. override fun onCreate(savedInstanceState: Bundle?) {
  88. super.onCreate(savedInstanceState)
  89. // Initializes the Bridge
  90. this.init(
  91. savedInstanceState,
  92. listOf<Class<out Plugin>>(SentryCapacitor::class.java)
  93. )
  94. }
  95. }
  96. `,
  97. },
  98. ],
  99. },
  100. ],
  101. additionalInfo: (
  102. <p>
  103. {tct(
  104. 'The version of the sibling SDK must match with the version referred by Sentry Capacitor. To check which version of the sibling SDK is installed, use the following command: [code:npm info @sentry/capacitor peerDependencies]',
  105. {code: <code />}
  106. )}
  107. </p>
  108. ),
  109. },
  110. {
  111. type: StepType.CONFIGURE,
  112. description: t('You must initialize the Sentry SDK as early as you can:'),
  113. configurations: [
  114. {
  115. description: t('With Ionic/Angular:'),
  116. language: 'typescript',
  117. code: `
  118. // app.module.ts
  119. import * as Sentry from '@sentry/capacitor';
  120. // Use "@sentry/angular-ivy" for Angular 12+ or "@sentry/angular" for Angular 10 or 11
  121. import * as SentryAngular from '@sentry/angular-ivy';
  122. Sentry.init(
  123. {
  124. dsn: '${dsn}',
  125. // To set your release and dist versions
  126. release: 'my-project-name@' + process.env.npm_package_version,
  127. dist: '1',
  128. // Set tracesSampleRate to 1.0 to capture 100% of transactions for performance monitoring.
  129. // We recommend adjusting this value in production.
  130. tracesSampleRate: 1.0,
  131. integrations: [
  132. new SentryAngular.BrowserTracing({
  133. // Set "tracePropagationTargets" to control for which URLs distributed tracing should be enabled
  134. tracePropagationTargets: ["localhost", /^https:\/\/yourserver\.io\/api/],
  135. routingInstrumentation: SentryAngular.routingInstrumentation,
  136. }),
  137. ]
  138. },
  139. // Forward the init method from @sentry/angular
  140. SentryAngular.init
  141. );
  142. @NgModule({
  143. providers: [
  144. {
  145. provide: ErrorHandler,
  146. // Attach the Sentry ErrorHandler
  147. useValue: SentryAngular.createErrorHandler(),
  148. },
  149. {
  150. provide: SentryAngular.TraceService,
  151. deps: [Router],
  152. },
  153. {
  154. provide: APP_INITIALIZER,
  155. useFactory: () => () => {},
  156. deps: [SentryAngular.TraceService],
  157. multi: true,
  158. },
  159. ],
  160. })
  161. `,
  162. },
  163. {
  164. description: t('Standalone:'),
  165. language: 'javascript',
  166. code: `
  167. // App.js
  168. import * as Sentry from "@sentry/capacitor";
  169. Sentry.init({
  170. dsn: "${dsn}",
  171. // Set your release version, such as 'getsentry@1.0.0'
  172. release: "my-project-name@<release-name>",
  173. // Set your dist version, such as "1"
  174. dist: "<dist>",
  175. });
  176. `,
  177. },
  178. ],
  179. },
  180. {
  181. type: StepType.VERIFY,
  182. description: t(
  183. 'This snippet includes an intentional error, so you can test that everything is working as soon as you set it up:'
  184. ),
  185. configurations: [
  186. {
  187. language: 'javascript',
  188. code: `
  189. import * as Sentry from "@sentry/capacitor";
  190. Sentry.captureException("Test Captured Exception");
  191. `,
  192. },
  193. {
  194. language: 'javascript',
  195. description: t('You can also throw an error anywhere in your application:'),
  196. code: `
  197. // Must be thrown after Sentry.init is called to be captured.
  198. throw new Error("Test Thrown Error");
  199. `,
  200. },
  201. {
  202. language: 'javascript',
  203. description: t('Or trigger a native crash:'),
  204. code: `
  205. import * as Sentry from "@sentry/capacitor";
  206. Sentry.nativeCrash();
  207. `,
  208. },
  209. ],
  210. },
  211. ];
  212. // Configuration End
  213. export function GettingStartedWithCapacitor({dsn, ...props}: ModuleProps) {
  214. return <Layout steps={steps({dsn})} {...props} />;
  215. }
  216. export default GettingStartedWithCapacitor;