jsLoader.tsx 5.0 KB

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