简体   繁体   English

从iframe发送Google Analytics(分析)事件?

[英]Sending Google Analytics event from within an iframe?

I have a web app that is a single page with an iframe. 我有一个Web应用程序,它是一个包含iframe的页面。 Page and iframe are from the same origin. 页面和iframe来自同一来源。 I'm trying to send an event to google analytics from within an iframe like this: 我正在尝试通过以下iframe将事件发送到Google Analytics(分析):

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. 这些事件不会显示在Google Analytics(分析)中。 However if I send an event like above from the parent of the iframe it works. 但是,如果我从iframe的父级发送类似上述的事件,则它可以正常工作。 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. 如果iframe在另一个域上,并且您可以控制它,则可以使用跨域发布消息功能。 Please go through the link http://blog.teamtreehouse.com/cross-domain-messaging-with-postmessage 请通过链接http://blog.teamtreehouse.com/cross-domain-messaging-with-postmessage

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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