Google 分析不适用于嵌入 iframe 的网站

Roe*_*sur 3 google-analytics google-analytics-api google-analytics-firebase firebase-analytics

我的网站上运行谷歌分析:mydomain.com 我不拥有的另一个网站(anotherdoamin.com)已将我的网站嵌入到其页面上的 iframe 中。

我希望我的网站分析能够在 anotherdomain.com 的 iframe 中工作。有人可以帮我吗?

Tim*_*imW 6

如果我理解正确的话

  • 您的网站是 example.com,当您浏览该网站时,您将在 GA 媒体资源中收到事件。
  • 您的网站 example.com 作为 iframe 嵌入在 example.org 上,当您浏览 example.org 时,您的 GA 媒体资源中不会收到来自 example.com 的任何事件。

同站点 cookie

如果您使用默认/标准 Google Analytics 模板,则其 cookie 仅在第一方上下文中工作。它们受SameSite 属性的约束约束。您嵌入的网站将无法访问 GA cookie,并且 GA 将不会发送任何事件。

这是可以修复的。

GAUA

ga('create', 'UA-XXXXXXXX-YY', {
  cookieFlags: 'samesite=none;secure'
});
Run Code Online (Sandbox Code Playgroud)

开发者.google.com

GA UA(带 gtag)

gtag('config', 'UA-XXXXXXXX-YY', {
  'cookie_flags': 'samesite=none;secure'
});
Run Code Online (Sandbox Code Playgroud)

开发者.google.com

GA4

gtag('config', 'G-XXXXXXXX', {
  'cookie_flags': 'samesite=none;secure'
});
Run Code Online (Sandbox Code Playgroud)

开发者.google.com

沙盒 iframe

其他选项可能是 example.org 将您的 example.com 作为沙盒 iframe嵌入,例如

<iframe src="https://example.com/" sandbox></iframe>
Run Code Online (Sandbox Code Playgroud)

这将阻止您的网站加载和执行 GA。

除了 iframe 的沙箱属性之外,iframe 还可能受到“内容安全策略 (CSP)”或“权限策略”的约束

您应该检查 example.org 的源代码以了解有关您的网站如何嵌入的更多信息,并且可能需要联系 example.com 进行更改。

无论哪种方式,在 GA 中,您的所有事件都将显示为从 example.com 发送。如果不进行额外的努力,您将不会知道这些事件源自嵌入 example.org 中的 example.com。