capacitor.tsx 6.3 KB

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