简体   繁体   English

在iframe中跟踪点击

[英]Track Click inside a iframe

I want to track with a google analytics event the click on a href that is inside a iframe on my page. 我想通过Google Analytics(分析)事件来跟踪页面上iframe内的href的点击。 The iframe is in the same domain. iframe位于同一域中。 Is it possible? 可能吗? how? 怎么样?
The iframe is insert dynamically on the page after it loads. 加载后,iframe会动态插入页面中。 Is it important for this code to be put after the iframe? 将此代码放在iframe之后是否重要?

If the iframe is on your domain the contents of the iframe are propably under your control ( if it wasn't you wouldn't have any business tracking the frame). 如果iframe在您的域中,则iframe的内容将完全在您的控制之下(如果不是,则您将没有任何业务来跟踪该框架)。 So you can simply install the Google Analytics tracking code in you iframe'd document (which you want to do anyway, after all a framed page is not very different from any other page on your website an you'll want to know how often it was called). 因此,您可以简单地在iframe的文档中安装Google Analytics(分析)跟踪代码(无论如何,您都希望这样做,毕竟框架页面与您网站上的其他页面没有太大不同,因此您需要知道它的频率被称为)。 So while you can access the parent frame from your (same origin) iframe (calling window.parent) it is not necessary or recommendable. 因此,尽管您可以从(相同来源)if​​rame(调用window.parent)访问父框架,但这不是必需的也不建议这样做。

If the iframe is the same domain, you can (from your main window) wait for the iframe to load and then install an event listener to handle the click on a particular link. 如果iframe是同一个域,则可以(从主窗口中)等待iframe加载,然后安装事件侦听器来处理对特定链接的单击。

The code you will need is: 您将需要的代码是:

  1. Code to wait for the iframe content to load. 等待iframe内容加载的代码。
  2. Code to find the relevant link in the iframe 在iframe中查找相关链接的代码
  3. Code to install the event listener 安装事件监听器的代码
  4. Code to handler the click event 处理点击事件的代码

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

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