运行 google Remix.run 上的分析脚本

Running google analytics script on Remix.run

我已经使用 remix 运行 构建了一个网络应用程序,我想添加 Google 分析。如何在不使打字稿生气的情况下将纯 JS 添加到 head 和 body 部分?

On any page, at anytime, you can flip between plain HTML and full client-side transitions.

If you need one tiny bit of interactivity, use a

<script
 dangerouslySetInnerHTML>.

示例,取自https://remix.run/docs/en/v1/guides/disabling-javascript

return (
  <>
    <select id="qty">
      <option>1</option>
      <option>2</option>
      <option value="contact">
        Contact Sales for more
      </option>
    </select>

    <script
      dangerouslySetInnerHTML={{
        __html: `
          document.addEventListener('DOMContentLoaded', () => {
            document.getElementById('qty').onchange = (event) => {
              if (event.target.value === "contact") {
                window.location.assign("/contact")
              }
            }
          });
        `
      }}
    />
  </>
);

这个存储库对我帮助很大:https://github.com/remix-run/remix/tree/main/examples/google-analytics

有一段时间让我感到困惑的一件事是我在 Brave 浏览器 上开发,它阻止了分析。

切换到 Chrome,Firefox、Safari 应该可以解决问题。