Roee Tsur
Roee Tsur

Reputation: 111

Google analytics not working for web site embeded in iframe

I have google analytics running on my website: mydomain.com Another website (anotherdoamin.com) that I don't own has embedded my website in an iframe on one if its pages.

I want my websites analytics to work in the iframe in anotherdomain.com . can any on help me with that?

Upvotes: 5

Views: 3013

Answers (1)

TimW
TimW

Reputation: 656

If I understand correctly,

  • your site is example.com and when you browse it you'll receive events in your GA property.
  • your site example.com is embedded as an iframe on example.org and when you browse example.org you don't receive any events from example.com in your GA property.

SameSite cookie

If you are using the default/standard Google Analytics templates, its cookies only work in first-party context. They are subject to SameSite attribute. You embedded site won't be able to access the GA cookie(s) and GA won't send any events then.

This can be fixed.

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

Sandboxed iframe

Other option could be that example.org embeddeds your example.com as a sandboxed iframe, e.g.

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

which will prevent your site from loading and executing GA.

Apart from the iframe's sandbox attribute the iframe might be also subject to "Content Security Policy (CSP)" or "Permissions-Policy"

You should check example.org's source code to learn more about how your site has been embedded and probably want to contact example.com to change it.

Either way, in GA all your events will appear as being sent from example.com. Without additional efforts, you won't know that the events originated from example.com embedded in example.org.

Upvotes: 10

Related Questions