设置用户反馈

了解更多关于在事件发生时收集用户反馈。Sentry 将反馈与原始事件配对,为您提供更多问题的洞察。

当用户遇到错误时,Sentry 提供了收集额外反馈的能力。您可以根据 SDK 支持的方法收集反馈。

我们的嵌入式、基于 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 进行身份验证,然后传递在后端生成的事件 ID。

确保您已安装 JavaScript SDK:

Copied
<script
  src="https://browser.sentry-cdn.com/8.54.0/bundle.min.js"
  integrity="sha384-OOkJGcfpcHOW2qdROjcfMtqqMTrDnSBag7fGypKUDA4WxyB6mmS5cMzY9YBGcBvF"
  crossorigin="anonymous"
></script>

接下来,创建 resources/views/errors/500.blade.php,并嵌入反馈代码:

resources/views/errors/500.blade.php
Copied
<div class="content">
  <div class="title">Something went wrong.</div>

  @if(app()->bound('sentry') && app('sentry')->getLastEventId())
  <div class="subtitle">
    Error ID: {{ app('sentry')->getLastEventId() }}
  </div>
  <script>
    Sentry.init({ dsn: 'https://examplePublicKey@o0.ingest.sentry.io/0' });
    Sentry.showReportDialog({
      eventId: '{{ app('sentry')->getLastEventId() }}'
    });
  </script>
  @endif
</div>

对于 Laravel 5 到 5.4 需要一些额外的工作。您需要打开 App/Exceptions/Handler.php 并扩展 render 方法,以确保 500 错误正确渲染为视图,在 5.5+ 中此步骤不再需要。

app/Exceptions/Handler.php
Copied
<?php

use Symfony\Component\HttpKernel\Exception\HttpException;

class Handler extends ExceptionHandler
{
    public function report(Exception $exception)
    {
        if (app()->bound('sentry') && $this->shouldReport($exception)) {
            app('sentry')->captureException($exception);
        }

        parent::report($exception);
    }

    // This method is ONLY needed for Laravel 5 up to 5.4.
    // You can skip this method if you are using Laravel 5.5+.
    public function render($request, Exception $exception)
    {
        // Convert all non-http exceptions to a proper 500 http exception
        // if we don't do this exceptions are shown as a default template
        // instead of our own view in resources/views/errors/500.blade.php
        if ($this->shouldReport($exception) && !$this->isHttpException($exception) && !config('app.debug')) {
            $exception = new HttpException(500, 'Whoops!');
        }

        return parent::render($request, $exception);
    }
}