简体   繁体   中英

Sending Google Analytics event from within an iframe?

I have a web app that is a single page with an iframe. Page and iframe are from the same origin. I'm trying to send an event to google analytics from within an iframe like this:

if(typeof parent.ga !== 'undefined') parent.ga('send','event', Category, Action, Label );

I also tried this approach:

if(typeof parent.ga !== 'undefined') parent.sendGaEvent(Category, Action, Label);

And in the parent the function looks like this:

function sendGaEvent(Category, Action, Label){
   ga('send','event', Category, Action, Label);
}

None of those approaches seem to work. The events don't show up in Google Analytics. However if I send an event like above from the parent of the iframe it works. What am I doing wrong?

You need to achieve parent to child communication here. If the iframe is on another domain and you have control over it then you can use Cross-domain post messaging. Please go through the link http://blog.teamtreehouse.com/cross-domain-messaging-with-postmessage

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