简体   繁体   中英

Google Analytics hardcoded events (NO GTM) not showing in an IFRAME

I set up a Google Analytics tag for my website, and I'm trying to fire custom events, but it's not working. The event is fired from an IFRAME, and it's not capturing in google analytics. If I'm using the full code as below, it works, but I want to split it and use the GA Tag for all websites and set hardcoded custom events within Iframe as the print screen.

<!-- Global site tag (gtag.js) - Google Analytics -->
<script async src="https://www.googletagmanager.com/gtag/js?id=UA-9999999-1"></script>
<script>
 window.dataLayer = window.dataLayer || [];
 function gtag(){dataLayer.push(arguments);}
 gtag('js', new Date());
 gtag('config', 'UA-99999999-1');
</script>
<script>gtag('event', 'visit', {
 'event_category': 'thisweek',
 'event_label': 'today',
 'value': 0
});</script>

So how to fire this event from inside an iframe: gtag('event'.......)

<script>gtag('event', 'visit', {
 'event_category': 'thisweek',
 'event_label': 'today',
 'value': 0
});</script>

How the events fired in the website

you need to use full gtag snippet within your Iframe, like:

<script async src="https://www.googletagmanager.com/gtag/js?id=UA-9999999-1"></script>
<script>
 window.dataLayer = window.dataLayer || [];
 function gtag(){dataLayer.push(arguments);}
 gtag('js', new Date());
 gtag('config', 'UA-99999999-1',, {'send_page_view': false}); // to avoid unnecessary pageviews
 gtag('event', 'visit', {
   'event_category': 'iframe event',
   'event_label': 'some label',
   'value': 0 
 })
</script>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM