jsLoader.tsx 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. import beautify from 'js-beautify';
  2. import {Alert} from 'sentry/components/core/alert';
  3. import ExternalLink from 'sentry/components/links/externalLink';
  4. import TracePropagationMessage from 'sentry/components/onboarding/gettingStartedDoc/replay/tracePropagationMessage';
  5. import {StepType} from 'sentry/components/onboarding/gettingStartedDoc/step';
  6. import type {
  7. DocsParams,
  8. OnboardingConfig,
  9. } from 'sentry/components/onboarding/gettingStartedDoc/types';
  10. import {
  11. getReplayConfigureDescription,
  12. getReplayJsLoaderSdkSetupSnippet,
  13. } from 'sentry/components/onboarding/gettingStartedDoc/utils/replayOnboarding';
  14. import {t, tct} from 'sentry/locale';
  15. import normalizeUrl from 'sentry/utils/url/normalizeUrl';
  16. type Params = DocsParams;
  17. const getInstallConfig = (params: Params) => [
  18. {
  19. type: StepType.INSTALL,
  20. configurations: [
  21. {
  22. description: t('Add this script tag to the top of the page:'),
  23. language: 'html',
  24. code: beautify.html(
  25. `<script src="${params.dsn.cdn}" crossorigin="anonymous"></script>`,
  26. {indent_size: 2, wrap_attributes: 'force-expand-multiline'}
  27. ),
  28. additionalInfo: (
  29. <Alert type="info" showIcon>
  30. {tct(
  31. 'Make sure that Session Replay is enabled in your [link:project settings].',
  32. {
  33. link: (
  34. <ExternalLink
  35. href={normalizeUrl(
  36. `/settings/${params.organization.slug}/projects/${params.projectSlug}/loader-script/`
  37. )}
  38. />
  39. ),
  40. }
  41. )}
  42. </Alert>
  43. ),
  44. },
  45. ],
  46. },
  47. ];
  48. const getVerifySnippet = () => `
  49. <!-- A button to trigger a test error -->
  50. <button id="test-error">Trigger Test Error</button>
  51. <script>
  52. const button = document.getElementById('test-error');
  53. button.addEventListener('click', () => {
  54. throw new Error('This is a test error');
  55. });
  56. </script>`;
  57. const feedbackOnboardingJsLoader: OnboardingConfig = {
  58. install: (params: Params) => [
  59. {
  60. type: StepType.INSTALL,
  61. configurations: [
  62. {
  63. description: t('Add this script tag to the top of the page:'),
  64. language: 'html',
  65. code: beautify.html(
  66. `<script src="${params.dsn.cdn}" crossorigin="anonymous"></script>`,
  67. {indent_size: 2, wrap_attributes: 'force-expand-multiline'}
  68. ),
  69. },
  70. ],
  71. },
  72. ],
  73. configure: () => [
  74. {
  75. type: StepType.CONFIGURE,
  76. description: t(
  77. 'When using the Loader Script, you can lazy load the User Feedback integration like this:'
  78. ),
  79. configurations: [
  80. {
  81. code: [
  82. {
  83. label: 'JavaScript',
  84. value: 'javascript',
  85. language: 'javascript',
  86. code: `
  87. window.sentryOnLoad = function () {
  88. Sentry.init({
  89. // add other configuration here
  90. });
  91. Sentry.lazyLoadIntegration("feedbackIntegration")
  92. .then((feedbackIntegration) => {
  93. Sentry.addIntegration(feedbackIntegration({
  94. // User Feedback configuration options
  95. }));
  96. })
  97. .catch(() => {
  98. // this can happen if e.g. a network error occurs,
  99. // in this case User Feedback will not be enabled
  100. });
  101. };
  102. `,
  103. },
  104. ],
  105. },
  106. ],
  107. additionalInfo: tct(
  108. `For a full list of User Feedback configuration options, [link:read the docs].`,
  109. {
  110. link: (
  111. <ExternalLink href="https://docs.sentry.io/platforms/javascript/user-feedback/configuration/" />
  112. ),
  113. }
  114. ),
  115. },
  116. ],
  117. verify: () => [],
  118. nextSteps: () => [],
  119. };
  120. const replayOnboardingJsLoader: OnboardingConfig = {
  121. install: (params: Params) => getInstallConfig(params),
  122. configure: (params: Params) => [
  123. {
  124. title: t('Configure Session Replay (Optional)'),
  125. description: getReplayConfigureDescription({
  126. link: 'https://docs.sentry.io/platforms/javascript/session-replay/',
  127. }),
  128. configurations: [
  129. {
  130. language: 'html',
  131. code: getReplayJsLoaderSdkSetupSnippet(params),
  132. },
  133. ],
  134. collapsible: true,
  135. additionalInfo: <TracePropagationMessage />,
  136. },
  137. ],
  138. verify: () => [
  139. {
  140. type: StepType.VERIFY,
  141. description: t(
  142. 'To verify your Replay setup, trigger an error on your page and watch Sentry capture the event along with a recording of the user interaction.'
  143. ),
  144. configurations: [
  145. {
  146. description: t('You can simulate an error by adding the following code:'),
  147. language: 'html',
  148. code: getVerifySnippet(),
  149. additionalInfo: t(
  150. 'After clicking the button, wait a few moments, and you\'ll see a new session appear on the "Replays" page.'
  151. ),
  152. },
  153. ],
  154. },
  155. ],
  156. nextSteps: () => [],
  157. };
  158. export {feedbackOnboardingJsLoader, replayOnboardingJsLoader};