设置用户反馈

了解如何在应用程序中启用用户反馈。

用户反馈功能允许您在应用程序中的任何地方、任何时间收集用户反馈,而无需首先发生错误事件。另一方面,崩溃报告模态框 功能允许您在错误事件发生时提示用户反馈。

请注意,如果您使用的是自托管的 Sentry 实例,则需要使用版本 24.4.2 或更高版本才能使用用户反馈功能的全部功能。较低版本可能具有有限的功能。

用户反馈 API 允许您在使用自己的 UI 时收集用户反馈。您可以使用应用程序中的相同编程语言发送用户反馈。在这种情况下,SDK 会创建 HTTP 请求,因此您无需通过 HTTP 发送数据。

Sentry 将反馈与原始事件配对,为您提供更多关于问题的见解。Sentry 需要 eventId 才能将用户反馈与相应的事件关联起来。例如,要获取 eventId,您可以使用 beforeSend或捕获事件方法的返回值。

Copied
import * as Sentry from "@sentry/browser";

const eventId = Sentry.captureMessage("User Feedback");
// OR: const eventId = Sentry.lastEventId();

const userFeedback = {
  name: "John Doe",
  email: "john@doe.com",
  message: "I really like your App, thanks!",
  associatedEventId: eventId,
};
Sentry.captureFeedback(userFeedback);

您还可以通过传递 hint 作为第二个参数来附加更多数据到反馈事件。这与其他 capture 方法类似:

Copied
Sentry.captureFeedback(
  { message: "I really like your App, thanks!" },
  {
    captureContext: {
      tags: { key: "value" },
    },
    attachments: [
      {
        filename: "screenshot.png",
        data: "base64-encoded-image",
      },
    ],
  },
);

或者,您可以直接使用 用户反馈 API 端点

我们的可嵌入、基于 JavaScript 的崩溃报告模态框在您通常会在网站上渲染一个简单的错误页面(经典的 500.html)时非常有用。

为了收集反馈,崩溃报告模态框会请求并收集用户的姓名、电子邮件地址和事件描述。当提供反馈时,Sentry 会将反馈与原始事件配对,为您提供更多关于问题的见解。

下面的截图提供了崩溃报告模态框的示例,具体样式可能因您的自定义而有所不同:

An example of a Crash-Report modal with text boxes for user name, email, and additional details about the break.

模态框使用您的公共 DSN 进行身份验证,然后传入在后端生成的 Event ID。

如果您使用的是像 ReactAngular 这样的框架,收集用户反馈的最佳位置是在您的错误处理组件中。(请参阅平台特定的文档以获取示例。)如果您没有使用框架,可以在事件发送之前使用 beforeSend 来收集反馈:

Copied
<script>
  Sentry.init({
    dsn: "https://examplePublicKey@o0.ingest.sentry.io/0",
    beforeSend(event, hint) {
      // Check if it is an exception, and if so, show the report dialog
      if (event.exception && event.event_id) {
        Sentry.showReportDialog({ eventId: event.event_id });
      }
      return event;
    },
  });
</script>