I have a basic static / single page app I added Google Analytics 4 (GA4) to using gtag
. Page views are tracked as expected when I load my app locally via http://localhost:8080. However, when I embed by app on another site in an iframe, no hits are sent.
To debug this, I've looked at the network requests in the Chrome network inspector. When I open my app directly at http://localhost:8080, I can see a request to https://analytics.google.com/g/collect?... in the network requests. However, when I load my app in an iframe (e.g. I add <iframe src="http://localhost:8080"></iframe>
to another page), I notice that this request isn't sent. I've also confirmed that no data shows up in Google Analytics in the Realtime view even though I can see the JavaScript for gtag loading.
I've also debugged by using the Tag Assistant at https://tagassistant.google.com/. Even when I connect to my app running in the iframe, the debugger shows that all events are registered as expected, but under hits sent, it says "No hits were sent by this container".
Why would the behavior of the same tracking code behave any differently when the app is loaded in an iframe? How do I ensure that hits are sent all the time?
Update: This appears to be related to cookies not being available within the cross-domain iframe, but is there a workaround?
Just go to your Google Tag Manager container, open Google Analytics Settings Variable that is used by your GA tags and set Cookie Domain to auto. By default, all GA Settings Variables have it enabled. That's it. There is no need to implement GA cross domain tracking for subdomains.
You get a 'No Hits' notification if your Google Analytics tracking has stopped working. Either someone has removed the GA tracking code from your website or the tracking code has broken or is not firing anymore. There could also be chances that your website suffered a temporary outage.
Your tracking code is not set up correctly Use a tool like GA Checkerto verify that you have added the Google Analytics tracking code to every page of your website. Make sure you have preserved the correct code formattingwhen formatting your Global Site Tag from your Google Analytics account.
In some cases, when you use third-party forms and embed them onto your website, the code is on both the forms (in an iFrame) and on the website. This causes Google Analytics to fire twice. The fix: Remove the code from the forms! Or use a different GA code for the other tool if you must keep the tracking for other use cases.
Traffic from a page with missing javascript code If there is a page on your website that doesn’t have the Google Analytics tracking code or the Google Tag Manager tracking code properly installed, traffic from this page to other pages will appear as direct traffic.
Send data to Google Analytics with gtag.js 1 Send data with the event command. Once you've added the global snippet to a web page, use the event command to send data to Google Analytics. 2 Route data to groups and properties. ... 3 Know when an event has been sent. ... 4 Specify different transport mechanisms. ...
By default, tracking fails due to SameSite cookie setting enforcement. By default, cookies aren't available in a third-party context, and this includes and iframe that's included from a different domain.
By using the cookie_flags config (docs), you can allow cookies to be read from a third-party context. There will be some caveats and this will vary by browser, as privacy restrictions keep increasing.
However, for now, setting cookie_flags
in the gtag
config fixes the issue as long as your site is secure:
gtag('config', '<MEASUREMENT_ID>', {
cookie_flags: 'SameSite=None;Secure'
})
Note that this won't actually work with http://localhost:8080 since it's not secure, but once the site is deployed to production, hopefully you'll be using https.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With