谷歌分析不工作的网站嵌入在iframe



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

我希望我的网站分析工作在iframe在anotherdomain.com。有谁能帮我吗?

如果我理解正确的话,

  • 你的网站是example.com,当你浏览它,你会收到事件在你的GA属性
  • 你的站点example.com作为iframe嵌入在example.org上,当你浏览example.org时,你不会在你的GA属性中收到example.com的任何事件。

SameSite饼干

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

这个是可以修复的。

GA UA

ga('create', 'UA-XXXXXXXX-YY', {
cookieFlags: 'samesite=none;secure'
});

developers.google.com

GA UA (with gtag)

gtag('config', 'UA-XXXXXXXX-YY', {
'cookie_flags': 'samesite=none;secure'
});

developers.google.com

GA4

gtag('config', 'G-XXXXXXXX', {
'cookie_flags': 'samesite=none;secure'
});

developers.google.com

沙箱iframe

其他选项可以是example.org嵌入你的example.com作为一个沙盒iframe,例如

<iframe src="https://example.com/" sandbox></iframe>

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

除了iframe的sandbox属性外,iframe还可能受到"内容安全策略(CSP)"的约束。或";Permissions-Policy"

你应该检查example.org的源代码,以了解更多关于你的网站是如何嵌入的,并且可能想联系example.com来更改它。

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

最新更新