Google 由于 "SameSite" 和 "Secure" cookie 设置,分析在 IFrame 中被阻止

Google Analytics blocked in IFrame due to "SameSite" & "Secure" setting of cookies

我们在 our-site.com 上 运行 提供服务。我们的客户可以通过简单地从 their-site.com 链接到 our-site.com/customer-service 或通过 iFrame 将我们的服务嵌入 their-site.com 来使用服务 "stand alone"。想象一下直接访问 GMaps 与看到它嵌入到 IFrame 中。

正在访问 "foreign" 带有我们在 IFarme 中的服务的页面

从 Chrome 80(我猜)开始,当服务在 iFrame 内运行时,我们的 Google 分析停止工作。 Chrome 给我这个(非常清楚的)信息:

A cookie associated with a cross-site resource at http://our-site.com/ was set without the SameSite attribute. It has been blocked, as Chrome now only delivers cookies with cross-site requests if they are set with SameSite=None and Secure. You can review cookies in developer tools under Application>Storage>Cookies and see more details at https://www.chromestatus.com/feature/5088147346030592 and https://www.chromestatus.com/feature/5633521622188032.

此外,如果我访问 their-site.com 时在 iFrame 中嵌入了我们的 our-site.com/customer-service,我在 GA 实时概览中也看不到任何内容。

如果我手动禁用 chrome://flags 中的 2 个功能 same-site-by-default-cookiescookies-without-same-site-must-be-secure 并访问 their-site.com 并在 iFrame 中嵌入 our-site.com/customer-service 我确实看到了GA 实时概览中的页面访问。

访问我们的服务"stand alone"

当直接访问 our-site.com/customer-service GA 时仍然工作正常:

问题

当 运行 在外部域的 iFrame 中时,我可以做些什么来让 GA 再次工作?

示例代码

我刚刚创建了一个非常简单的测试环境,它也显示了上述问题:

我们的网站。com/customer-service

<html lang="en">
  <head>
    <!-- Global site tag (gtag.js) - Google Analytics -->
    <script async src="https://www.googletagmanager.com/gtag/js?id={tracking-id}"></script>
    <script>
      window.dataLayer = window.dataLayer || [];
      function gtag() { dataLayer.push(arguments); }
      gtag("js", new Date());
      gtag("config", "{tracking-id}");
    </script>

    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>our site service</title>
  </head>
  <body>our-site.com/customer-service</body>
</html>

他们的-site.com

<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>their-site.com</title>
  </head>
  <body>
    <iframe src="https://www.our-site.com/customer-service" style="width: 500px; height: 500px;"></iframe>
  </body>
</html>

即使是这个非常简单的示例也显示了上述行为,其中直接打开 our-site.com/customer-service 会在 GA 实时概览中显示数据,而打开 their-site.com 不会...

来自 Google tag manager support forum 的回答:

在第三方上下文 (iframe) 中访问第一方 cookie (_ga) 时,必须显式添加 cookieFlags 和值 samesite=none;secure

可以在 this blogpost.

找到各种场景的详细描述